max-age=60 meant for browsers let a shared cache serve one customer account page to two others. RFC 9111 rules, CDN minimum TTLs, Vary and a header test that catches it.
An account page served to the wrong customer
Belgavi's Book Store put a CDN in front of its website to speed up the catalogue. A week later, Jenya opened /account and saw Geetha's name and recent orders. Nothing was hacked. The account page had been served with Cache-Control: max-age=60, which a developer had added so that browsers would not refetch it on every back-button press. The CDN read the same header, stored Geetha's page for 60 seconds, and served it to the next two customers who asked for /account.
This guide works through the rules in RFC 9111, HTTP Caching, that decide when a shared cache may store and reuse a response, shows where CDN settings override them, and turns that into headers and a test. The companion code implements those rules for a shared cache and replays the store's mistake under nine header and configuration choices. It models the rules; it is not a CDN.
The afternoon, replayed
The companion example sends four requests through one shared cache: Geetha, Jenya, Tanishka, then Geetha again, ten seconds apart, each with a separate session cookie. The origin personalises the page from the cookie.
| Response headers and cache setting | Pages shown to the wrong customer | Cache hits |
|---|---|---|
max-age=60 | 2 (Jenya and Tanishka both see Geetha) | 3 |
private, max-age=60 | 0 | 0 |
no-store | 0 | 0 |
max-age=60 with Vary: Cookie | 0 | 1 |
private, max-age=60, CDN minimum TTL 300 s | 2 | 3 |
max-age=60, credentials in Authorization | 0 | 0 |
public, max-age=60, credentials in Authorization | 2 | 3 |
max-age=60 plus Set-Cookie: cart=... | 2, and both receive Geetha's cookie | 3 |
max-age=60, cookie in the CDN cache key | 0 | 1 |
Four rows deserve a note.
private is the intended fix. It keeps the browser benefit the developer wanted (a private cache may still store it for 60 seconds) and forbids shared caches from storing it.
A CDN minimum TTL can override the origin. CloudFront's documentation says that when the origin adds no-cache, no-store or private, CloudFront respects them only if the minimum TTL is 0; with a minimum TTL above 0, CloudFront caches the object for the minimum TTL. One setting chosen for static assets, applied to the whole distribution, undoes the header.
Authorization is protected until someone adds public. Section 3.5 stops shared caches from reusing responses to authenticated requests, unless the response opts in. Adding public "to make caching work" for an API opts every user's response in.
Set-Cookie gets cached along with the body. The second and third customers receive Geetha's cart cookie as well as Geetha's account page.
Vary and the cache key
Vary: Cookie tells a cache that the stored response may be reused only for requests whose Cookie header matches. RFC 9111 section 4.1 requires that; Vary: * means it never matches. It prevents the leak, but every customer gets a separate copy, and any change to any cookie (an analytics cookie, a consent cookie) creates another. In the replay, it saved one origin request out of four. For pages that are genuinely personal, private is simpler and says what you mean.
CDNs also let you choose the cache key directly. CloudFront separates the cache policy (what forms the key) from the origin request policy (what is forwarded to the origin). Its documentation describes the origin request policy as the way to send information to the origin without including it in the cache key. That is exactly the configuration in which a page can vary by a cookie the cache does not key on: forward the session cookie for personalisation, leave it out of the key for hit rate, and every customer shares one copy.
Headers that say what you mean
Decide per route, not per site:
# Personal pages and APIs
Cache-Control: private, no-store # or private, max-age=0 if back-button caching does not matter
# Public catalogue pages
Cache-Control: public, max-age=60, s-maxage=600
# Public pages that set a cookie: do not set cookies on cacheable responsesThen make the CDN agree: minimum TTL 0 on personal paths, cookies and Authorization either excluded from forwarding or included in the cache key, and a separate behaviour for static assets with their own long TTLs.
A test that would have caught it
A unit test on the origin that asserts headers per route is cheap and catches the developer's change:
def test_account_page_is_not_shared_cacheable(client):
r = client.get("/account", cookies={"session": "test-geetha"})
cc = {d.strip().split("=")[0] for d in r.headers["Cache-Control"].lower().split(",")}
assert "private" in cc or "no-store" in cc
assert "public" not in cc and "s-maxage" not in ccIt does not catch the CDN override. For that, run a two-user test against the real edge in a staging environment: sign in as two test customers, request the same personal URL as both within the TTL, and assert that each one sees only the data of that account and no Age or cache-hit header on the second response.
Failure modes
- Headers set for browsers, read by CDNs.
max-ageapplies to both kinds of cache. Useprivatewhen only browsers should store. - Framework defaults that change. A framework upgrade or middleware that adds caching headers to every
200response can turn personal pages cacheable. The header test above catches it. - Error and redirect pages. RFC 9110 lists
301and404among the status codes that are heuristically cacheable, so a personalised one can be stored without any freshness header; a302can be stored when it carries explicit freshness such asmax-age. - Purge is not a fix. Purging after a leak removes the copy, not the cause. Fix headers and configuration first.
When this is unnecessary
If nothing personal is ever served from a cached hostname, for example account pages live on a separate origin that bypasses the CDN entirely, the risk is architectural rather than per header. Even then, keep the header test: routes move.
Verification
The companion tests assert each row of the table above, that Vary: * never matches, and that directive parsing is case-insensitive. They verify the model of the rules, not any CDN. For the edge, use the two-user staging test.