What this page is for
Intent: the full picture behind the quick CORS page. Same sections, same order, with more detail in each: raw HTTP, exact rules, server config, debugging and the harder interview questions.
Read the quick page first. Each section here starts where the quick one stops.
Time: about 20 minutes.
Words you need first
Everything from the quick page (origin, same-origin policy, CORS, header, fetch(), cookie), plus:
- Scheme, host, port: the three parts of an origin. In
https://api.shop.com:8443/cart, the scheme ishttps, the host isapi.shop.com, the port is8443. The path (/cart) is not part of the origin. - Site: a looser idea than origin. Roughly the scheme plus the main domain (
shop.com).api.shop.comandwww.shop.comare the same site but different origins. Cookies’SameSiterule uses site. CORS uses origin. - HTTP method: the verb of a request:
GET(read),POST(create or send),PUT/PATCH(update),DELETE, andOPTIONS(ask what’s allowed). - Simple request: a cross-origin request the browser sends directly, without a preflight (exact rules under Preflight).
- Safelisted header: a header the browser considers harmless, so it doesn’t trigger a preflight (request side) or is always readable by the page (response side).
- Opaque response: a reply the page receives but can’t look inside (no status, no headers, no body). You get one from
mode: "no-cors". - Middleware: code on the server that runs on every request before your route handler, e.g. a CORS middleware that adds the headers.
Is it the same origin as https://shop.com?
| URL | Same origin? | Why |
|---|---|---|
https://shop.com/cart | Yes | path doesn’t count |
http://shop.com | No | scheme differs |
https://api.shop.com | No | host differs (subdomain) |
https://shop.com:8443 | No | port differs (default is 443) |
Why it exists
The full story behind the quick version:
- You log in to
bank.com. It sets a session cookie, and your browser sends that cookie on requests tobank.com. If the cookie is set withSameSite=None, it’s sent even when another site’s page started the request. - You visit
evil.com. Its script runsfetch("https://bank.com/api/accounts", { credentials: "include" }). - The browser attaches your bank cookie. The bank sees a valid login and returns your accounts.
- Without the same-origin policy, the script would read that reply and send it to the attacker.
The same-origin policy blocks step 4. But real apps often need cross-origin reads (front end on shop.com, API on api.shop.com), so CORS lets a server list exactly which origins may read its replies.
Note what the policy does not block: loading images, scripts and stylesheets from other origins (<img src>, <script src>), or submitting forms to other origins. Those were allowed before CORS existed, and that’s why CSRF is still possible (see CORS is not CSRF protection).
The picture
The same four steps as the quick page. The rest of this page zooms into each box: the raw headers in step 2 and 3, the preflight that can come before step 1, and what the browser does at the check.
How it works, with an example
Simply: Picture a shop asking a warehouse for items. The browser adds an Origin label, and the warehouse must send that origin back in an Access-Control-Allow-Origin header. This is CORS (Cross-Origin Resource Sharing). The page may read the reply only if the origin matches.
What to know: The same shop.com → api.com example, as the raw HTTP on the wire.
Request (the browser adds Origin, your code can’t change it):
GET /cart HTTP/1.1
Host: api.com
Origin: https://shop.comReply that allows the page to read it:
HTTP/1.1 200 OK
Content-Type: application/json
Access-Control-Allow-Origin: https://shop.com
Vary: Origin
{"items": [42]}What the browser checks: does Access-Control-Allow-Origin exactly equal the page’s origin, or is it * (and the request had no credentials)? Only one value is allowed. A list like https://a.com, https://b.com is invalid. A server that allows several origins checks the incoming Origin against its allowlist and echoes back the one that matched.
Vary: Origin: if the reply’s Allow-Origin depends on who asked, add Vary: Origin. It tells caches, such as a CDN (a network of servers that store copies of replies close to users), to keep a separate copy per origin. Otherwise a cache might serve shop.com’s header to other.com, and their page breaks.
Reading response headers: by default the page can only read a few safelisted reply headers (Cache-Control, Content-Language, Content-Length, Content-Type, Expires, Last-Modified, Pragma). To let it read others, like X-Total-Count for pagination, the server lists them: Access-Control-Expose-Headers: X-Total-Count.
The most common mistake
CORS blocks reading, not sending
Everything from the quick page holds. Three consequences interviewers like:
- Side effects still happen. A blocked cross-origin
POSTthat’s a simple request still ran on the server. If it deleted something, it’s deleted. Only the page couldn’t read the answer. - Errors can look like CORS errors. If your server crashes and returns a 500 without the CORS headers (many frameworks skip middleware on errors), the browser reports “blocked by CORS policy”, not the 500. Check the Network tab for the real status.
- CORS is not access control for your API. Anyone can call your API from curl or a script on their server. Protect data with login checks (authentication) and permission checks (authorization), never with CORS.
Preflight
Simply: Some requests are not simple, such as a PUT or a JSON body. The browser first sends a small OPTIONS request to ask what is allowed. This check is the preflight, like calling a shop ahead. The real request goes out only after a yes.
What to know:
Exact simple-request rules. A request skips the preflight only if all are true:
- the method is
GET,HEADorPOST; - it only sets safelisted headers:
Accept,Accept-Language,Content-Language,Content-Type(with limits below), andRange(simple values); Content-Type, if set, isapplication/x-www-form-urlencoded,multipart/form-dataortext/plain;- no upload progress listener and no streaming body.
So Content-Type: application/json and Authorization: Bearer ... both trigger a preflight. Most real API calls are preflighted.
The preflight headers:
| Header | Sent by | Meaning |
|---|---|---|
Access-Control-Request-Method | browser | the method the real request will use |
Access-Control-Request-Headers | browser | the non-safelisted headers it will send |
Access-Control-Allow-Methods | server | methods allowed |
Access-Control-Allow-Headers | server | headers allowed |
Access-Control-Max-Age | server | seconds the browser may cache this answer and skip the preflight (browsers cap it, e.g. Chrome at 2 hours) |
Preflight gotchas:
- The preflight carries no cookies and no
Authorizationheader. If your auth middleware runs before your CORS middleware, it answers theOPTIONSwith 401 and the preflight fails. Put CORS first. - The server must answer
OPTIONSwith a 2xx status (usually 204). A 404 or 405 forOPTIONSfails the preflight. - The real reply still needs
Access-Control-Allow-Origin. Passing the preflight isn’t enough.
Cookies across sites
Simply: Credentials means cookies and other login data. A membership card stays at home by default. To send it, the page must ask, and the server must name one exact origin and allow credentials.
What to know: Everything from the quick page, plus:
- With credentials, no wildcards anywhere.
Allow-Origin,Allow-Headers,Allow-MethodsandExpose-Headersmust all be explicit lists. A*is treated as the literal text ”*”. - The cookie must also be allowed to travel. For a cross-site request, the cookie needs
SameSite=None; Secure. Chrome treats cookies with noSameSitesetting asLax, which doesn’t send them on cross-sitefetch. Some browsers also block third-party cookies entirely. So “I set Allow-Credentials and the cookie still isn’t sent” is usually aSameSiteproblem, not a CORS one. - Same site, different origin is easier.
shop.comcallingapi.shop.comis cross-origin (CORS applies) but same-site (SameSite=Laxcookies are sent). This is why companies put the API on a subdomain. - Tokens instead of cookies. If the front end sends an
Authorization: Bearer <token>header instead of cookies,credentials: "include"isn’t needed, but every request is preflighted (custom header).
How to fix a CORS error
Simply: Think of a guest list at a door. Fix it on the server, which must name the front end’s origin. In development, a proxy passes calls on to the real API.
What to know:
Read the console message first. It names the missing piece, e.g. “No ‘Access-Control-Allow-Origin’ header is present” or “Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response”.
Then check the Network tab: is there an OPTIONS row? What’s its status? Which Access-Control-* headers came back on each row?
Server config examples:
// Express (Node.js)
import cors from "cors";
app.use(cors({
origin: ["https://shop.com", "http://localhost:5173"],
credentials: true,
}));# FastAPI (Python)
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=["https://shop.com", "http://localhost:5173"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)Dev proxy (Vite): the page calls /api/cart on its own origin, and the dev server forwards it, so the browser never sees a cross-origin request.
// vite.config.js
export default { server: { proxy: { "/api": "http://localhost:8000" } } };Don’t do these:
- Reflect any origin. Copying the request’s
Originstraight intoAllow-OriginwithAllow-Credentials: truemeans every site can read your users’ data. That’s a real security bug. Always check against an allowlist. - Allow
null. Sandboxed pages and local files sendOrigin: null, and attackers can produce it too. - Use
mode: "no-cors". The request goes out, but you get an opaque reply: no status, no headers, no body. - Browser extensions or disabling web security. They only change your browser. Your users still get the error.
CORS is not CSRF protection
Simply: CSRF (Cross-Site Request Forgery) is when another site makes your browser send a request you did not mean to send, with your cookies attached. Think of a forged check: the bank pays it, even though the forger never sees the bank’s reply.
What to know:
Why CORS can’t stop it: a CSRF attack doesn’t need to read the reply. A hidden form on evil.com that POSTs to bank.com/transfer is a simple request, so there’s no preflight. The browser sends it with your cookie (if SameSite allows), and the bank moves the money. CORS only hides the reply, which the attacker didn’t need.
What does stop it:
SameSitecookies:Lax(Chrome’s default) stops cookies on cross-site POSTs.Strictstops them on all cross-site requests, even link clicks.- CSRF tokens: the server puts a random secret in its own forms and checks it on submit.
evil.comcan’t read your page (same-origin policy), so it can’t learn the token. - Checking the
Originheader on state-changing requests. - Requiring JSON or a custom header forces a preflight, which a strict CORS setup will refuse. It helps, but don’t rely on it alone.
SOP vs CORS vs CSP, in detail:
| SOP | CORS | CSP | |
|---|---|---|---|
| Who sets it | built into the browser | the API server, per reply | the site, on its own pages |
| What it controls | which replies a page may read | which other origins may read my replies | where my page may load scripts, images and data from |
| Protects against | sites reading your data | (it loosens SOP, carefully) | XSS: injected scripts running in your page |
Say it in an interview
Start with the 60-second answer from the quick page. If they push deeper, add one of these:
- Internals: “Only one
Allow-Originvalue is allowed, so for several front ends the server checksOriginagainst an allowlist and echoes the match, withVary: Originso caches don’t mix them up.” - Preflight details: “The preflight sends
Access-Control-Request-Methodand-Headers. It carries no credentials, so CORS middleware must run before auth.Max-Agecaches the answer.” - Security: “CORS isn’t access control and isn’t CSRF protection. Reflecting arbitrary origins with credentials is a vulnerability.”
Questions interviewers ask
The ten from the quick page, plus these harder ones. Tap to reveal.
https://shop.comcallshttps://api.shop.com. Is CORS involved?Yes. Different host means different origin. But it’s the same site, so
SameSite=Laxcookies are still sent.
Your API works in Postman but fails in the browser. Why?
Postman isn’t a browser and doesn’t enforce the same-origin policy. The server is missing CORS headers for the page’s origin, or failing the preflight.
The preflight returns 401. Why?
The preflight carries no cookies or
Authorizationheader, and auth middleware ran before CORS middleware. LetOPTIONSthrough, or put CORS first.
How do you allow two front-end origins?
Keep an allowlist, compare the request’s
Origin, echo back the one that matches, and addVary: Origin. You can’t list two values in the header.
Why is reflecting the Origin header dangerous?
With
Allow-Credentials: true, it lets any website read logged-in users’ data. It turns CORS off while looking like it’s configured.
The page can't read the
X-Total-Countheader. Why?Only safelisted response headers are readable cross-origin. The server must send
Access-Control-Expose-Headers: X-Total-Count.
What does
mode: "no-cors"do?Sends the request but gives the page an opaque reply it can’t read. It doesn’t “fix” CORS.
The server crashed with a 500, but the console says CORS error. Why?
The error reply didn’t include CORS headers, so the browser hid it and reported a CORS failure. Check the Network tab for the real status.
What does Access-Control-Max-Age do?
It lets the browser cache the preflight answer for that many seconds, so repeated requests skip the extra
OPTIONSround trip.
How is CORS different from authentication?
CORS decides which pages may read replies in a browser. Authentication decides which users may get data at all. Any non-browser client bypasses CORS, so you always need both.
Mental map
The same map as the quick page. Each branch is a section above.
Watch
Web Dev Simplified, about 6 minutes: what CORS is and how it relates to the same-origin policy.