Skip to content

CORS in web applications: common risks and secure configuration

CORS is easy to misunderstand and easy to misconfigure. See what it really does and how to avoid allowing too much.

Reading time
9 min
Published

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.

Comparison: dangerous configuration and secure configuration
// 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: true

Common 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.

Frequently asked questions

What is CORS?
CORS (Cross-Origin Resource Sharing) is the mechanism a server uses to tell the browser which other origins can read its responses. By default, the browser prevents a site from reading the response to a request made to another domain, and CORS opens controlled exceptions to that rule. Note that the browser is what enforces CORS: a call made with curl or from another server does not go through it.
Does CORS protect my API?
Not directly. CORS is a browser mechanism that controls which origins can read cross-origin responses; it does not authenticate or authorize, and it does not affect requests made outside the browser. The API still needs its own access control on the server. A well-configured CORS policy prevents abuse from third-party pages, but it does not replace authentication.
Can I use a wildcard in Access-Control-Allow-Origin?
It is risky, and combined with credentials it does not even work: the browser does not accept a wildcard together with Access-Control-Allow-Credentials set to true. Allowing any origin lets third-party sites read responses in the user's browser. The recommended approach is to validate the origin by exact comparison against a list of trusted origins. A wildcard may be acceptable only for truly public APIs without credentials.
Is it safe to reflect the request origin?
Not if it is done without validation. Dynamically echoing the received origin back in the header amounts to allowing any origin, only in disguise. The correct approach is to compare the origin by exact equality with an allowlist and only then return it, adding Vary: Origin so caches are not poisoned. Reflecting without validation is one of the most common and most exploited CORS mistakes.
Is it enough to check that the origin ends with my domain?
No. Suffix matching lets through attacker domains that end with your name, and substring matching is even looser. Validation has to be by exact equality against a list of known origins, including the scheme and the port when relevant. Also be careful with the null origin value, which must not be treated as trusted.
Does CORS protect against CSRF?
No. CSRF exploits the fact that the browser sends cookies automatically, and many requests take effect on the server even if the attacker never reads the response, which is the part CORS controls. The defense against CSRF comes from cookies with the SameSite attribute, anti-CSRF tokens and server-side origin checks on sensitive operations. They are separate layers, and you need both.
Are CORS and CSP the same thing?
No. CORS controls which origins can read the responses to cross-origin requests. CSP (Content Security Policy) controls where the browser can load resources such as scripts and styles from, which helps against XSS. They are different mechanisms that complement each other: one handles cross-origin access, the other handles which content the page loads.

When to bring in specialist help

Assessing CORS together with the application's authentication and authorization is part of GUARDIASEC's Application Security and Penetration Testing services, which test how the API actually behaves and identify configurations that increase risk.