Skip to main content

Caching personalised responses: how a CDN serves one user's data to another

A cache leak needs no exploit code. A personalised response gets stored by a CDN or reverse proxy, and the next person who requests the same URL receives it: their name, email, order history, sometimes a CSRF token or an API key. It usually starts with a performance change, such as a new CDN rule, a framework upgrade that changed default headers, or a middleware that set max-age on everything.

This post covers how shared caches decide what to store, the three ways personalised data ends up in them (including web cache deception), the same bug inside application caches, and the headers and code that prevent it.

How a shared cache decides to store a response

HTTP caching is defined by RFC 9111. A few of its rules decide most leaks:

Rule Section What it means for you
private 5.2.2.7 A shared cache MUST NOT store the response. The browser still may.
no-store 5.2.2.5 No cache may store any part of the request or response.
s-maxage 5.2.2.10 Overrides max-age for shared caches only.
Authenticated requests 3.5 A shared cache must not reuse a response to a request with an Authorization header unless the response explicitly allows it.
Vary: * 4.1 A stored response with Vary: * never matches a later request.

Notice what section 3.5 does not cover: cookies. A response to a cookie-authenticated request has no special protection in the standard. If it carries Cache-Control: public, max-age=300, or no explicit freshness at all on a status code that caches may store heuristically, a shared cache is allowed to keep it. CDNs add their own rules on top, often "cache anything that ends in .css, .js or .png" or "cache everything under /static/", which matter for web cache deception below.

Failure 1: the personalised response says it is cacheable

The direct case. A global middleware sets caching headers for performance, and it also runs on /api/me:

// Applied to every route, including authenticated ones
app.use((req, res, next) => {
  res.set("Cache-Control", "public, max-age=300");
  next();
});

The fix is to make "authenticated" and "cacheable by others" mutually exclusive, in one place:

function noSharedCache(req, res, next) {
  res.set("Cache-Control", "private, no-store");
  next();
}

app.use("/api", requireSession, noSharedCache, apiRouter);
app.use("/account", requireSession, noSharedCache, accountRouter);

In Django, the never_cache decorator adds Cache-Control: max-age=0, no-cache, no-store, must-revalidate, private (only if the header is not already set). Use private alone if you want the browser to keep its own copy, and no-store when the response contains secrets or tokens.

Failure 2: the cache key misses what varies

A cache stores responses under a key, usually the method, host and URL. If the response depends on something that is not in the key, such as the session cookie, the Authorization header, a tenant subdomain mapped to one origin, or a header that picks the account, two users share one entry.

Vary: Cookie tells a cache to include the cookie in the key. In practice, many CDNs ignore Vary or honour it only for a few headers, and a cookie-keyed entry is useless anyway because every user's cookie differs. Do not rely on Vary to keep personalised data safe in a shared cache. Mark the response private instead, and use Vary for what it is good at: Accept-Encoding and content negotiation on public responses.

Failure 3: web cache deception

Web cache deception tricks the cache into storing a dynamic, personalised page by making its URL look static. The attacker sends the victim a link such as:

https://app.example.com/account/settings/x.css

If the application ignores the trailing segment and returns the victim's settings page, and the CDN caches anything ending in .css, the CDN stores the victim's page under that URL. The attacker then requests the same URL and reads it.

PortSwigger Research's 2024 work showed how far this goes beyond trailing segments. Origins and caches disagree about path delimiters (a semicolon in Spring, a dot in Rails, an encoded null byte in some servers) and about normalising encoded dot-segments, so a URL can match a static cache rule at the CDN while the origin routes it to a dynamic page. Static directory rules (/static/..%2faccount) and exact filename rules (/robots.txt) are reachable the same way.

Defences, in order of strength:

  1. Cache-Control: private, no-store on every personalised response. A cache that respects origin headers will not store it, whatever the URL looks like.
  2. Strict routing. Return 404 for unexpected trailing segments and extensions on dynamic routes, rather than matching /account/settings/*.
  3. Narrow CDN rules. Cache by explicit static paths that only your build output populates, and configure the CDN to respect origin Cache-Control instead of overriding it by extension. Some CDNs can also refuse to cache a response whose Content-Type does not match the URL extension.

The same bug inside your application cache

Application caches leak in the same way, with no CDN involved. The most damaging version skips authorization entirely:

def get_invoice(request, invoice_id):
    cached = cache.get(f"invoice:{invoice_id}")
    if cached:
        return JsonResponse(cached)  # returned before any ownership check

    invoice = Invoice.objects.get(id=invoice_id, account=request.user.account)
    data = serialize(invoice)
    cache.set(f"invoice:{invoice_id}", data, timeout=300)
    return JsonResponse(data)

The first request is authorized. Every later request for the same id, from any user, gets the cached copy without the ownership check. Put the authorization before the cache read and the tenant in the key:

def get_invoice(request, invoice_id):
    account_id = request.user.account_id
    key = f"acct:{account_id}:invoice:{invoice_id}"
    data = cache.get(key)
    if data is None:
        invoice = get_object_or_404(Invoice, id=invoice_id, account_id=account_id)
        data = serialize(invoice)
        cache.set(key, data, timeout=300)
    response = JsonResponse(data)
    response["Cache-Control"] = "private, no-store"
    return response

Because the key contains the account, a cache hit can only return data that the same account was already authorized to read.

How to test for it

  • Request a personalised URL as user A, then as user B (or with no session). Compare the bodies, and look at the Age header and the cache status header your CDN adds.
  • Repeat with /x.css, ;x.css, %2f..%2fx.css and .json appended to account pages, and check whether any of them return the personalised page with a cache hit on the second request.
  • List the CDN rules that override origin headers, and check that each one only matches build output.
  • Grep the code for global middleware that sets Cache-Control, and for cache.get calls that run before an authorization check.

Finding caching bugs with Vulkro

Vulkro Core checks the code side of this: what authenticated handlers send and how application cache keys are built.

vulkro scan . --format json > findings.json
vulkro explain cached-response-skips-object-authz
Rule What it flags
auth-endpoint-no-cache-private An authenticated route that sends responses with no Cache-Control: private, or with a non-private value
cached-response-skips-object-authz A cache keyed by the caller-chosen id alone, returned on a hit before the object authorization runs
cache-key-no-tenant-prefix A per-record cache key with no tenant or org prefix
mt-unscoped-cache-key A cache key without the tenant when the handler binds a tenant on the principal

CDN configuration and path normalisation live outside the repository in most setups, so the deception tests above still need to be run against the deployed stack.

FAQ

What is the difference between Cache-Control private and no-store?

private means shared caches such as CDNs and proxies must not store the response, but the user's own browser may. no-store means no cache at all may store it. Use private for personalised pages and no-store for responses that contain secrets or tokens.

What is web cache deception?

An attack where the victim is lured to a URL that looks static, such as /account/x.css. The origin returns the victim's personalised page, the CDN caches it because of the extension, and the attacker fetches the cached copy.

Not reliably. Many CDNs ignore Vary or support only some headers, and a cookie-keyed entry is not reusable anyway. Mark personalised responses Cache-Control: private or no-store instead.

See the API security use case and the OWASP API Top 10 rules, or start a 14-day trial. Related: the missing tenant column and IDOR prevention in Express and NestJS.