Vary: Origin and CDN cache poisoning
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
- Site A requests
/api/items. The response carriesAccess-Control-Allow-Origin: https://a.comand the CDN caches it. - Site B requests the same URL and receives the cached response.
- B's browser sees
https://a.comand 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
| Server | Setting |
|---|---|
| Nginx | add_header Vary Origin always; |
| Cloudflare Workers | headers.set("Vary", "Origin") |
| Express (cors) | automatic when origin is a function; otherwise res.vary("Origin") |
| Apache | Header 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 CorsLabFrequently 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
- When a CORS preflight (OPTIONS) is sent, and what the browser checksThe exact conditions that make the browser send OPTIONS before the real request, the three headers it carries, and the order in which the response is checked.
- Access-Control-Allow-Origin, credentials: include and the wildcard banExact-match rules for Allow-Origin, the multiple-value error, why * is forbidden with credentials, and the three headers the rule applies to.
- Simple-request checks: method, headers and Content-TypeThe Fetch Standard safelist explained value by value, including the byte rules for header values and the Range exception.