# Same-Origin Policy

A browser security boundary that restricts how documents and scripts from one origin can interact with resources from another origin.

- Type: principle
- Domain: Engineering & architecture
- Level: Intermediate
- Last reviewed: 2026-09-23
- Original: [Soroush’s Product Wiki](<https://wiki.soroush.uk/topic/same-origin-policy/>)

## What 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.

## Why 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.

## When 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.

## How it works

1. The browser assigns each document and resource an origin from its URL and applicable rules.
2. Scripts receive broad access to same-origin documents, storage, and responses.
3. Cross-origin reads and DOM access are blocked unless a defined mechanism permits them.
4. Servers can permit selected fetch responses through CORS, while windows can exchange validated messages through postMessage.
5. Applications layer authorization, CSRF defenses, frame policy, and content controls on top of browser isolation.

## A 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.

## Where 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.

## Visualization

[View the visualization on the website](<https://wiki.soroush.uk/topic/same-origin-policy/#visualization-same-origin-policy>)

## Sources

1. [Same-Origin Policy](<https://developer.mozilla.org/en-US/docs/Web/Security/Defenses/Same-origin_policy>) — MDN Web Docs · Official technical documentation
2. [Fetch Standard](<https://fetch.spec.whatwg.org/>) — WHATWG · Living web standard

## Related topics

- [Cross-Origin Resource Sharing \(CORS\)](<https://wiki.soroush.uk/topic/cross-origin-resource-sharing/>)
- [Cross-Site Request Forgery \(CSRF\)](<https://wiki.soroush.uk/topic/cross-site-request-forgery/>)
- [Cross-Site Scripting \(XSS\)](<https://wiki.soroush.uk/topic/cross-site-scripting/>)
