Same-Origin Policy
A browser security boundary that restricts how documents and scripts from one origin can interact with resources from another origin.
Download Markdown (.md)Soroush’s Product Wiki · https://wiki.soroush.uk/topic/same-origin-policy/
01What it means
The same-origin policy isolates web content using an origin defined primarily by scheme, host, and port. Cross-origin writes and embedding are often allowed in constrained ways, while reading data and accessing document objects are restricted. Mechanisms such as CORS and postMessage create explicit exceptions.[1][2]
- Same-origin DOM
- Broad scripted access
- Same-origin storage
- Origin-scoped state
- Cross-origin reads
- Restricted by default
- Cross-origin embedding
- Often allowed with constraints
- CORS
- Server permits selected response exposure
- postMessage
- Validated cross-window messages
Cross-origin embedding and writes may still occur; CORS and postMessage create explicit, constrained exceptions for reads or communication.
02Why it matters
Without origin isolation, any malicious page could read a user's authenticated mail, banking, or application data. Understanding the boundary explains browser behavior behind CORS, cookies, CSRF, frames, storage, and cross-window messaging.
03When to use it
Treat origin boundaries as a core assumption in every browser architecture. Use standard exception mechanisms narrowly when separate origins must communicate, rather than disabling checks or combining unrelated applications under one origin casually.
04How it works
- The browser assigns each document and resource an origin from its URL and applicable rules.
- Scripts receive broad access to same-origin documents, storage, and responses.
- Cross-origin reads and DOM access are blocked unless a defined mechanism permits them.
- Servers can permit selected fetch responses through CORS, while windows can exchange validated messages through postMessage.
- Applications layer authorization, CSRF defenses, frame policy, and content controls on top of browser isolation.
05A practical example
A script from shop.example cannot read account.example's page even if the user is logged in there. The account API explicitly allows one shop origin through CORS and still authenticates and authorizes every request.
06Where it breaks
The policy is not a server-side access control and does not prevent requests from being sent. Cross-site scripting runs inside the trusted origin, shared origins weaken isolation, and historical exceptions make behavior more nuanced than a simple host comparison.
Common mistakes
- Assuming subdomains, ports, or HTTP and HTTPS share one origin.
- Treating blocked response access as proof that no cross-origin request occurred.
- Using postMessage without checking the sender origin and exact target origin.
- Hosting mutually untrusted applications on the same origin.
07Sources
- Same-Origin PolicyMDN Web Docs · Official technical documentationSame-Origin Policy
- Fetch StandardWHATWG · Living web standardFetch Standard