{"canonical_url":"https://engineer.company/glossary/cors/","content_markdown":"# CORS\n\n## What it is\n\nCORS, written out as Cross-Origin Resource Sharing, is the set of rules by\nwhich a server tells a browser that pages from other websites may read what it\nsends back. Browsers keep origins apart. An origin is a scheme, a host and a\nport taken together, so `https://app.example.com`, `https://api.example.com`\nand `http://app.example.com` are three different origins, and script running on\none of them may not read a response from another unless that other one agrees.\nCORS is how it agrees: a small family of HTTP headers, nearly all of them\nstarting `Access-Control-`, that the server adds to its responses and the\nbrowser checks before handing anything to the calling script.\n\nThe protocol lives in the WHATWG Fetch Standard, the living specification of how\nbrowsers fetch resources. Its reasoning is stated plainly there. A page should\nbe able to use data from another origin, and to send requests richer than an\nHTML form can, but only when the other side opts in. Without the opt-in, a page\nfrom the open web could read servers that sit behind a company firewall, or read\na user's private data on another site with that user's cookies attached.\n\nThe core exchange is short. When script asks for a resource on another origin,\nthe browser adds an `Origin` header naming the page's own origin. The server\nanswers with `Access-Control-Allow-Origin`, whose value is either that origin,\nwritten exactly, or `*` for anybody. If it matches, the script receives the\nresponse. If it does not, the script receives a network error and nothing of\nthe body.\n\nSome requests need permission before they are sent at all. A `GET`, `HEAD` or\n`POST` with only a few ordinary headers and a body of type\n`application/x-www-form-urlencoded`, `multipart/form-data` or `text/plain` goes\nstraight out, because an HTML form could send the same thing from any site\nalready. Anything beyond that, such as a `PUT`, a `DELETE`, a JSON body or an\n`Authorization` header, is preceded by a preflight: an `OPTIONS` request that\nnames the method in `Access-Control-Request-Method` and any extra headers in\n`Access-Control-Request-Headers`. The real request follows only if the answer\nhas an ok status, such as 200 or 204, and allows both.\n\n## Why it matters\n\nAlmost every web application with a separate API meets CORS on the first day.\nThe frontend is served from one origin and the API from another, and the first\n`fetch` from one to the other fails in the browser console until the API sends\nthe right headers. Teams that meet it this way tend to remember it as an\nobstacle and to configure it as one, by allowing whatever makes the error go\naway. That is where the risk lives.\n\nThe headers decide who may read your users' data through their browsers. A user\nwho is logged in to your application carries a session cookie. If your API\nallows any origin to make credentialed requests and read the answers, then any\npage that user visits can call your API as them and read the result. The Fetch\nStandard is blunt about sharing credentialed responses: \"extreme care has to be\ntaken to avoid the confused deputy problem\". The browser, holding the user's\ncredentials, is the deputy, and a careless CORS policy lets a stranger's page\ndirect it.\n\nIt is just as important to know what CORS does not do. It does not protect the\nserver. A simple request reaches the server and runs whether or not the\nresponse is later shared, so a `POST` that changes data has changed it before\nthe browser looks at a single CORS header. Outside a browser the `Origin` header\nis whatever the caller types, and the OWASP testing guide warns that it may be\nspoofed. Who may call an endpoint and what they may do there is decided by\nauthentication and authorisation on the server, every time. CORS only decides\nwhich web pages may read the answer inside a browser.\n\n## What it looks like in practice\n\nSuppose a single-page application at `https://app.example.com` saves a note\nthrough an API at `https://api.example.com`, using the session cookie the API\nset at login:\n\n```js\nawait fetch(\"https://api.example.com/v1/notes/42\", {\n  method: \"PUT\",\n  credentials: \"include\",\n  headers: { \"Content-Type\": \"application/json\" },\n  body: JSON.stringify({ text: \"Ship on Friday\" }),\n});\n```\n\nA `PUT` with a JSON body is more than a form could send, so the browser asks\nfirst. The preflight carries no cookies; it only names what the real request\nwill use:\n\n```http\nOPTIONS /v1/notes/42 HTTP/1.1\nHost: api.example.com\nOrigin: https://app.example.com\nAccept: */*\nAccess-Control-Request-Method: PUT\nAccess-Control-Request-Headers: content-type\n```\n\nThe API checks the origin against its own list and answers:\n\n```http\nHTTP/1.1 204 No Content\nAccess-Control-Allow-Origin: https://app.example.com\nAccess-Control-Allow-Credentials: true\nAccess-Control-Allow-Methods: PUT\nAccess-Control-Allow-Headers: content-type\nAccess-Control-Max-Age: 600\nVary: Origin\n```\n\nThe browser now sends the `PUT` with the cookie and an `Origin` header, and the\nAPI's response repeats `Access-Control-Allow-Origin` and\n`Access-Control-Allow-Credentials: true`. Without both, the note is saved on\nthe server but the script never learns it. `Access-Control-Max-Age` lets the\nbrowser reuse the preflight answer for up to 600 seconds instead of asking\nbefore every call; when the header is missing, the standard keeps it for five\nseconds. The `Vary: Origin` header matters because the answer depends on who\nasked, and a cache that ignored that could hand one origin's answer to another.\n\nOn a platform holding professional and organisational data, the API layer runs\n[rate limiting, CORS, request-size limits and input validation](/portfolio/hardened-the-application-with-nonce-based-csp/)\nbefore anything reaches the database.\nThe same page explains why CORS is one entry in that list and not the whole\ndefence: the platform assumes the client is hostile, and the server checks each\nrequest's entitlements again whatever the browser has allowed.\n\n## Where it goes wrong\n\n**The server copies any `Origin` back into `Access-Control-Allow-Origin`.** It\nis the quickest way to make the console error disappear, and with\n`Access-Control-Allow-Credentials: true` it hands every website on the internet\nthe user's view of the API. Keep an explicit list of the origins that should\nhave access, compare the incoming `Origin` against it exactly, and send the\nheaders only on a match.\n\n**The list is checked with a prefix or a substring.** A rule that accepts any\norigin starting with `https://app.example.com` also accepts\n`https://app.example.com.attacker.example`, and a substring match is worse.\nCompare whole serialised origins. Note that a serialised origin has no path and\nno trailing slash, so `https://app.example.com/` in the list will never match\nanything.\n\n**`*` is expected to work with credentials.** When a request includes\ncredentials, the browser refuses `*` in `Access-Control-Allow-Origin`,\n`Access-Control-Allow-Methods`, `Access-Control-Allow-Headers` and\n`Access-Control-Expose-Headers`, and `Access-Control-Allow-Credentials` must be\nexactly `true`, in lower case. A public, read-only API with no cookies can\nsafely answer `*`; an API that relies on a session has to name the origin.\n\n**`null` is on the allowed list.** Pages loaded from a `data:` URL get an\nopaque origin, and a request that has been redirected across origins also\nreports `Origin: null`, so many unrelated documents present the same value.\nAllowing `null` allows all of them. Leave it off.\n\n**`Vary: Origin` is missing.** If the API sends `Access-Control-Allow-Origin`\nonly when a request carries a matching `Origin`, a cache can store an answer\nwithout the header and serve it to a later CORS request, or store one origin's\nanswer and serve it to another. The Fetch Standard asks for `Vary: Origin`\nwhenever the answer depends on the origin. If the value is `*` or one fixed\norigin, send it on every response and leave `Vary` out.\n\n**`Access-Control-Allow-Headers: *` is trusted to cover everything.** The\nwildcard never covers `Authorization`, which has to be listed by name, and it\ncovers nothing at all for credentialed requests. Name the headers the client\nreally sends.\n\n**CORS is treated as access control.** A server that refuses requests because\nthe `Origin` looks wrong, and accepts them otherwise, has written a check that\nany script outside a browser walks past. Authenticate the caller, authorise the\naction on the server, and let CORS do its one job, which is deciding what a\nbrowser shows to a page.\n\n## Further reading\n\n- [Fetch Standard: the CORS protocol](https://fetch.spec.whatwg.org/#http-cors-protocol)\n  — the definition: the request and response headers, the preflight, the\n  credentials table and the advice on `Vary`.\n- [RFC 6454: The Web Origin Concept](https://www.rfc-editor.org/rfc/rfc6454) —\n  what an origin is, how scheme, host and port decide it, and how it is\n  serialised into the `Origin` header.\n- [OWASP Web Security Testing Guide: Testing Cross Origin Resource Sharing](https://wstg.owasp.org/v4.2/4-Web_Application_Security_Testing/11-Client-side_Testing/07-Testing_Cross_Origin_Resource_Sharing/)\n  — how testers look for a wildcard origin or an origin echoed back without\n  checks.\n\n\u003chttps://engineer.company/glossary/cors/\u003e\n","date_modified":"2026-10-29T11:17:41Z","description":"How CORS decides which other sites may read a server's responses, what a preflight request asks, and the header mistakes that leak data.","formats":{"gemtext":"https://engineer.company/glossary/cors/index.gmi","gopher":"https://engineer.company/glossary/cors/gophermap","html":"https://engineer.company/glossary/cors/","json":"https://engineer.company/glossary/cors/index.json","markdown":"https://engineer.company/glossary/cors/index.md","pdf":"https://engineer.company/glossary/cors/index.pdf"},"kind":"page","language":"en","title":"What is CORS?","url":"https://engineer.company/glossary/cors/","version":1}