CORS

Why a browser stops a page from freely reading another site's responses, and how that site's server can allow it.

What is it?

Browsers are deliberately wary of a page that fetches data from a different site than the one it came from. If a page at shopping.example requests bank.example, the browser blocks the page from reading the response by default, even if the request went through. There's no reason to assume bank.example wants another site reading its data on a visitor's behalf.

This default is the same-origin policy, a core browser security protection. But many legitimate cases need cross-site requests, like a public weather API that lots of sites call. So a server needs a way to say "this other site may read my responses."

That mechanism is CORS (Cross-Origin Resource Sharing). A server that allows other origins sends response headers saying so. The browser checks them before letting the page's JavaScript read the response. Without the right headers, the browser blocks access even though the server already sent the response.

Explain like I'm 10

It's like a package delivered to your door that you (the browser) won't let anyone inside open unless the sender included a note saying 'yes, this address may open it.'

Examples

A blocked cross-origin request

// Running on https://myapp.example
fetch("https://api.otherservice.example/data")
  .then(res => res.json())
  .catch(err => console.error(err));
// Console: blocked by CORS policy — no
// 'Access-Control-Allow-Origin' header present

The request may have reached the server and gotten a response, but the browser won't give the data to this page's JavaScript because the server didn't allow this origin.

A server opting in

HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://myapp.example
Content-Type: application/json

{"forecast": "sunny"}

This header names the exact origin allowed to read the response, telling the browser that https://myapp.example may access the data. A value of * would allow any origin.

How it works

When JavaScript on one origin requests another origin, the browser adds the page's origin to the request. It then checks the response headers before exposing the body to the page.

If the response has an Access-Control-Allow-Origin header matching the requesting origin (or a wildcard for any origin), the page can read it. Otherwise the browser blocks access, even though the network request may have completed.

For some kinds of requests, the browser first sends an automatic "preflight": a separate request asking the server what's allowed, before the real one.

Why does it exist?

The same-origin policy is essential for security but too strict for the many legitimate cases that share data across origins: public APIs, third-party widgets, services split across domains. CORS lets servers opt in to loosening it only where they choose, instead of browsers either blocking everything cross-origin or dropping the protection.

When to use it

You need CORS whenever a browser frontend on one origin calls an API on another, a common setup. You configure it on the server, since the server's headers grant permission.

When not to use it

CORS doesn't apply to server-to-server requests: no browser, so no same-origin policy. It also doesn't replace authentication or authorization. Allowing an origin only controls whether a browser can read the response, not whether the request should be trusted or permitted.

Common mistakes

  • Assuming a CORS error means the request never reached the server, when the server may have processed it fine. The browser only blocks reading the response.

  • Setting Access-Control-Allow-Origin to * on an API that also handles sensitive, authenticated requests, which can be too permissive.

  • Thinking CORS protects the server, when it's a browser-enforced protection for the calling page and its users.

Practice exercises

  1. Easy:

    In your own words, explain why the same-origin policy stops a page's JavaScript from reading a cross-origin response by default.

  2. Medium:

    A fetch request fails with a CORS error. Name the response header the server needs to add to fix it, with the specific allowed origin.

  3. Hard:

    Explain what a CORS preflight request is, why some requests need it and others don't, and sketch what the server must respond with to satisfy it.

Interview questions

What problem does the same-origin policy solve?

By default it stops a page's JavaScript from reading responses from another origin. That protects users from malicious pages silently reading data from other sites they're logged into.

What is CORS, and who configures it?

Cross-Origin Resource Sharing: the server sends response headers that let specific other origins read its responses. It's configured on the server, not the requesting page.

If a request fails due to CORS, did the server actually receive it?

Often yes. The request can reach the server and be processed normally; the browser just blocks the page's JavaScript from reading the response because the server didn't send the right permission header.