Echoing Origin with credentials lets any site read a signed-in response. Why suffix and regex checks and the null origin fail too, and an exact allowlist with Vary: Origin.
The configuration that made a bug go away
The customer account API at Belgavi's Book Store, https://api.bookstore.example, is called from two browser front ends: the shop at https://shop.bookstore.example and the help-desk console at https://help.bookstore.example. Both send the session cookie, so requests use credentials: 'include'. When the help-desk console moved to its own origin, its requests started failing with a CORS error. A quick fix landed in the API gateway: copy the request's Origin header into Access-Control-Allow-Origin, and add Access-Control-Allow-Credentials: true. The error went away.
So did the protection. With that configuration, a script on any website a signed-in customer visits can read that customer's account data from the API. This guide explains why, walks through the near-misses teams use instead of an exact list, and builds the policy that holds. The interactive lab linked below runs each policy against a set of requests and shows which responses a script could read.
What the browser actually checks
CORS is enforced by the browser, on the response. The Fetch standard's CORS check is short enough to quote nearly in full. The browser takes the response's Access-Control-Allow-Origin value. If there is none, the check fails. "If request's credentials mode is not "include" and origin is *, then return success." Otherwise, if the value is not exactly the request's origin, fail. If the request carries no credentials, succeed. Finally, with credentials, the check succeeds only if Access-Control-Allow-Credentials is true.
Three consequences follow. A wildcard never works for credentialed requests. For credentialed requests, the server must name the exact origin and opt into credentials. And the browser trusts whatever the server names: if the server names the attacker's origin, the check passes for the attacker.
That last point is the whole problem with reflection. The check compares the header to the request's origin. A server that copies the request's origin into the header makes the comparison always true.
Near-misses that fail the same way
The lab runs nine presets. Five of them expose data, and only one of those five is plain reflection:
| Policy | Request origin that gets through | Why |
|---|---|---|
| Reflect every origin | https://evil.example | the comparison is always true |
Reflect, cookies SameSite=Lax | https://old-promo.bookstore.example | same-site subdomain gets the cookie |
Suffix match on bookstore.example | https://evilbookstore.example | a suffix is not a domain boundary |
| Unanchored regular expression for the shop and help hosts | https://shop.bookstore.example.evil.example | the pattern matches inside a longer host |
Exact list plus null | null | sandboxed iframes and data: documents send Origin: null |
The regular expression in the fourth row is https://(shop|help)\.bookstore\.example, used with a search rather than a full match. It looks careful, with escaped dots and an explicit list of hosts, and it still accepts any origin that contains an allowed one.
MDN is direct about the last one: the value null "should not be used", because the origin of sandboxed documents and of resources with schemes such as data: or file: is serialized as null, so an attacker can produce a null origin from a page they control.
Two more presets fail safe but break the product. A wildcard with credentials makes every credentialed request fail the check, including the shop's. And a shared cache without Vary: Origin stores the response it computed for the shop, with Access-Control-Allow-Origin: https://shop.bookstore.example, and serves it to the help-desk console, whose check then fails.
The policy that holds
An exact allowlist of full origins, compared as strings, with Vary: Origin on every response:
ALLOWED_ORIGINS = frozenset({'https://shop.bookstore.example', 'https://help.bookstore.example'})
ALLOWED_METHODS = ('GET', 'POST', 'PATCH')
ALLOWED_HEADERS = ('content-type', 'x-case-id')
def cors_headers(origin, method, preflight_headers='', is_preflight=False):
h = {'Vary': 'Origin'}
if origin is None or origin not in ALLOWED_ORIGINS:
return h
h['Access-Control-Allow-Origin'] = origin
h['Access-Control-Allow-Credentials'] = 'true'
if is_preflight:
asked = [x.strip().lower() for x in preflight_headers.split(',') if x.strip()]
if method.upper() not in ALLOWED_METHODS or any(x not in ALLOWED_HEADERS for x in asked):
return {'Vary': 'Origin'}
h['Access-Control-Allow-Methods'] = ', '.join(ALLOWED_METHODS)
h['Access-Control-Allow-Headers'] = ', '.join(ALLOWED_HEADERS)
h['Access-Control-Max-Age'] = '600'
h['Vary'] = 'Origin, Access-Control-Request-Method, Access-Control-Request-Headers'
return hSet Vary: Origin even on responses that grant nothing: a cached refusal served to an allowed origin breaks it just as a cached grant served to the wrong one would. MDN recommends exactly this whenever the server returns an explicit origin rather than *.
The list belongs in configuration owned by the team that owns the front ends, reviewed like any other access rule. If you have many tenant subdomains, generate the list from the tenant registry rather than reaching for a pattern; if you must use a pattern, anchor it at both ends, escape the dots, and test it against the look-alike hosts in the table.
Preflights are not a separate defence
Requests with methods other than GET, HEAD and POST, or with non-safelisted headers such as X-Case-Id, are preceded by an OPTIONS preflight. The preflight response goes through the same CORS check, and additionally must list the method and headers. In the lab's preflight preset the help-desk console's PATCH with X-Case-Id is blocked until both are added, and the attacker's identical request stays blocked because its origin is not listed. A failed preflight stops the real request from being sent at all.
That is the one case where CORS stops a request from reaching the server. For simple requests it does not: the browser sends the GET or the form-encoded POST with the cookie and only hides the response from the script. In the lab's exact-allowlist preset, all five requests carry the cookie and three are blocked from reading. If a GET changes state, CORS does nothing for you. That is cross-site request forgery, and it needs its own defences: no state changes on GET, a CSRF token or a required custom header on writes, and SameSite cookies.
Migrating a reflecting configuration
- Log the
Originheader of credentialed requests for a week, and build the list from what you see, removing anything you do not own. - Deploy the exact list in report-only form if your gateway supports it, logging what would have been refused.
- Switch to enforcement, with
Vary: Originon every response, and purge caches that hold responses without it. - Remove
nulland any patterns. Search the codebase and the gateway configuration forendsWith, unanchored regexes and*next toAllow-Credentials. - Audit DNS for subdomains pointing at resources you no longer control; same-site takeover defeats
SameSite.
Verification
The companion example has seven offline tests: the exact policy echoes only listed origins and always varies on Origin; null, look-alike suffixes and embedded hosts get nothing; preflights check method and headers; and the lab model, which follows the Fetch standard's CORS check step by step, flags every weak preset. The model simplifies browser behaviour: it treats the last two host labels as the site, ignores the public suffix list, private network access and third-party cookie blocking, and models one shared cache. Check real browser behaviour in your own test page before relying on an edge case.
Interactive experiment
CORS with credentials: which origins can read the response. Synthetic browser-local fixtures, adjustable inputs, decision traces and JSON export.