Same-origin, CORS and CSP: who allows what to whom
The same-origin policy stops a script from one origin (scheme, host and port) from reading the responses and DOM of another. CORS deliberately relaxes it through response headers, and CSP complements it as a second line against XSS. The policy limits reading, not sending a request, which is why CSRF exists. CORS works only in the browser, and a classic mistake is reflecting Origin with credentials.
Pentest · HTTP
A foreign page can send a request to your bank but cannot read the answer. When does that break?
A user opened an attacker’s site in one tab and has a banking account open in another. Why can the attacker’s page not simply read the balance? And why does CSRF exist at all? Both questions are answered by the same-origin policy, while CORS and CSP describe how it is relaxed and complemented.
How it works and why. An origin is the scheme, host and port together. The same-origin policy does not let a script from one origin read the responses and DOM of another. It exists because the browser attaches cookies to requests to the bank by itself, and without it any page would read everything a logged-in user can see.
What counts as the same origin
| Difference | Example | Same? |
|---|---|---|
| path | shop.example/cart | yes |
| scheme | http://shop.example | no |
| subdomain | api.shop.example | no |
| port | shop.example:8443 | no |
Compared with https://shop.example. An important detail: the policy restricts reading the response, not sending the request. A form or an image from another site still sends the request with the cookie and the server executes it. CSRF rests on exactly this, which is why a token, an Origin check and SameSite are needed against it.
CORS. Sometimes a legitimate front end on app.shop.example has to read an API on api.shop.example. The API then allows it in a response: Access-Control-Allow-Origin: https://app.shop.example. If the request is “non-simple” (a method such as PUT or DELETE, an Authorization header, JSON in Content-Type), the browser first sends an OPTIONS request (a preflight) and asks permission. A “simple” request goes straight away, so CORS does not guarantee that an unwanted request will not reach the server, it only stops a script from reading the response. And CORS protects only in a browser: curl does not notice it.
Example: CORS that opens private data to foreign sites. The tester adds an arbitrary Origin to the request (only on a test system within scope):
GET /api/account HTTP/1.1
Host: api.shop.example
Origin: https://evil.example
Cookie: __Host-sid=...
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://evil.example
Access-Control-Allow-Credentials: true
{"email":"user@shop.example","iban":"UA..."}
The server mirrors any Origin and allows credentials: if the session cookie is sent cross-site (SameSite=None), the page evil.example will read the victim’s data in the victim’s browser. The fix: an allowlist of origins with exact comparison, not a mirror and not an “ends with shop.example” check (evilshop.example matches it), a Vary: Origin header and no acceptance of the value null (sandboxed iframes and local files send it). The value * together with credentials is rejected by browsers, so it cannot be abused the way mirroring can.
Mirroring the Origin against an allowlist
The server returns whatever Origin arrived plus credentials: true. A foreign site reads the victim’s responses.
An exact list of trusted origins, Vary: Origin, and no Allow-Origin at all for the rest.
CSP. The Content-Security-Policy header tells the browser where scripts and other resources may be loaded from and usually forbids inline scripts. For example, default-src 'self'; script-src 'self'; object-src 'none'. It is a second line of defence: if XSS succeeded, the policy often stops the script from running or sending data out. But it does not replace output encoding, and a policy that is too broad (unsafe-inline, *) gives almost nothing.
A real case. In October 2005 Samy Kamkar released the Samy worm on MySpace: code in a profile that added him as a friend and copied itself into the profile of everyone who viewed it. Over a million users ran it within 20 hours. The same-origin policy did not help here: the script ran as part of MySpace’s own page, that is in the same origin, and acted on behalf of the visitor. MySpace closed the vulnerability. The lesson: SOP does not protect from XSS, output encoding and CSP do, and a “joke” without permission is still a crime.
Limits. SOP and CORS work only in a browser: a direct request (curl, a script, a mobile app) does not notice them, so data is protected by authentication and server-side permission checks. SameSite=Lax does not cover GET requests with side effects or requests from subdomains of the same site, and CSP helps little if the policy allows unsafe-inline or domains with arbitrary content.
Common mistakes.
- Thinking that CORS blocks the request itself: reading the response is blocked, while the side effects of a simple request have already happened.
- Allowing any subdomain with a regular expression: one forgotten or taken-over subdomain gives everyone access.
- Treating CORS as authentication:
curland server-side scripts simply do not apply it: CORS works only in a browser. - Adding a CSP “for the checkbox” with
unsafe-inline: XSS passes just the same.
Check yourself
An API responds with Access-Control-Allow-Origin: * without credentials. Can a foreign site read a logged-in user’s data?
Answer
Not data that depends on a cookie: even if the script sends the request with the cookie, the browser will not let it read the response (a wildcard does not work with credentials), and without the cookie the server returns only public data. There is a risk if the data is not public and access is granted by network position (an internal network) instead of a cookie.
Why can the attacker’s page send POST /transfer with the victim’s cookie even though it cannot read the response?
Answer
The same-origin policy restricts reading, not sending. A form from another site sends the request and the browser attaches the cookie. That is why a token, an Origin check and SameSite are needed against CSRF.