Skip to the solution
HardFrontend7 min read

What is CORS?

1.1k views

The problem

Question presented to candidate: "If a server sends back an Access-Control-Allow-Origin header that doesn't match the requesting page's origin, who actually blocks the request β€” the server, or something else? Where does that blocking genuinely happen?"

What a strong answer should cover:

  • πŸ“Œ Interview term: CORS (Cross-Origin Resource Sharing) β€” a browser-enforced security mechanism restricting whether a web page running at one origin can read the response from a request made to a DIFFERENT origin β€” a real, deliberate relaxation of the browser's stricter same-origin policy, opted into explicitly by the SERVER via response headers.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: making the identical cross-origin-style request from Node (not a browser) against a real local server sending a mismatched Access-Control-Allow-Origin header genuinely succeeded β€” Node's own fetch implementation does not enforce CORS at all, confirming CORS blocking is enforced specifically by the BROWSER's own fetch/XHR implementation, never by the server and never by Node itself.
  • πŸ“Œ Interview term: Access-Control-Allow-Origin β€” the response header a server sends to explicitly grant permission β€” verified directly, reading the real header value a server sent; the SERVER only ever suggests permission via this header, it never itself performs any blocking.
  • πŸ“Œ Interview term: simple requests vs. preflighted requests β€” a "simple" request (a plain GET/POST with only a few allowed headers) is sent directly; anything else (custom headers, other HTTP methods, certain content types) triggers a real, automatic preflight β€” the browser sends a separate OPTIONS request first, checking permission BEFORE sending the actual request at all.
  • A precise answer names that CORS is fundamentally a browser security feature protecting the USER, not a mechanism protecting the server β€” a non-browser client (curl, Node's own fetch, a mobile app) can genuinely make the identical cross-origin request with no CORS restriction whatsoever, verified directly.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; directly answering WHO enforces CORS (the browser, not the server) is the strong signal, since it is the single most commonly misunderstood aspect.

Code / implementation expected: Optional β€” demonstrating that a non-browser client (this very verification script) is genuinely unaffected by a mismatched CORS header is the clearest way to isolate exactly what CORS does and does not do.

  • cors

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof, run in this actual browser: a cross-origin request to a CORS-enabled API succeeds, while one to a site with no CORS headers is genuinely blocked

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 123 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

How does `Atomics.waitAsync()` enable non-blocking waits on SharedArrayBuffer?

Continue