You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bug: harden cache middleware key generation and restore Methods config
- Restore configurable Methods field (default: GET, HEAD) to replace hardcoded method check, with uppercase normalization and nil vs empty-slice semantics (nil = default, [] = disable caching)
- Fix escapeKeyDelimiters fast-path bug: backslash was not checked, allowing collisions between literal "\p" and escaped "|"
- Fix path delimiter injection: escape pipe/colon/backslash in request path before boundKeySegment to prevent crafted paths from manipulating cache key structure
- Optimize canonicalQueryString: add single-param fast path (skips url.ParseQuery/sort) and use sync.Pool for output buffer
- Simplify string conversions: replace utils.CopyString(utils.UnsafeString(buf)) with string(buf) and utils.UnsafeBytes(boundKeySegment(...)) with direct string append
- Add comprehensive tests: Methods config (POST caching, bypass, empty-slice, lowercase normalization), escapeKeyDelimiters unit regression test with collision-pair verification
- Update docs: Methods field in config table, default config, and migration guide
Copy file name to clipboardExpand all lines: docs/middleware/cache.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -120,7 +120,7 @@ This prevents common collisions from path-only keys (for example, `/?id=1` vs `/
120
120
121
121
The middleware **does not include request body/form values in the default cache key**.
122
122
123
-
Cache lookup/storage is only applied for `GET` and `HEAD` requests. Other HTTP methods always bypass the cache middleware.
123
+
Cache lookup/storage is applied only for `GET` and `HEAD` requests by default. Other HTTP methods bypass the cache middleware. You can change this via the `Methods` config field.
124
124
125
125
If a response sets `Vary`, request lookup/storage is also partitioned by those header values unless `DisableVaryHeaders` is `true`. Responses with `Vary: *` remain uncacheable.
126
126
@@ -138,6 +138,7 @@ If a response sets `Vary`, request lookup/storage is also partitioned by those h
138
138
| DisableQueryKeys |`bool`| Disables canonicalized query params in keys. |`false`|
139
139
| KeyHeaders |`[]string`| Header allow-list used for key partitioning. Names are normalized case-insensitively and sorted. Use `[]string{}` to disable header-based partitioning. |`[]string{"accept","accept-encoding","accept-language"}`|
| Methods |`[]string`| HTTP methods eligible for caching. Requests whose method is not in this list bypass the cache. |`[]string{fiber.MethodGet, fiber.MethodHead}`|
| ExpirationGenerator |`func(fiber.Ctx, *cache.Config) time.Duration`| ExpirationGenerator allows you to generate custom expiration keys based on the request. |`nil`|
143
144
| Storage |`fiber.Storage`| Storage is used to store the state of the middleware. | In-memory store |
Copy file name to clipboardExpand all lines: docs/whats_new.md
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1369,7 +1369,7 @@ Cache keys are now redacted in logs and error messages by default, and a `Disabl
1369
1369
1370
1370
The default cache key strategy was also hardened. Instead of path-only behavior, keys now use structured request dimensions: method partitioning, path, canonical query string, and selected representation headers (`Accept`, `Accept-Encoding`, `Accept-Language`). This avoids collisions such as `/items?id=1` vs `/items?id=2` while keeping key generation deterministic. New config fields were added for explicit control: `DisableQueryKeys`, `KeyHeaders`, `KeyCookies`, and `DisableVaryHeaders`.
1371
1371
1372
-
As a security/performance default, request body/form values are not part of the default cache key. Cache handling is limited to `GET` and `HEAD` requests.
1372
+
As a security/performance default, request body/form values are not part of the default cache key. Cache handling is limited to `GET` and `HEAD` requests by default, configurable via the `Methods` field.
1373
1373
1374
1374
:::note
1375
1375
The deprecated `Store` and `Key` options have been removed in v3. Use `Storage` and `KeyGenerator` instead.
@@ -2890,6 +2890,7 @@ To restore v2 behavior:
2890
2890
2891
2891
Additional v3 cache key options:
2892
2892
2893
+
- `Methods`: HTTP methods eligible for caching (default `GET`, `HEAD`)
2893
2894
- `DisableQueryKeys`: disable canonicalized query args in keys (default `false`)
2894
2895
- `KeyHeaders`: request header allow-list for key partitioning
2895
2896
- `KeyCookies`: explicit cookie allow-list for key partitioning
0 commit comments