Track 08 · Lesson 2 of 20Attacks and defenses
- 01What HTTPS protects
- 02The same-origin policy
- 03CORS
- 04Security headers
- 05Cookies done right
- 06Cross-site scripting (XSS)
- 07Content Security Policy
- 08Cross-site request forgery (CSRF)
- 09Clickjacking
- 10Unsafe URLs and open redirects
- 11Prompt injection
- 12SQL and command injection
- 13Prototype pollution, CRLF and SVG
- 14Server-side request forgery (SSRF)
- 15Remote code execution and denial of service
- 16Spoofed headers
- 17Rate limiting
- 18Cache poisoning
- 19The OWASP Top 10
- 20The attack lab
Track 08 · Attacks and defenses
The same-origin policy
Lesson 2 of 20· 6 min
An origin is scheme + host + port. By default, a page can only read data from its own origin. This rule is why a random site can’t read your email in another tab.
Same origin, same site?
Pick a pair or type your own URLs
| Part | Page A | Page B | Match |
|---|---|---|---|
| Scheme | https | https | Same |
| Host | app.example.com | api.example.com | Different |
| Port | 443 (implied) | 443 (implied) | Same |
The host differs. A can link to B, embed it or submit forms to it, but can’t read B’s responses unless B allows it with CORS.
Both belong to https://example.com, so SameSite cookies are sent between them.
Same-site is looser#
app.example.com and api.example.com are different origins but the same site. That matters for cookies: SameSite stops cookies being sent from other sites, but not from your own subdomains. Browsers decide what counts as a site with the Public Suffix List, which is why two github.io pages are separate sites.
Sending is allowed, reading is not#
The same-origin policy doesn’t stop another site from making your browser send requests to your app. Links, images and forms to other sites are the whole point of the web, and the browser attaches your cookies to them. What it blocks is reading the answer.
What can evil.example do to your bank?
Switch the cookie’s SameSite value
A normal link to bank.example/statements
<a href="https://bank.example/statements">Request sentCookie attachedCan’t read the answerThe statements page opens in a new tab. evil.example can’t see it.
An image tag pointing at the bank
<img src="https://bank.example/avatar.png">Request sentNo cookieCan’t read the answerThe image shows, but evil.example can’t read its pixels: drawing it taints the canvas.
A hidden form that posts a transfer
<form method="POST" action="https://bank.example/transfer">Request sentNo cookieCan’t read the answerNo cookie goes along, so the bank sees a logged-out request and refuses.
fetch() of the balance, with credentials
fetch("https://bank.example/api/balance", { credentials: "include" })Request sentNo cookieCan’t read the answerThe request reaches the bank, but the browser hides the response: CORS error.
Frame the bank and read its page
frame.contentDocument.body.innerTextRequest sentNo cookieCan’t read the answerThe frame may show, but reading it throws SecurityError: Blocked a frame with origin “https://evil.example”.
So other sites can make your browser do things; they just can’t see the result. That gap is where CSRF lives, a few lessons from now.
Check yourself