Web Development

What is CORS?

Cross-Origin Resource Sharing — a security mechanism controlling which domains can access resources.

Definition

CORS (Cross-Origin Resource Sharing) is a browser security feature that restricts web pages from making requests to a different domain than the one serving the page. When a frontend at app.example.com tries to fetch data from api.other.com, the browser sends a preflight OPTIONS request. The server must respond with appropriate Access-Control-Allow-* headers to permit the cross-origin request.

What the browser sends, and what it needs back

A page on https://app.example.com calls PUT https://api.example.com/v1/orders/42 with a JSON body and a bearer token. The method is PUT and the request carries Content-Type: application/json and Authorization, so the browser first sends a preflight:

OPTIONS /v1/orders/42 HTTP/1.1
Host: api.example.com
Origin: https://app.example.com
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: authorization, content-type

The API has to answer with headers that name that origin, that method and those headers:

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

Only then does the browser send the real PUT. If a header is missing or does not match, the PUT is never sent and the console shows a CORS error. A simple request skips the preflight: the browser sends it straight away and, if the response lacks a matching Access-Control-Allow-Origin, hides the response from your JavaScript. The server still received and processed it.

Which requests trigger a preflight

RequestPreflight?Why
GET, HEAD or POST with only safelisted headers, the body sent as form data or plain textNoA "simple" request: HTML forms could always send these
POST with Content-Type: application/jsonYesJSON is not a safelisted content type
Any request with Authorization or a custom header such as X-Api-KeyYesNon-safelisted request header
PUT, PATCH or DELETEYesNot a safelisted method

Cookies and credentials

To send cookies cross-origin, the page has to ask for them (fetch(url, { credentials: 'include' })) and the server has to reply with Access-Control-Allow-Credentials: true and a specific origin. Browsers reject Access-Control-Allow-Origin: * on credentialed requests.

Where CORS stops

CORS only decides what browser JavaScript may read. curl, server-side code and mobile apps ignore it, so it is never an authorization layer: the API needs authentication either way. When the frontend and the API can share an origin, for example through a reverse-proxied /api path, there is nothing to configure at all.

When to Use CORS

You need CORS whenever browser code calls an API on a different origin, meaning a different scheme, host or port. Subdomains count: app.example.com calling api.example.com is cross-origin. Allow the exact origins you serve from an allowlist rather than reflecting whatever arrives, add Vary: Origin when the allowed origin changes per request, and keep authentication on the API regardless, because non-browser clients never look at CORS headers.

CORS in Practice

  • Express: allow one origin and let it send cookies: app.use(cors({ origin: 'https://app.example.com', credentials: true }));
  • Send cookies with a cross-origin fetch: fetch('https://api.example.com/me', { credentials: 'include' });
  • Reproduce a preflight from the terminal to see which headers the API returns: curl -i -X OPTIONS https://api.example.com/v1/orders \ -H 'Origin: https://app.example.com' \ -H 'Access-Control-Request-Method: PUT'

Common Mistakes

  • Sending Access-Control-Allow-Origin: * together with Access-Control-Allow-Credentials: true; browsers reject the combination.
  • Echoing back whatever Origin header arrives instead of checking it against an allowlist, which lets any site read authenticated responses.
  • Leaving out Vary: Origin when the allowed origin differs per request, so a CDN caches one site’s header and serves it to another.
  • Treating CORS as protection for the API: it only constrains browsers, so curl and server-to-server calls go straight through.

Related Terms

Keep the references coming

One email a week: new and trending developer tools, fresh comparisons, and what shipped. Unsubscribe in one click.