Full answer
The same-origin policy is the browser rule that a page may freely read data only from its own origin. An origin is the scheme, host and port together, so https://app.example.com and https://api.example.com are different origins, and so are http://localhost:3000 and http://localhost:4000.
CORS (Cross-Origin Resource Sharing) is the standard way for a server to relax that rule for specific origins. It is an opt-in. The server answers with Access-Control-Allow-Origin: https://app.example.com, and the browser then lets scripts from that origin read the response. Without the header, the response arrives at the browser but the script cannot read it, and fetch rejects with a network error.
Points that interviewers check:
- The browser enforces CORS. Tools such as
curland server-to-server calls ignore it, which is why an API can work incurland fail in a page. - CORS is about reading the response. For many requests the browser still sends the request, and the server still runs the action. The test below shows that the server handled a request whose response the page could not read. CORS is therefore not a defence against CSRF, and it does not authenticate anyone.
- The server decides. A front end developer cannot fix a missing header in client code. Setting
mode: 'no-cors'only gives an opaque response that cannot be read. Access-Control-Allow-Origin: *allows any origin to read public responses, but it cannot be used with credentials such as cookies.- When the allowed origin is chosen dynamically from a list, add
Vary: Origin, so caches do not give one origin's answer to another.
A short statement for an interview: "CORS is a way for a server to tell the browser which other origins may read its responses. The same-origin policy does the blocking, and CORS is the permission."
The Node server below allows only origins from a list. The test runs a page on one port and an API on another. First the origin is not in the list, so the page cannot read the response, but the server counts the request. Then the origin is added, and the page reads the data.
import http from 'node:http';
const allowed = new Set((process.env.ALLOWED ?? 'https://app.example.com').split(','));
let handled = 0;
http.createServer((req, res) => {
handled += 1;
res.setHeader('Vary', 'Origin');
const origin = req.headers.origin;
if (origin && allowed.has(origin)) res.setHeader('Access-Control-Allow-Origin', origin);
if (req.url === '/handled') return res.end(String(handled));
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ courses: ['HTML', 'CSS'] }));
}).listen(process.env.PORT, () => console.log('listening'));Run in headless Chrome 156 through Playwright. Two full runs gave the same results. The page and the API ran on different localhost ports. With the origin not allowed, the page could not read the response, and the server still counted the request. With the origin allowed, the page read the JSON.
Key points
- CORS is Cross-Origin Resource Sharing, a server opt-in to the browser's same-origin policy.
- An origin is scheme, host and port.
- The browser enforces it by hiding the response from the script. The request may still reach the server.
- It is not authentication and not a defence against CSRF.
- Allow specific origins, add Vary: Origin, and avoid * with credentials.
Common mistakes
- Saying CORS prevents unauthorized sites from calling the API. It only controls who can read the response in a browser.
- Trying to fix a CORS error with no-cors on the client.
- Reflecting any Origin header back as allowed, which grants access to every site.
- Forgetting that curl and servers ignore CORS.
Follow-ups
- Why can a CORS-blocked request still change data on the server?
- When does the browser send a preflight request?
- Why is the wildcard not allowed with cookies?
Tips
- Name the same-origin policy first, since CORS is its exception.
- Say who enforces it and what it does not protect.