CORS: when the browser lets a foreign site read the response
CORS is the server's permission, enforced by the browser, for a foreign origin to read responses; it does not protect the server, because curl ignores it. It is dangerous when the server reflects an arbitrary Origin with Allow-Credentials, accepts null or compares origins by suffix or prefix. The right way is a closed list of full origins plus Vary: Origin.
Pentest · Client side
A frontend on another domain gets a CORS error. Add Allow-Origin: * and forget it?
A frontend is developed on app.example, the API lives on api.example, and the console shows a red CORS error. The quickest "cure" is to reflect any Origin in the response header or to set *. Sometimes this is harmless, and sometimes it lets any site read a user’s private data. The lesson tells the cases apart.
Mechanism. Under the same-origin policy a script on a page cannot read the responses of another origin (scheme + host + port). CORS (Cross-Origin Resource Sharing) is the server’s permission, enforced by the browser, to break this rule for chosen origins. So CORS does not protect the server: curl or any backend ignores it, and in simple cases (a form, GET) the browser still sends the request and only blocks reading the response. It protects the user from foreign pages and is not a means of authentication.
For "non-simple" requests (Content-Type: application/json, custom headers, PUT/DELETE) the browser first sends a preflight, an OPTIONS request asking for permission:
Preflight and the main request
With cookies (fetch(url, { credentials: "include" })) stricter rules apply: Access-Control-Allow-Origin must be a specific origin, not *, and Access-Control-Allow-Credentials: true is added. This very combination makes a mistake dangerous.
A vulnerable configuration example. An authorized test, your own account. A request with an arbitrary Origin:
GET /api/account HTTP/1.1
Host: api.shop.example
Origin: https://evil.example
Cookie: sid=<your-test-session>
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://evil.example
Access-Control-Allow-Credentials: true
The server reflected a foreign origin and allowed cookies. Now any page a logged-in user opens (provided his cookie is sent cross-site: SameSite=None in a browser that neither blocks nor partitions third-party cookies, such as Chrome by default) can run fetch to /api/account and read the response. A set of Origin variants is checked:
Origin: https://evil.example -> reflected?
Origin: null -> allowed?
Origin: https://shop.example.evil.net -> prefix check?
Origin: https://evilshop.example -> suffix check?
Origin: http://shop.example -> plain http trusted?
Typical configuration mistakes: reflecting any origin; allowing null (pages in a sandboxed iframe, file://, data: get that origin); weak comparison (endsWith("shop.example") lets evilshop.example through, startsWith("https://shop.example") lets https://shop.example.evil.net through); trusting all subdomains and http origins; a missing Vary: Origin (a cache may serve someone else’s response).
The fix: compare the full origin string with a closed list.
const ALLOWED = new Set([
"https://shop.example",
"https://admin.shop.example",
]);
if (ALLOWED.has(origin)) {
res.setHeader("Access-Control-Allow-Origin", origin);
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Vary", "Origin");
}
A real case. In October 2016 James Kettle (PortSwigger) published research on CORS mistakes in real sites. On one cryptocurrency exchange the server reflected any origin with cookies allowed, which let users’ API keys be stolen from a foreign page; the exchange fixed it in about 20 minutes. Others allowed null and used weak suffix or prefix comparison (definitelynotadvisor.com passed a check for advisor.com). The research showed that a "working" CORS made by reflecting the origin regularly reaches production.
Limits. * without cookies is acceptable for really public data (open fonts, a public API). But for a service inside an internal network even it lets a foreign page read, through an employee’s browser, what is reachable only from inside. Cookies with SameSite=Lax are not sent in a cross-site fetch, so some attacks are weaker, while an API with a token in the Authorization header (the site’s script takes it from localStorage) is not exposed to a foreign page by origin reflection: that page has no token, and the browser does not add it by itself.
Common mistakes.
- Treating CORS as protection of the server from requests. It only governs reading the response in a browser;
- Curing the error by reflecting the origin instead of a list;
- Not adding
Vary: Originand letting a cache mix responses.
Check yourself
An API answers Access-Control-Allow-Origin: * and Access-Control-Allow-Credentials: true. Can a foreign site read the user’s private data?
Answer
Not through this combination: the browser does not let the page read the response to a request with cookies when the value is *. It is dangerous when the server reflects a specific origin with that permission. The configuration should still be fixed because it points to a wrong understanding.
Why is the check origin.endsWith("shop.example") unreliable?
Answer
It lets through any domain with such an ending, for example https://evilshop.example. The full origin string has to be compared with a closed list, not a part of the string.