The Wild West Problem
Imagine the internet without security boundaries. You're on your bank's website in one tab and some random blog in another. The blog's JavaScript could:
- Read your bank account balance
- Transfer money to the attacker's account
- Download your transaction history
- Change your password
Scary, right? In the early days of the web, this was actually possible! Then Netscape engineers invented the Same-Origin Policy (SOP) in 1995, and it became the foundation of web security.
Think of SOP as the "mind your own business" rule of the internet. Websites can only interact with their own stuff, not poke around in other websites' data.
Why This Matters for Hackers
What Exactly is an "Origin"?
An origin is defined by three things. ALL three must match for two URLs to be considered "same origin":
Same Origin Examples
Let's see if these URLs have the same origin as https://example.com/page1:
| URL | Same Origin? | Why? |
|---|---|---|
| https://example.com/page2 | ✓ Yes | Path doesn't matter |
| https://example.com:443/page | ✓ Yes | 443 is default for HTTPS |
| http://example.com/page | ✗ No | Different scheme (http vs https) |
| https://api.example.com/page | ✗ No | Different host (subdomain) |
| https://example.com:8080/page | ✗ No | Different port |
| https://example.org/page | ✗ No | Different domain entirely |
api.example.com is NOT the same origin as example.com. This catches many developers off guard.What Does SOP Actually Block?
The Same-Origin Policy restricts how a document or script from one origin can interact with resources from another origin. But it's not a simple "block everything" rule. SOP is nuanced:
❌ Reading from Cross-Origin (Blocked)
- Reading content from an iframe with different origin
- Reading responses from fetch()/XMLHttpRequest to different origins
- Accessing another window's DOM
- Reading cookies of another origin
✓ Embedding Cross-Origin (Usually Allowed)
<img src="...">- Images from anywhere<script src="...">- Scripts from anywhere<link href="...">- Stylesheets from anywhere<iframe src="...">- Embed (but can't read content)<video>,<audio>- Media from anywhere<form action="...">- Submit to anywhere
✓ Writing Cross-Origin (Usually Allowed)
- Sending GET/POST requests (CSRF vulnerability here!)
- Redirects
- Form submissions
The Key Insight
Why This Matters: Real Attack Scenarios
Scenario 1: Without SOP
You visit evil-blog.com which contains:
Without SOP, your bank account details would be stolen just by visiting a malicious page!
Scenario 2: With SOP
The same code fails because:
- Browser sends request to yourbank.com
- Bank responds with your data
- Browser sees evil-blog.com trying to read response from yourbank.com
- Browser: "Nope, different origins!" and blocks JavaScript from reading response
CORS: When You WANT Cross-Origin Access
Sometimes cross-origin access is legitimate. Your frontend at app.example.comneeds to call your API at api.example.com. They're different origins!
CORS (Cross-Origin Resource Sharing) is a mechanism for servers to say "I'll allow this specific origin to read my responses."
How CORS Works
Simple CORS Request
Common CORS Headers
Setting
Access-Control-Allow-Origin: * combined withAccess-Control-Allow-Credentials: true is extremely dangerous and browsers actually block this combination!CORS Misconfigurations Hackers Love
CORS Misconfigurations
Ways to "Bypass" SOP
SOP isn't a wall - it's more like a fence with gates. Here are legitimate (and illegitimate) ways to get cross-origin access:
Legitimate Methods
- CORS: Server explicitly allows your origin
- JSONP: Old trick using script tags (deprecated, dangerous)
- postMessage: Controlled communication between windows
- document.domain: Relax SOP for subdomains (deprecated)
- Proxy: Your server fetches data, then serves it to your frontend
Attack Methods
- XSS: Inject code INTO the target origin, then it's same-origin!
- CORS misconfiguration: Abuse servers that reflect any origin
- DNS rebinding: Make a domain resolve to different IPs
- Dangling markup: Exfiltrate data through allowed elements
Test Your Knowledge
Hands-On Challenge
Key Takeaways
- Same-Origin Policy prevents websites from reading each other's data
- Origin = Scheme + Host + Port (path doesn't matter)
- Subdomains are DIFFERENT origins - api.site.com ≠ site.com
- SOP blocks READING, not SENDING - requests still go through
- CORS lets servers explicitly allow specific origins to read responses
- XSS bypasses SOP because injected code runs IN the target origin
- CORS misconfigurations are a common vulnerability in real-world apps