CORS headers: preflight, credentials and why the errors are so opaque
This CORS header generator builds the Access-Control-* response set for four common scenarios and includes a simulator that shows whether a given request preflights and whether the browser will allow it. Below: what CORS actually protects, and the three bugs behind most failures.
What CORS is for
Browsers enforce the same-origin policy: script on one origin cannot read responses from another. That is what stops a malicious page reading your webmail in a background request while you are logged in.
CORS is the controlled exception. A server opts in by returning Access-Control-* headers saying which origins may read its responses. The browser enforces the answer.
The consequence people miss: CORS protects the user, not the server. A cross-origin request that fails CORS has usually still arrived at your server and still executed. The browser simply refuses to hand the response back to the calling page. Anything that must not happen requires authorisation, not CORS.
Simple requests and preflight
Some cross-origin requests go straight out. Others are preceded by an OPTIONS request asking permission first. A request is simple — no preflight — only if all three hold:
- The method is
GET,HEADorPOST. - The
Content-Typeisapplication/x-www-form-urlencoded,multipart/form-dataortext/plain. - There are no non-safelisted request headers.
| Request | Preflight? | Why |
|---|---|---|
GET, no custom headers | No | Simple. |
POST form-encoded | No | Simple content type. |
POST JSON | Yes | JSON is not a simple content type. |
GET with Authorization | Yes | Non-safelisted header. |
PUT, PATCH, DELETE | Yes | Not a simple method. |
application/json is not on that list. Every JSON POST is therefore preflighted, which is the single most common surprise in the whole topic. Adding an Authorization header also forces a preflight regardless of anything else.
Preflight must not require authentication
This causes more wasted debugging than anything else in CORS.
The browser sends the OPTIONS preflight without cookies and without an Authorization header, by design — it is asking whether it may send the real request, so it does not yet send credentials.
If your authentication middleware runs first and returns 401 for the unauthenticated OPTIONS, the preflight fails. The browser then reports a CORS error, and you spend an afternoon adjusting Access-Control-Allow-Origin when the headers were never the problem.
- Handle OPTIONS firstAnswer preflight before any auth middleware in the chain.
- Return 204No body needed. Any 2xx works; 204 is conventional.
- Echo the CORS headers on itThe preflight response carries the permissions, not the actual response.
- Do not require a tokenThere will not be one.
- Cache it
Access-Control-Max-Ageavoids paying for the round trip on every call.
Credentials change the rules
Sending cookies or an Authorization header cross-origin requires opt-in on both sides: credentials: 'include' in the client, and Access-Control-Allow-Credentials: true on the server. Three restrictions then apply:
Access-Control-Allow-Origincannot be*. It must be a specific echoed origin. The browser rejects the wildcard outright.Access-Control-Allow-Headers: *stops meaning "any header" and is treated as a literal asterisk. List headers explicitly.- The same applies to
Access-Control-Expose-Headers.
null origin with credentials enabled. Sandboxed iframes, file:// documents and certain redirects all produce a null origin, so any site can obtain one. It is not a safe identifier.And a security point: an origin on a credentialed allowlist can act as the logged-in user. Only list origins you control, and treat adding one as a privilege grant rather than a configuration change.
Vary: Origin is not optional
Because Access-Control-Allow-Origin holds one value, supporting multiple origins means reading the request's Origin header and echoing it back. That makes the response depend on a request header — and caches need telling.
Without Vary: Origin, a CDN or reverse proxy may store the response generated for https://app.example.com and serve it, headers included, to a request from anywhere else. The second origin receives an Access-Control-Allow-Origin naming the first, which usually just breaks — but with the right cache ordering it hands a CORS grant to an origin you never allowlisted.
The symptom is intermittency: it works, then it does not, depending on who warmed the cache. That signature almost always means a missing Vary.
Reading response headers
By default, JavaScript can only read six response headers cross-origin: Cache-Control, Content-Language, Content-Type, Expires, Last-Modified and Pragma.
Anything else — a pagination count, a rate-limit header, a Link header, a custom request ID — is invisible unless you name it in Access-Control-Expose-Headers. The header is present on the wire and in the network tab, which makes this confusing: developers can see it in DevTools and cannot read it in code.
undefined in your code, you need Access-Control-Expose-Headers. Nothing is wrong with the request.Related tools
- Fetch API Request Generator — the client side, including
credentials. - curl Command Generator — reproduce a request without CORS in the way.
- Content Security Policy Generator — the other major browser security header.
- Permissions-Policy Generator — disable unused browser features.
- Axios Request Generator — instances and interceptors for the calling side.