Reflected-origin CORS: when 'allow everyone' hides in your code
A wildcard in disguise
Cross-Origin Resource Sharing (CORS) is how your server tells the browser which other origins are allowed to read its responses. The obvious mistake is setting Access-Control-Allow-Origin: * — allow everyone. The subtler and more dangerous one looks careful but is not: reflecting the request's Origin header straight back into the response. The header ends up naming a specific site, so it seems restrictive — but it names whatever site asked, which means all of them.
How reflection sneaks in
The code that causes this is usually well-intentioned. A developer wants to support a few frontends on different domains, finds that a literal * will not work with credentials, and writes something that takes the incoming Origin and echoes it back:
- Read the
Originheader from the request. - Put that exact value into
Access-Control-Allow-Origin. - Ship it.
It passes every test, because in testing the origin is always your own site, so it always matches. In production, an attacker's page sends its own origin, your server politely reflects it, and the browser concludes that attacker's page is allowed.
Why credentials make it serious
Reflection on its own, for public data, is messy but limited. The danger arrives with Access-Control-Allow-Credentials: true, which tells the browser it is fine to send cookies and read the authenticated response across origins. Now combine the two:
- A logged-in user visits a malicious page.
- That page makes a request to your API; the browser includes the user's cookies.
- Your server reflects the attacker's origin and allows credentials.
- The attacker's JavaScript reads the authenticated response — the user's private data.
That is the chain that turns a config convenience into a real data-exposure bug. The user did nothing wrong — they were simply logged in and visited the wrong page — which is what makes this class of issue worth taking seriously.
How to set CORS correctly
The principle is simple: never trust an origin you did not explicitly approve.
- Keep an allowlist of the exact origins you support, and compare incoming origins against it. Only reflect a value if it is on the list.
- Match exactly. Avoid loose checks like
endsWith('yoursite.com'), whichevil-yoursite.comwould pass. Compare the full origin. - Be careful with credentials. Only enable
Allow-Credentialsfor origins you fully trust, and never alongside a blanket reflect-everything rule. - Default to no CORS for endpoints that do not need cross-origin access at all — the safest setting is not sending permissive headers in the first place.
- Test with a hostile origin. Before shipping, send a request with an
Originyou do not own and confirm your server does not reflect it back. If it does, your allowlist is not really an allowlist.
Find out what your API really allows
CORS behaviour is invisible from the page and easy to get subtly wrong — the header looks fine until you send it an unexpected origin. An automated scan probes your responses with different origins and reports whether your API is reflecting origins or allowing more than it should. Scan your site and check what your CORS policy actually permits.