Understanding Same-Origin Policy
The Same-Origin Policy (SOP) is a fundamental browser security mechanism that controls how web pages interact with resources from different origins. It prevents malicious scripts from accessing sensitive data across unrelated websites, protecting users from attacks like Cross-Site Scripting (XSS) and unauthorized data access.
Key Points
- SOP restricts scripts to interact only with resources from the same origin (protocol + domain + port)
- An origin is defined by three components: URI scheme, hostname, and port number
- Scripts cannot read data from different origins without explicit permission via CORS
- SOP blocks reading cross-origin data but allows sending certain requests (images, scripts)
- Modern workarounds like CORS and PostMessage API enable controlled cross-origin communication
What Defines an Origin?
An origin consists of three distinct components that must all match for resources to be considered same-origin:
| Component | Example | Description |
|---|---|---|
| URI Scheme | http, https | The protocol used to access the site |
| Hostname | example.com | The domain name of the site |
| Port | 80, 443 | The communication endpoint |
Example:
https://example.com:443andhttp://example.com:80are different origins because both the scheme and port differ.
Same-Origin vs Cross-Origin Examples
Same-Origin (all components match):
https://example.com/page1andhttps://example.com/page2http://store.example.com:80/cartandhttp://store.example.com:80/checkout
Cross-Origin (one or more components differ):
https://example.comandhttp://example.com(different scheme)https://example.comandhttps://api.example.com(different hostname)https://example.com:443andhttps://example.com:8080(different port)
How SOP Protects Users
Script Access Rules
- Allowed: Scripts can freely read and modify resources from the same origin
- Blocked: Scripts cannot access cookies, localStorage, DOM elements, or response data from different origins
- Partial: Scripts can trigger cross-origin requests (like loading images or submitting forms) but cannot read the responses
Real-World Security Scenario
Imagine you're logged into
https://bank.comwhile browsing other sites. Without SOP, a malicious script fromhttps://evil-site.comcould:
- Read your banking session cookies
- Extract account balances from the bank's DOM
- Initiate unauthorized transfers
SOP prevents these attacks by blocking
evil-site.comfrom accessing any data frombank.com.
Security Benefits
- Prevents XSS exploitation: Limits the damage from injected scripts to their own origin
- Protects sensitive data: Isolates cookies, session tokens, and user information per origin
- Enforces compartmentalization: Ensures compromised sites cannot affect other domains
- Reduces attack surface: Makes cross-site data theft significantly harder
Controlled Cross-Origin Access
While SOP is restrictive by default, legitimate use cases require cross-origin communication. Modern web standards provide secure methods:
| Method | Description | Use Case | Security Level |
|---|---|---|---|
| CORS | Server explicitly allows specific origins to access resources | REST APIs, CDNs | High |
| PostMessage API | Enables secure message passing between windows/iframes | Embedded widgets, OAuth flows | High |
| JSONP | Uses <script> tags to fetch data (legacy, deprecated) | Legacy applications only | Low |
Best Practice: Always use CORS for modern applications. JSONP is outdated and poses security risks.
CORS Example
A server can allow cross-origin requests by setting response headers:
Access-Control-Allow-Origin: https://trusted-site.com
Access-Control-Allow-Methods: GET, POST
Access-Control-Allow-Credentials: true
Common Misconceptions
Misconception: "SOP blocks all cross-origin requests"
Reality: SOP blocks reading cross-origin responses, not sending requests. Elements like <img>, <script>, and <link> can load cross-origin resources, but JavaScript cannot access the response data.
Misconception: "SOP and CORS are the same thing"
Reality: SOP is the default restrictive policy enforced by browsers. CORS is an opt-in mechanism that allows servers to selectively relax SOP for specific origins.
Misconception: "Changing the document.domain property bypasses SOP"
Reality: The document.domain property only works for subdomains of the same parent domain (e.g., app.example.com and api.example.com) and is deprecated in modern browsers.
Learn More
- MDN Web Docs: Same-Origin Policy
- MDN Web Docs: Cross-Origin Resource Sharing (CORS)
- OWASP: Cross-Site Scripting Prevention
- Web.dev: Browser Security Fundamentals