Few web security topics cause as much confusion as CORS. Many people think it protects the API, but it is a browser mechanism: it controls which origins can read the response to a request made from another origin. Misconfigurations show up often. Some teams allow any origin for convenience, some echo the request origin back without validating it and some combine a broad origin with credentials, and each of these choices opens a real gap.
That is why the approach here is to understand the mechanism before touching the configuration. If you do not know exactly what CORS does, you end up weakening the policy without noticing. We start with what it does and what it does not do, including the preflight, and then move on to common mistakes, the relationship with CSRF and secure configuration.
What CORS does and what it does not do
The browser is what enforces CORS. It decides whether JavaScript from one origin can read the response to a request made to another origin by looking at the headers the server returns, such as Access-Control-Allow-Origin. Note that CORS is neither authentication nor authorization, and it has no effect on requests made outside the browser (a curl command on the command line, for example). Its reach is limited to cross-origin reads inside the browser.
So CORS does not replace access control on the server, and the API still has to authenticate and authorize every request. Anyone who uses CORS as a security barrier for the backend leaves the API exposed to any client that is not a browser. Those clients simply ignore the policy and reach the API anyway.
Preflight and headers in detail
Not every cross-origin request goes through the same check. Simple requests, such as a GET without unusual headers, are sent directly, and the browser only decides afterward whether JavaScript can read the response. Requests that use methods such as PUT and DELETE, or custom headers, trigger a preflight first. The preflight is an OPTIONS request in which the browser asks the server, through the Access-Control-Request-Method and Access-Control-Request-Headers headers, whether that method and those headers are allowed.
There is a security detail that often goes unnoticed: the wildcard in Access-Control-Allow-Origin cannot be combined with credentials. If the response carries Access-Control-Allow-Credentials set to true, the browser requires an explicit origin and rejects the wildcard. This is why so many servers echo back the origin they received, to get around the rule, and with that the risk comes back. If your origin is dynamic, include Vary with the value Origin. Without it, a cache along the way can store the response given to one origin and serve it to another.
// dangerous: origin reflected without validation, with credentialsAccess-Control-Allow-Origin: <reflected-origin>Access-Control-Allow-Credentials: true// safe: origin validated against a list before respondingAccess-Control-Allow-Origin: https://app.your-domainVary: OriginAccess-Control-Allow-Credentials: trueCommon mistakes and their impact
The most frequent mistake is allowing any origin with the wildcard. Worse still is taking the request origin and returning it in the header without validation, which in practice allows any site. If that comes together with permission for credentials, a malicious page can make authenticated requests on the user's behalf and read the responses. The result is a data leak.
A subtler variant is validating the origin with a loose comparison. If the code only checks whether the origin ends with your-domain, an address such as https://attacker-your-domain also ends that way and passes the check. Checking only whether the origin contains your domain is even worse. The comparison has to be exact, against a list of known origins. Also be careful with the null origin, which shows up in some contexts and must not be treated as trusted.
- Never allow any origin by default
- Return the request origin only after validating it against an exact list
- Validate the origin by equality, never by suffix or substring
- Treat the null origin as untrusted
- Take extra care when allowing credentials across origins
- Allow only the methods and headers the application uses
- Keep authentication on the server, since CORS does not replace it
CORS is not the defense against CSRF
Many people confuse CORS with CSRF protection, but the two deal with different problems. CORS controls whether JavaScript can read the response to a cross-origin request. CSRF exploits something else: the browser sends cookies automatically. Many requests (simple requests and form submissions, for example) reach the server and take effect even if the attacker never reads the response. In other words, a strict CORS policy alone does not stop a forged request from being processed.
The defense against CSRF comes from other controls. Cookies carry the SameSite attribute, state-changing requests carry an anti-CSRF token and the server checks the origin on sensitive operations. Treat CORS and CSRF as separate layers, where each one covers what the other leaves out.
Secure configuration and testing
A secure configuration starts with a list of trusted origins. When a request arrives, the server compares its origin with that list, by exact equality, and only then builds the response. Allow only the methods and headers the application uses and be very conservative when enabling credentials. Also keep the two cases apart: for public APIs without credentials, the wildcard may be acceptable; for authenticated APIs, the origin has to be explicit and validated.
Then test the behavior, always on your own systems and without attacking third parties. Check how the API responds to an unauthorized origin, to an origin that looks like the legitimate one (but is different) and to the null origin. Revisit the configuration with every new client or integration, because adding an origin to the trust list is a security decision like any other.
Practical checklist
- Treat CORS as a browser control, not a backend control
- Keep authentication and authorization on the server regardless of CORS
- Validate the origin by exact comparison against a list of trusted origins
- Do not reflect the origin without validation, and do not use suffix matching
- Add Vary: Origin when the origin is dynamic
- Avoid allowing any origin by default
- Be cautious when allowing credentials across origins
- Restrict methods and headers to what is needed
- Handle CSRF with SameSite and tokens, not with CORS
- Configure public APIs and authenticated APIs differently
- Test the response to unauthorized origins, look-alike origins and the null value
- Review the configuration when adding new clients
Best practices
- Understand that CORS protects the browser, not the API
- Use a list of trusted origins with exact comparison instead of a wildcard
- Be conservative when combining origins and credentials
- Keep real access control on the server
- Defend against CSRF with its own controls instead of counting on CORS
- Keep methods and headers to a minimum
- Review CORS whenever an integration changes
Common mistakes
- Allowing any origin with a wildcard
- Any site can read responses in the user's browser.
- Reflecting the request origin without validation
- It amounts to allowing any origin, only in disguise.
- Validating the origin by suffix or substring
- An attacker domain that ends with your name passes the check.
- Broad origin combined with credentials
- A malicious page makes authenticated requests and reads the user's data.
- Relying on CORS to stop CSRF
- Forged requests are still processed, even if no one reads the response.
- Treating CORS as backend security
- The API is left exposed to clients that are not browsers.
Authorship and review
Cesar Gargiulo
CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.
Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.