Cross-Origin Resource Sharing (CORS)
A browser-controlled HTTP mechanism through which a server declares which origins may read selected cross-origin responses.
Download Markdown (.md)Soroush’s Product Wiki · https://wiki.soroush.uk/topic/cross-origin-resource-sharing/
01What it means
Cross-Origin Resource Sharing (CORS) extends browser fetch behavior with response headers that permit specified origins, methods, headers, and credential modes. For non-simple requests, the browser may send a preflight request first. CORS relaxes the same-origin policy; it is not server-side authentication or authorization.[1][2]
- 01Cross-origin fetchBrowser detects CORS
- 02Preflight when requiredOPTIONS with origin and method
- 03Server policyExplicit allow headers
- 04Actual requestSent or blocked
- 05Response exposureBrowser checks headers
A qualifying request may be preflighted. CORS relaxes browser same-origin rules; it is not server authentication or authorization.
02Why it matters
CORS enables web frontends and APIs on different origins to integrate without giving every site permission to read responses. Misconfiguration can expose credentialed data, while misunderstanding it causes needless proxies and broken clients.
03When to use it
Configure CORS when browser JavaScript on an allowed origin must read a cross-origin response. It is irrelevant to non-browser clients and should not be used as the only protection for an API.
04How it works
- A script makes a cross-origin fetch and the browser determines whether CORS applies.
- For qualifying requests, the browser sends an OPTIONS preflight describing the origin, method, and headers.
- The server evaluates policy and returns explicit allow headers when appropriate.
- The browser sends the actual request or blocks it based on the preflight result.
- The browser exposes the response to the script only when response headers satisfy CORS rules.
05A practical example
A dashboard at app.example requests api.example with credentials. The API returns one exact allowed origin and permits credentials; it does not reflect arbitrary Origin values or use a wildcard.
06Where it breaks
CORS is enforced by browsers, so scripts, servers, and command-line clients can still send requests. It does not prevent CSRF by itself, validate users, or stop an API from returning data to an unauthorized caller.
Common mistakes
- Using Access-Control-Allow-Origin: * with sensitive assumptions or credentialed access.
- Reflecting any Origin header without validating an allowlist.
- Trying to fix CORS entirely in frontend code.
- Treating a CORS error as proof the server never received the request.
07Sources
- Fetch StandardWHATWG · Living web standardFetch Standard
- Cross-Origin Resource Sharing (CORS)MDN Web Docs · Official technical documentationCross-Origin Resource Sharing (CORS)