How the Same-Origin Policy (SOP) Limits Browser Data Access
The same-origin policy (SOP) is a browser boundary that limits how a document or script from one web origin can read another origin’s responses, DOM, and storage. An origin is the combination of **scheme, host, and port**: paths alone may differ, but HTTP versus HTTPS, different subdomains, or different ports are cross-origin. SOP does not block every cross-origin action; reads, writes, and embeds behave differently, and a server may grant reads through CORS.
Compare the origins of two URLs
The browser uses an origin to decide how documents and scripts may interact with data and APIs. The path and query do not form part of the origin.
| URLs | Same origin? | Why |
|---|---|---|
https://app.example.com/account and https://app.example.com/help | Yes | Only the path differs |
http://app.example.com and https://app.example.com | No | Scheme differs |
https://app.example.com and https://api.example.com | No | Host differs |
https://app.example.com and https://app.example.com:8443 | No | Port differs |
Same-site and same-origin are not interchangeable. Subdomains may be related at the site level but remain distinct origins. SOP, cookie SameSite rules, and CSRF defenses use different boundaries; one setting does not replace the others. MDN: Origin
Cross-origin reads, writes, and embeds differ
SOP does not treat every cross-origin action alike. Writes such as links, redirects, and many form submissions can be allowed. Embedding resources such as images, scripts, or frames is often possible. Directly reading response data through fetch() or XMLHttpRequest is generally restricted. Embedding can still reveal side information, such as an image’s dimensions or whether a resource exists, so blocked reads do not mean zero information exposure. MDN: Same-origin policy
This matters for CSRF. A browser may send a state-changing request with a user’s cookie even when the attacker’s page cannot read the response. SOP or CORS therefore does not replace server-side CSRF defenses. State-changing operations need controls such as CSRF tokens, appropriate cookie settings, and origin checks according to the application’s threat model.
CORS lets a server share responses with selected origins
When a front end from another origin needs to read an API response, the server can declare permitted origins using Cross-Origin Resource Sharing (CORS) headers. The browser checks Access-Control-Allow-Origin and related conditions before exposing the response to script. Some requests first send an OPTIONS preflight to ask whether the method and headers are allowed. Requests that skip preflight still require server permission for the script to read the response, and may still change server state. MDN: CORS
CORS is a browser response-sharing rule, not API authentication or a server firewall. Clients outside a browser are not stopped by SOP. For credentialed requests, allow only the origins that need access; reflecting any Origin or using a wildcard for sensitive responses can expose data. Separate controls that govern whether a request can reach the server from controls that govern whether browser code can read its response.
Validate message boundaries between documents
For cooperation between cross-origin frames or popups, consider window.postMessage() rather than opening broad DOM access. The receiver should compare event.origin to an exact expected origin, check event.source when appropriate, and validate the message schema and allowed actions. Avoid targetOrigin: "*" for sensitive data: if a popup navigates elsewhere, the data may go to the new origin. MDN: cross-origin script API access
The document.domain setter is deprecated and should not be used in new designs to relax subdomain isolation. Prefer narrowly configured CORS or validated messaging. Web Storage and IndexedDB are separated by origin. Cookies follow a different scope model and do not use port as an origin boundary, so check each storage mechanism’s actual scope.
Diagnose an access problem
Compare the page and request scheme, host, and port from the browser console; a shared “domain” alone does not establish a same origin.
Determine whether script is trying to read response data, embed another resource, or send a write. Review the control that applies to that action.
If API reads are required, inspect the server’s CORS allowed origins, methods, headers, and credential conditions. Allow only necessary origins; avoid wildcards and origin reflection.
Do not treat an SOP/CORS error as CSRF protection. Verify that the server checks the required token, session, authorization, and origin signals for state changes.
For iframe or popup messaging, verify `targetOrigin`, receiver-side `event.origin`, and the message schema. Do not add `document.domain` as a new workaround.
Do not treat Origin: null as a trusted domain. Sandboxed or data: documents and other opaque origins can serialize as null; it is not the name of a regular origin. MDN: Origin header
References
[MDN: Same-origin policy](https://developer.mozilla.org/en-US/docs/Web/Security/Defenses/Same-origin_policy)
[MDN: Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS)
[MDN: Origin](https://developer.mozilla.org/en-US/docs/Glossary/Origin)
[MDN: Origin header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Origin)
[MDN: Document.domain](https://developer.mozilla.org/en-US/docs/Web/API/Document/domain)