Difficulty: Intermediate
What is the Same-Origin Policy, what exactly defines an origin, and what does the policy allow and block?
An origin is the combination of three parts of a URL: the scheme (protocol), the host and the port. Two URLs are same-origin only if all three match exactly. The Same-Origin Policy (SOP) is the browser rule that stops JavaScript running on one origin from reading data belonging to another origin, which is what stops a random website from reading your Gmail or your bank balance using your logged-in cookies.
Compared against https://shop.example.com/cart (default port 443):
| URL | Same origin? | Reason |
|---|---|---|
https://shop.example.com/orders?id=5 | Yes | Path and query do not matter |
http://shop.example.com/cart | No | Different scheme |
https://api.example.com/cart | No | Different host (subdomains count) |
https://shop.example.com:8443/cart | No | Different port |
https://shop.example.com:443/cart | Yes | 443 is the default for https |
https://example.com/cart | No | Parent domain is a different host |
SOP is mainly about reading, not sending:
<script src>, <img>, <link rel=stylesheet>, <video>, <iframe> can load from other origins.fetch() or XHR responses, the DOM of a cross-origin iframe, and canvas pixels from cross-origin images (the canvas becomes "tainted") are not readable by your script.Storage is also partitioned by origin: localStorage, sessionStorage and IndexedDB for https://a.example.com are invisible to https://b.example.com.
Access-Control-Allow-Origin and related headers so specific origins can read responses.postMessage: windows on different origins exchange messages; the receiver must check event.origin.document.domain used to let subdomains merge origins. It is deprecated, and Chromium-based browsers now ignore setting it by default (origin-keyed agent clusters), so do not build on it.A site is the scheme plus the registrable domain (eTLD+1), for example https://example.com. So https://shop.example.com and https://api.example.com are same-site but cross-origin. SameSite cookies use the site definition, while SOP and CORS use the origin definition. That is why a SameSite=Lax cookie is still sent on requests between your own subdomains, and why a compromised subdomain can be a CSRF risk.
POST with a form still reaches your server with cookies attached (subject to SameSite).<script src> run with the origin of the page that includes them, not the origin they were downloaded from, so including third-party JS gives it full access to your page.file:// and sandboxed iframes get opaque origins ("null"). Never allowlist Origin: null in CORS.| Action | Cross-origin allowed? | |
|---|---|---|
| Submit a form or follow a link | Send request | Allowed |
| Embed script, image, CSS | Load resource | Allowed |
| Read a fetch response | Read data | Blocked unless CORS |
| Access iframe DOM | Read data | Blocked |
| Read localStorage | Read data | Blocked, per origin |
function sameOrigin(a, b) {
// URL.origin normalises default ports and lowercases the host
return new URL(a).origin === new URL(b).origin;
}
console.log(sameOrigin("https://shop.example.com/cart", "https://shop.example.com:443/x")); // true
console.log(sameOrigin("https://shop.example.com", "http://shop.example.com")); // false
console.log(sameOrigin("https://shop.example.com", "https://api.example.com")); // false
The URL API gives you the serialized origin, which is the reliable way to compare origins instead of string matching.
window.addEventListener("message", (event) => {
// Always verify who sent the message before trusting it
if (event.origin !== "https://checkout.example.com") return;
if (event.data?.type === "PAYMENT_DONE") {
showReceipt(event.data.orderId);
}
});
postMessage deliberately crosses origins, so the receiver must enforce its own origin check.
Same-Origin Policy, Origin, CORS, Same-Site vs Same-Origin, Cross-Origin Reads