CORS (Cross-Origin Resource Sharing) is an HTTP-header mechanism that lets a server tell browsers which other websites are allowed to read its responses. Browsers block cross-origin reads by default under the same-origin policy; CORS is the controlled exception. The server sends headers such as Access-Control-Allow-Origin, and the browser either shares the response with the requesting page or blocks it.
Key takeaways
- CORS is enforced by the browser, not the server — curl and server-to-server calls ignore CORS headers entirely.
- A wildcard Access-Control-Allow-Origin: * is fine for public data, but it must never be combined with credentials such as cookies.
- Requests that change data or use custom headers trigger a preflight OPTIONS request before the real request is sent.
You build a front end on one domain and an API on another, wire up a fetch call, and the browser refuses to show you the data — even though the API answered fine. That refusal is CORS doing its job. Understanding it takes about ten minutes, and it saves hours of guessing at error messages.
What is CORS?
CORS (Cross-Origin Resource Sharing) is an HTTP-header mechanism that lets a server tell browsers which other websites are allowed to read its responses. It is the controlled exception to the browser's same-origin policy, which otherwise blocks JavaScript on one site from reading data from a different site.
An "origin" is the combination of scheme, host and port. https://example.com, http://example.com and https://api.example.com are three different origins, even though they look related. Change any one of the three parts and you have crossed an origin boundary.
CORS matters because modern web apps are split across origins by design: a single-page app on one domain, an API on another, fonts and media on a CDN. Without CORS, none of those pieces could share data with each other through the browser.
What is the same-origin policy?
The same-origin policy is the browser's default rule: JavaScript running on one origin may not read responses from another origin. It exists so that a malicious page cannot silently read your email, your bank balance or your company's internal tools just because you happen to be logged in to them in another tab.
The policy only restricts reading responses. A page can still embed an image from another site, load a script from a CDN, or submit a form to a different domain — it just cannot read the result back into JavaScript. That distinction is why CORS exists: some cross-origin reads are legitimate, and the server that owns the data should be able to allow them.
Note that the same-origin policy is a browser rule, not a server rule. It protects users browsing the web. It does not protect your API from direct calls, because anything that is not a browser simply ignores it.
How does CORS work?
CORS works through a short exchange of HTTP headers between the browser and the server. The browser sends an Origin header with every cross-origin request, naming the site that made it. The server answers with Access-Control-Allow-Origin, naming which origins may read the response.
For a simple request, the flow is short. The browser sends the request with its Origin header, the server responds with the data plus Access-Control-Allow-Origin, and the browser checks that header before handing the data to JavaScript.
If the header names your origin, or is the wildcard *, the data is shared. If not, the browser blocks it — and, for security reasons, JavaScript is only told that an error occurred, not why.
The key headers are:
- Origin — sent by the browser; the scheme, host and port of the requesting page.
- Access-Control-Allow-Origin — sent by the server; the origin(s) allowed to read the response, or
*for any origin. - Access-Control-Allow-Credentials — whether the browser may send cookies and HTTP authentication with the request.
- Access-Control-Allow-Methods — which HTTP methods the server accepts for cross-origin requests.
- Access-Control-Allow-Headers — which request headers the server accepts.
- Access-Control-Max-Age — how long the browser may cache a preflight result, in seconds.
What is a preflight request?
A preflight request is an OPTIONS request the browser sends before the real request, to ask the server whether the real request is acceptable. It happens when the request could change data on the server or uses features a plain HTML form could not, such as a PUT or DELETE method, a JSON Content-Type, or a custom header.
The preflight carries two extra headers: Access-Control-Request-Method, naming the method the real request will use, and Access-Control-Request-Headers, naming the headers it will carry. The server answers with the methods and headers it allows, and only then does the browser send the actual request.
Requests that do not need a preflight are the ones that look like a classic HTML form submission: GET, HEAD or POST, with only safelisted headers such as Accept and Content-Language, and a Content-Type of application/x-www-form-urlencoded, multipart/form-data or text/plain.
The reasoning, documented by MDN's CORS guide, is that a form could already submit such a request to any origin, so the server must already defend against cross-site request forgery — the preflight adds nothing. The server must still opt in with Access-Control-Allow-Origin before the browser shares the response.
Preflight results can be cached: Access-Control-Max-Age tells the browser how many seconds it may reuse the answer. MDN notes the default is 5 seconds, and each browser also enforces its own maximum, so a huge value will be capped.
CORS vs CSRF: what is the difference?
CORS and CSRF (cross-site request forgery) are related but solve different problems. CORS controls whether a browser lets a page read a cross-origin response. CSRF is an attack in which a malicious site tricks a browser into sending a request — for example, submitting a form that changes your password — while you are logged in somewhere else.
CORS does not stop CSRF. A cross-origin request still reaches your server; CORS only decides whether the response is readable. An attacker does not need to read the response to change your data. That is why servers need anti-CSRF tokens, SameSite cookies and proper authorization even when CORS is configured correctly.
| Feature | CORS | CSRF protection |
|---|---|---|
| What it controls | Whether a page may read a cross-origin response | Whether a cross-origin request may change state |
| Enforced by | The browser, using server-sent headers | The server, using tokens, SameSite cookies or origin checks |
| Stops an attacker reading your data? | Yes, in a browser | Not its job |
| Stops an attacker changing your data? | No | Yes |
| Applies outside browsers? | No — curl and servers ignore it | Yes, if the server enforces it |
Why do CORS errors happen?
Most CORS errors fall into a few patterns. Work through them in order, using the browser console — the console message names the missing header, while JavaScript itself only sees a generic error.
- No Access-Control-Allow-Origin header at all. The server has no CORS configuration, or the CORS middleware only runs on some routes. Check that the header is present on the failing response, not just on a successful one.
- Origin not in the allowlist. The server allows
https://app.example.combut your page runs onhttp://localhost:3000or a preview deployment with a different host. Every origin, including local development ones, must be listed. - Wildcard with credentials. Browsers reject
Access-Control-Allow-Origin: *when the request includes credentials. The server must echo the exact requesting origin instead. - Missing Allow-Headers or Allow-Methods. The preflight fails because the server did not approve a custom header or method. The actual request is never sent.
- Missing Vary: Origin. When a server echoes different origins for different callers, caches can serve one origin's CORS response to another. OWASP's secure-headers guidance and MDN both note the
Vary: Originheader as the fix. - Redirect after preflight. Some browsers still refuse to follow a cross-origin redirect after a preflight. Avoid redirects on API endpoints that require preflight.
A useful diagnostic: repeat the request with curl. If curl gets a good response but the browser blocks it, the problem is CORS headers, not your API logic. If curl also fails, fix the API first.
Which CORS misconfigurations are dangerous?
CORS misconfiguration is a real security finding, and OWASP's Web Security Testing Guide (WSTG-CLNT-07) lists the patterns testers look for. The dangerous ones all share one trait: they let an attacker's website read authenticated responses as if it were your own front end.
- Wildcard origin with credentials.
Access-Control-Allow-Origin: *together withAccess-Control-Allow-Credentials: trueis the classic mistake. Browsers reject the combination, but the intent — trusting everyone with authenticated data — is what matters, and proxies or frameworks can mangle it into something exploitable. - Reflected origin. The server copies whatever
Originheader arrives intoAccess-Control-Allow-Originwithout checking it against a list. That is a wildcard with extra steps: any site on the internet is trusted. - Loose subdomain matching. A regex meant to allow
*.example.comcan be satisfied by an attacker-controlled domain such asnotexample.comorexample.com.attacker.comif the pattern is not anchored properly. - Allowing the null origin. Some sandboxed content sends
Origin: null. Trusting it lets attacker-controlled sandboxed pages read your responses.
OWASP also warns against relying on the Origin header for access control. It is sent by browsers and cannot be changed from JavaScript, but anything outside a browser can set it to any value. Authentication and authorization must live on the server, independent of CORS.
How should you configure CORS safely?
Start from the data, not the framework default. Ask three questions: is the resource public or authenticated, which exact origins need access, and which methods and headers do those origins actually use? Then configure only that much.
OWASP's Secure Headers Project gives a clean set of patterns:
- Public, no authentication:
Access-Control-Allow-Origin: *with credentials disabled. Fine for public feeds, fonts and open data. - Restricted, no authentication: echo the origin only if it is on your allowlist; otherwise return 403 with no CORS headers.
- Restricted, with authentication: same as above, but with
Access-Control-Allow-Credentials: trueand an exact origin — never*.
Other good habits: keep Access-Control-Max-Age modest (an hour or less) so policy changes take effect quickly; list only the methods and headers your front end uses; add Vary: Origin whenever the response depends on the requesting origin; and test the configuration with a request from an untrusted origin to confirm it is actually rejected.
If you use a framework's CORS middleware, read what it does by default. Some libraries reflect any origin when credentials are enabled, or allow all methods and headers — exactly the settings you are trying to avoid. A five-minute review of the defaults beats a post-incident fix.
How do you test a site's CORS setup?
Test from the attacker's point of view, not just from your own front end. Send a request with an Origin header for a domain you do not own and see whether the server allows it — especially with credentials. OWASP's testing guide recommends exactly this: identify endpoints that implement CORS, then check whether the configuration is secure or harmless.
Also run Cysoro's free security headers checker against your site to review the headers it actually sends.
Confirm the site is served over valid HTTPS with the SSL checker — CORS over plain HTTP exposes credentials and tokens to anyone on the network.
For a broader review, see how Cysoro's website security platform monitors headers and certificates continuously.
The bottom line
CORS is not a wall and not a vulnerability by itself — it is a permission slip. The browser enforces it, the server writes it, and the safe version is always the narrow one: exact origins, only the methods and headers you need, and no wildcard anywhere near credentials.
When a CORS error appears, read the console, check the headers with curl, and fix the server configuration instead of disabling the browser's protection. The error is the browser telling you the permission slip is missing — and that is exactly the behavior you want it to have.
Run a quick check of your own site's headers with the security headers checker to see what your server is actually telling browsers today.
Frequently asked questions
What is CORS in simple terms?
CORS is the set of rules that lets one website ask another website for data and actually read the answer. By default, browsers block JavaScript on one site from reading responses from a different site. CORS lets the server that owns the data opt in and say which sites are allowed to read it.
What is the same-origin policy?
The same-origin policy is a browser security rule that stops JavaScript on one origin from reading data from a different origin. An origin is the combination of scheme, host and port, so a site and its API subdomain are different origins even though they look related. CORS is the standard way for a server to relax that rule in a controlled way.
Why do I get a CORS error?
A CORS error means the browser asked for a cross-origin resource and the server did not return the headers that permit your origin to read it, or returned headers that do not match. The request usually reaches the server and the server usually responds; it is the browser that refuses to hand the response to your JavaScript. Check the browser console for the exact header that is missing.
What is a CORS preflight request?
A preflight request is an OPTIONS request the browser sends before the real request when that request could change data on the server, for example a DELETE request or a POST with a JSON body or a custom header. The preflight asks the server which methods and headers it accepts. Only if the server approves does the browser send the actual request.
Is Access-Control-Allow-Origin: * safe?
It is safe only for public resources that need no authentication, such as a public data feed or a font file. It is not safe for any endpoint that accepts cookies or auth tokens, because then any website could make authenticated requests as your users and read the answers. For authenticated endpoints, list the exact trusted origins instead.
Does CORS protect my API from attackers?
No. CORS only controls what a browser lets a web page read. Attackers can call your API directly with curl, Postman or their own server and ignore CORS headers completely. You still need authentication, authorization and CSRF protection on the server. Treat the Origin header as a hint, never as authentication.
How do I fix a CORS error in development?
The correct fix is to configure the API server to allow your development origin, for example your local development address, with the right methods, headers and credentials settings. Browser extensions and flags that disable CORS are workarounds for local testing only and must never be used in production or relied on by real users.
Sources
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against MDN's CORS guide, the WHATWG Fetch Standard and OWASP's CORS testing and secure-headers guidance.
How we write and fact-check →