Guide

CORS explained: why the browser blocked your request and how to fix it

Published · 8 minute read

CORS errors are frustrating because the request often works perfectly from the command line and fails only in the browser. That is not a bug. It is the clue to what CORS is: a rule that browsers enforce on behalf of their users, which a server can choose to relax.

The rule being relaxed: the same-origin policy

An origin is the combination of scheme, host and port. These are four different origins:

https://example.com
http://example.com          ← different scheme
https://api.example.com     ← different host
https://example.com:8443    ← different port

By default, a script running on one origin may send requests to another, but may not read the responses. The reason is that your browser attaches your cookies to requests automatically. Without this rule, any page you visited could quietly request your webmail or your bank and read what came back, as you.

Cross-Origin Resource Sharing is the mechanism by which a server says "responses from me may be read by scripts from these other origins". The server states the policy in response headers; the browser enforces it.

That explains the command-line puzzle. A tool like curl is not a browser, carries nobody's cookies and enforces nothing. It follows that CORS is not access control for your API. It protects users of browsers. It does not stop anyone from calling your server directly, so it is never a substitute for authentication.

Simple requests and preflighted requests

Browsers divide cross-origin requests into two kinds.

A simple request is one that an HTML form could already have made before CORS existed: a GET, HEAD or POST with only basic headers and a content type of text/plain, multipart/form-data or application/x-www-form-urlencoded. The browser sends it directly, then checks the response headers to decide whether the script may see the result.

Anything else triggers a preflight. That includes most API calls, because sending Content-Type: application/json, using PUT, PATCH or DELETE, or adding a header such as Authorization all take a request out of the simple category. Before the real request, the browser asks permission with an OPTIONS request:

OPTIONS /orders HTTP/1.1
Origin: https://app.example.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type, authorization

The server must answer with what it allows:

HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: content-type, authorization
Access-Control-Max-Age: 600

Only if this answer covers the method and every header does the browser send the real request. Note that a simple request is delivered to your server even when the response is then hidden from the script. CORS controls reading, not sending, which is why it is not a defence against cross-site request forgery.

What each header does

Response headerPurpose
Access-Control-Allow-OriginThe one origin allowed to read the response, or * for any
Access-Control-Allow-MethodsMethods permitted, in the preflight answer
Access-Control-Allow-HeadersRequest headers permitted, in the preflight answer
Access-Control-Allow-Credentialstrue if cookies or HTTP authentication may be included
Access-Control-Expose-HeadersResponse headers the script may read beyond the basic set
Access-Control-Max-AgeSeconds the browser may cache the preflight answer

Access-Control-Allow-Origin takes a single origin or a wildcard. It does not take a list. To allow several origins, the server compares the incoming Origin header against its own allow-list and echoes back the matching value. When it does that, it must also send Vary: Origin, or a cache may serve one origin's response to another.

Cookies change the rules

If the request includes credentials (the script sets credentials: "include" in fetch), three stricter requirements apply:

This is the most common "it works without cookies but not with them" failure. The tempting shortcut is to echo back whatever Origin arrives while also allowing credentials. That makes every website on the internet a trusted reader of your users' authenticated data, which is exactly what the same-origin policy exists to prevent. Always check against an explicit list.

Reading the error message

The browser console tells you which check failed. The common ones:

The script itself cannot see any of this. It receives only a generic network error, by design, so that it learns nothing about the other origin. Always debug CORS from the browser's network tab, where the preflight and its headers are visible, or reproduce the preflight in a CORS tester.

Fixes that work, and fixes that only appear to

A sensible default configuration

  1. Keep an explicit list of allowed origins per environment. No wildcard for anything that uses cookies or returns private data.
  2. Echo the matching origin and send Vary: Origin.
  3. Allow only the methods and headers you use.
  4. Set Access-Control-Max-Age to several minutes so that each request is not preceded by a preflight.
  5. Use the wildcard only for truly public, read-only resources such as open data or fonts.

Tools used in this guide

More guides