CORS

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 Origin header 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'), which evil-yoursite.com would pass. Compare the full origin.
  • Be careful with credentials. Only enable Allow-Credentials for 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 Origin you 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.

Related reading

FAQ

What is reflected-origin CORS?
It is when your server copies whatever Origin the request sent straight into the Access-Control-Allow-Origin response header. It looks like a specific allowlist but effectively trusts every site, since it reflects whatever origin asked — including a malicious one.
Why is reflecting the origin dangerous with credentials?
If you also send Access-Control-Allow-Credentials: true, the browser will send a logged-in user's cookies and let the requesting page read the authenticated response. A malicious site can then read your user's private data across origins.
How do I fix a reflected-origin CORS setup?
Keep an explicit allowlist of the exact origins you support and only reflect a value if it matches one of them exactly. Avoid loose suffix checks, and only enable credentials for fully trusted origins — never alongside a reflect-everything rule.