DEV Community

Ioneyrezende
Ioneyrezende

Posted on

Private responses behind shared caches: closing web cache deception gaps

Private responses behind shared caches: closing web cache deception gaps

A private account page can become a public document when an intermediary sees a cacheable request and a response that does not state who may store it. The result is not a flaw in HTTP caching itself. It is a mismatch between three systems that should agree: the origin application, the reverse proxy, and the shared cache in front of them.

The OWASP Web Cache Security guidance separates cache poisoning, cache deception, and cross-tenant leakage. Cache deception is especially easy to miss because an authenticated user may request a path that the application treats as private while the cache treats the extension, path, or URL normalization differently. A response intended for one authenticated session then becomes available at a URL-shaped key that another visitor can request. [1]

The decision belongs to the origin

RFC 9111 makes the response directives the primary contract for shared caches. A shared cache must not store a response marked private for reuse across users. The absence of a directive is not a reliable security boundary because storage rules also depend on status codes, methods, freshness information, and cache configuration. [2]

A secure implementation should make privacy explicit on every response that depends on identity, authorization, a tenant, a CSRF token, or another user-specific value. That includes JSON API responses, fragments, redirects, error pages, and download metadata. Teams should test the exact response emitted after middleware, not only the controller's intended behavior.

Path confusion is a cache problem and an application problem

Cache deception often relies on paths such as a private route followed by a static-looking suffix. The origin may route the request to an account controller and return private data. A cache may see a static extension and choose a long lifetime. Neither component is necessarily malfunctioning in isolation. They disagree about the meaning of the URL.

The fix starts with canonical routing. Normalize the path once, reject ambiguous segments and encoded separators, and ensure that the origin and the cache make decisions from the same representation. Serve authenticated content only from routes that cannot be mistaken for static assets. If a CDN has a rule for all .css, .js, or image paths, that rule should apply after the origin has established that the request cannot reach a dynamic authorization handler.

Response headers are the second layer. Set Cache-Control: no-store or a correctly qualified private policy for sensitive pages. Do not rely on Vary: Cookie as a complete answer. RFC 9111 explains what a shared cache is allowed to do with a response, but a deployment may have a cache that ignores, strips, or mishandles a header. Test the deployed edge, not a local development server.

Review the cache key

Ask what fields distinguish one stored object from another. The key may include the host, path, query string, selected headers, device class, or country. It should not include a user identifier unless the cache is private to that user. A cache key that omits a tenant header can mix tenants even when every backend request is authorized correctly.

Review query parameters as well. Tracking parameters, preview keys, locale selectors, and feature flags can create many copies of a private page. Some caches also normalize parameter order or drop unknown parameters. A sensitive response cached under the normalized form may be served to a request that carries different security context. The safest design keeps authorization-sensitive responses out of shared caches rather than expecting every edge rule to preserve the full context.

Test with the browser and the proxy

Build a small authorization matrix. For each critical route, record the anonymous response, the response for user A, and the response for user B. Inspect status, body, Cache-Control, Vary, Set-Cookie, and any CDN-specific cache status header. Then repeat with encoded paths, trailing suffixes, duplicate parameters, alternate host names, and malformed percent encodings. The purpose is to find places where one component normalizes differently.

Log the cache decision alongside the application request identity. A useful log line contains the request ID, normalized path, tenant or account identifier, chosen cache rule, cache key hash, and whether the response was stored, bypassed, or rejected. Do not log session cookies or full Authorization headers. The goal is to explain a leak after the fact without creating a new credential store.

Operational controls that survive change

Put sensitive-route caching in a deny-by-default policy owned by the application team. Add a deployment test that fails when a sampled authenticated page returns a shared-cacheable response. Review CDN and reverse-proxy rules during every route migration. When a new framework adds middleware or changes URL parsing, rerun the matrix because the old assumption may no longer hold.

Finally, define containment. If a private page is cached, purge the affected key and every alias that normalized to it. Rotate anything the page exposed, such as reset tokens or account details. Preserve the cache logs and origin logs long enough to identify who received the response. A purge without an access review leaves the impact unknown.

The cache is a shared component with its own interpretation of identity. Encryption and login checks do not repair a URL that the edge considers public. Make the route, response policy, and cache key agree before the first sensitive response reaches the edge.

References

[1] OWASP, "Web Cache Security Cheat Sheet." https://cheatsheetseries.owasp.org/cheatsheets/Web_Cache_Security_Cheat_Sheet.html

[2] Fielding, Nottingham, and Reschke, "RFC 9111: HTTP Caching." https://www.rfc-editor.org/rfc/rfc9111.html

Top comments (0)