CORS, or cross-origin resource sharing, is an HTTP-header-based mechanism that lets a server tell the browser which other origins may read its responses. Among browser technologies on the Web, it is the controlled exception to the same-origin policy, which by default stops a script on one origin from reading data returned by another.
CORS is defined in the WHATWG Fetch Standard and is enforced entirely by the browser. The server only declares its policy in response headers; the browser decides whether the calling page receives the response. This division of labor explains most of the confusion around CORS errors.
How CORS relaxes the same-origin policy
CORS works by adding an origin check on the response side. An origin is the combination of scheme, host and port, so app.example.com and api.example.com served over HTTPS are different origins, and so are http and https versions of the same host. When a script calls fetch() or XMLHttpRequest toward another origin, the browser attaches an Origin request header that names the calling page.
The server answers with Access-Control-Allow-Origin. If the value matches the calling origin or is the wildcard *, the browser hands the response to the script. If the header is missing or does not match, the request may still have reached the server, but the browser withholds the response body and reports a CORS error in the console. CORS therefore protects the reading of data, not the sending of requests.
The table below shows how the browser classifies typical request pairs before any CORS logic applies.
| Page (scheme, host, port) | Target (scheme, host, port) | Classification |
|---|---|---|
| HTTPS, shop.example.com, 443 | HTTPS, shop.example.com, 443 | Same origin |
| HTTPS, shop.example.com, 443 | HTTPS, api.example.com, 443 | Cross origin (different host) |
| HTTP, shop.example.com, 80 | HTTPS, shop.example.com, 443 | Cross origin (different scheme) |
| HTTPS, shop.example.com, 443 | HTTPS, shop.example.com, 8443 | Cross origin (different port) |
Simple requests and preflight requests
The Fetch Standard divides cross-origin requests into simple requests and requests that need a preflight. A simple request uses GET, HEAD or POST, carries only CORS-safelisted headers, and, for POST, uses one of three content types: text/plain, multipart/form-data or application/x-www-form-urlencoded. These requests are sent directly, because ordinary HTML forms could already send them before CORS existed.
Any other request triggers an OPTIONS preflight. The browser first asks the server, through Access-Control-Request-Method and Access-Control-Request-Headers, whether the real request is acceptable. Only if the server approves the method and headers does the browser send the actual request. A JSON body with Content-Type: application/json or a custom Authorization header is enough to require a preflight.
The following table summarizes the main CORS response headers and their roles.
| Header | Purpose | Typical value |
|---|---|---|
| Access-Control-Allow-Origin | Names the origin allowed to read the response | The exact calling origin |
| Access-Control-Allow-Methods | Lists methods approved in a preflight | GET, POST, PUT, DELETE |
| Access-Control-Allow-Headers | Lists request headers approved in a preflight | Content-Type, Authorization |
| Access-Control-Allow-Credentials | Permits cookies and HTTP authentication | true |
| Access-Control-Expose-Headers | Makes extra response headers readable to scripts | X-Request-Id |
| Access-Control-Max-Age | Caches the preflight result in seconds | 600 to 7200 |
Browsers cap the preflight cache duration regardless of the value sent: Chromium limits it to two hours, and Firefox to one day. Preflights add one extra round trip, so a sensible Access-Control-Max-Age noticeably reduces latency for chatty single-page applications.
Credentials and the wildcard restriction
By default, cross-origin fetch() calls do not send cookies. A script must opt in with credentials: "include", and the server must answer with Access-Control-Allow-Credentials: true. In that case the specification forbids the wildcard: Access-Control-Allow-Origin must name the exact origin, and the allowed methods and headers must also be listed explicitly.
This restriction exists because a wildcard combined with cookies would let any website read authenticated data on behalf of a logged-in user. Servers that support several trusted front ends usually keep an allowlist of origins and echo the matching one back. When they do, they must add Vary: Origin so that shared caches and the browser cache do not serve a response tailored to one origin to another.
How CORS compares with related browser protections
CORS is often confused with neighboring security mechanisms, but each answers a different question. The comparison below separates them.
| Mechanism | Question it answers | Who sets it |
|---|---|---|
| Same-origin policy | May a script read another origin by default | Browser, built in |
| CORS | Which other origins may read this response | Server, via headers |
| Content Security Policy | Which sources may this page load or contact | Page owner, via header |
| CSRF tokens | Did this state-changing request come from my site | Application logic |
| Cross-Origin Resource Policy | May other origins embed this resource at all | Server, via header |
The most important distinction is with CSRF protection. Because simple requests are delivered before any CORS check, CORS does not prevent cross-site request forgery. An attacker page can still submit a form-encoded POST to a vulnerable endpoint; it simply cannot read the answer. State-changing endpoints therefore still need SameSite cookies, anti-CSRF tokens or both.
Configuring CORS on a server
Good CORS configuration starts from the principle of least privilege: allow only the origins, methods and headers that real clients need. The decision table below covers the most common situations.
| Situation | Recommended setting | Reason |
|---|---|---|
| Public, read-only open data API | Wildcard origin, no credentials | Data is not user specific |
| Own front end on a separate subdomain | Exact origin, credentials allowed | Session cookies must flow |
| Several partner front ends | Origin allowlist with Vary: Origin | Each response names one origin |
| Internal service called only by servers | No CORS headers at all | Browsers never call it |
| Local development on localhost | Separate development allowlist | Keeps production policy strict |
The preflight must be answered before authentication, because browsers never attach credentials to the OPTIONS request. A frequent production bug is an authentication layer that rejects the preflight with 401 or 403, which the browser reports as a generic CORS failure even though the headers are configured correctly elsewhere.
Common CORS mistakes and misconceptions
Most CORS problems come from a small set of recurring misunderstandings. The table links each symptom to its usual cause.
| Symptom | Likely cause | Fix |
|---|---|---|
| No Access-Control-Allow-Origin header | Server or proxy omits the header | Add the header at the right layer |
| Wildcard rejected with credentials | Using * together with cookies | Echo the exact allowed origin |
| Preflight fails with 401 | Authentication runs on OPTIONS | Exempt preflight from authentication |
| Custom header unreadable in script | Header not exposed | List it in Access-Control-Expose-Headers |
| Random failures behind a CDN | Missing Vary: Origin | Add Vary: Origin to responses |
- Reflecting any Origin value with credentials enabled is equivalent to having no protection and is a well-known security vulnerability.
- Allowing the null origin is risky, because sandboxed iframes and local files can produce it.
- Disabling CORS in the browser or with an extension only hides the problem on one machine; real users still see the error.
- CORS does not apply to server-to-server calls or tools such as curl, which ignore browser policy entirely.
The idea in brief
A web page is normally allowed to read data only from the website it came from. This rule, the same-origin policy, keeps a random site from quietly reading your email or bank account in another tab.
CORS is the polite permission note a server can send back. It says, in effect, that a named website may read this reply. The browser acts as the gatekeeper: it checks the note and either passes the data to the page or blocks it. The key point is that the server grants permission and the browser enforces it.
A practical scenario
A retail company moves its storefront to the shop.example.com host while its product API stays on api.example.com. After deployment, the cart stops working and the console shows a CORS error on every POST that sends JSON with a session cookie.
The backend team finds two faults. The API returns Access-Control-Allow-Origin: *, which browsers reject when credentials are included, and an authentication middleware answers the OPTIONS preflight with 401. They replace the wildcard with an allowlist echoing the storefront origin, add Access-Control-Allow-Credentials: true and Vary: Origin, exempt preflights from authentication, and set Access-Control-Max-Age to 3600 seconds.
The cart works again within the same release, and because preflights are now cached, the number of OPTIONS requests drops sharply, cutting one round trip from most repeat API calls during a shopping session.