Skip to the page

The browser's security model · core

CORS exactly: simple requests, preflight and credentials

CORS is a way for a SERVER to tell the browser which other origins may read its responses, using Access-Control-* headers; non-simple requests get an OPTIONS preflight first, and credentialed requests need an exact origin plus Access-Control-Allow-Credentials: true.

The notes

What you must know

  • Simple request (no preflight): method GET, HEAD or POST; only safelisted headers (Accept, Accept-Language, Content-Language, Content-Type, Range); Content-Type only application/x-www-form-urlencoded, multipart/form-data or text/plain.
  • Anything else (PUT/DELETE/PATCH, Authorization header, Content-Type: application/json, custom X- headers) triggers a preflight: OPTIONS with Origin, Access-Control-Request-Method and Access-Control-Request-Headers.
  • The server answers the preflight with Access-Control-Allow-Origin, -Allow-Methods, -Allow-Headers and optionally -Max-Age (default 5 s; browsers cap it). Only then is the real request sent.
  • Credentials: fetch does not send cookies cross-origin unless credentials: 'include'. The response must have Access-Control-Allow-Credentials: true and an EXACT origin; '*' is not allowed for origin, headers, methods or expose-headers.
  • When you echo an allow-listed origin, also send Vary: Origin so caches don't serve one origin's CORS headers to another.
  • Only safelisted response headers are readable by JS; others need Access-Control-Expose-Headers.
  • CORS is enforced by the browser on reading. It is not authentication and it does not protect the server: curl and Postman ignore it, and a simple request still reaches the server.

The red pen

Common traps: mistake → fix

  • Mistake: "CORS blocks the request from reaching the server"

    Fix: for simple requests the server receives and processes it; the browser only hides the response.

  • Mistake: "It works in Postman, so the API is fine"

    Fix: Postman is not a browser and does not enforce CORS.

  • Mistake: "Access-Control-Allow-Origin: * with credentials"

    Fix: the browser rejects it; credentialed responses need an exact origin.

  • Mistake: "CORS protects against CSRF"

    Fix: a simple cross-site form POST is sent without any CORS check.

  • Mistake: "Reflect any Origin header back"

    Fix: that allows every site, including attackers, to read authenticated responses.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is CORS and why does my fetch work in Postman but fail in the browser?
  2. What makes a request 'simple'? What triggers a preflight?
  3. How do you send cookies with a cross-origin fetch? What must the server return?
  4. Why is reflecting the Origin header dangerous? What about Vary: Origin?
  5. Your app on app.example.com calls api.example.com with a JSON body and a bearer token. List every header in the preflight and the response.

Practise

Practise with these questions

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview