opyebCorsLab

Vary: Origin and CDN cache poisoning

Caching · 6 min

In one line: How a cache that keys on the URL alone reuses the wrong response, what it looks like in both directions, and how to set Vary: Origin on each platform.

When the CORS configuration looks right but “some users work and some do not”, suspect the cache. If Access-Control-Allow-Origin changes per request and Vary: Origin is missing, a CDN will happily serve one site's response to another.

1. The cache key is the URL plus Vary

Proxies and CDNs separate responses by URL by default. When one URL must produce different responses, Vary tells the cache what the response depends on. If the allow-origin value depends on the request origin, the response depends on Origin.

2. The poisoning scenario

  1. Site A requests /api/items. The response carries Access-Control-Allow-Origin: https://a.com and the CDN caches it.
  2. Site B requests the same URL and receives the cached response.
  3. B's browser sees https://a.com and blocks it. B reports that it “works sometimes”.

In the other order the outcome is worse: an origin that should have been rejected can receive a response carrying its own value.

3. Setting it per platform

ServerSetting
Nginxadd_header Vary Origin always;
Cloudflare Workersheaders.set("Vary", "Origin")
Express (cors)automatic when origin is a function; otherwise res.vary("Origin")
ApacheHeader always set Vary "Origin" env=...

Never overwrite an existing Vary: merge. If Vary: Accept-Encoding is already present, the result must be Vary: Accept-Encoding, Origin.

4. Does the preflight response need it too?

Yes, for safety. Preflight responses can be cached as well, and a CDN that caches OPTIONS has the same problem. With a long Max-Age the poisoned entry survives for hours.

5. What if Allow-Origin is fixed?

If every origin receives the same value, a mixed cache still produces the same answer, so the immediate risk is low. Add Vary: Origin anyway so that switching to an allowlist later is safe. CorsLab grades a missing Vary as a warning for a dynamic allowlist and as info for a fixed value.

6. Verifying it

curl -sI https://api.example.com/items -H "Origin: https://a.example.com"
curl -sI https://api.example.com/items -H "Origin: https://b.example.com"

Compare Vary and Access-Control-Allow-Origin between the two. Behind a CDN, also look at cf-cache-status or x-cache.

Put the same request and response into the simulator to see whether it passes and which header to change.

Open CorsLab

Frequently asked questions

Does Vary: Origin reduce cache efficiency?

It splits the cache per origin, so the hit rate falls. With a few allowed domains that is fine; with many, consider normalising at the edge or using * where credentials allow.

Does * need Vary?

No. Every origin receives the same value, so there is no reason to split the cache key.

Does it affect the browser cache?

Browsers honour Vary as well. The preflight cache is separate and follows Max-Age.

Sources

Read next

0 bytes of your input sent to a server (advertising and affiliate widgets excluded)