Same-Origin Policy (SOP)

Difficulty: Intermediate

Question

What is the Same-Origin Policy, what exactly defines an origin, and what does the policy allow and block?

Answer

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.

Working out whether two URLs are same-origin

Compared against https://shop.example.com/cart (default port 443):

URLSame origin?Reason
https://shop.example.com/orders?id=5YesPath and query do not matter
http://shop.example.com/cartNoDifferent scheme
https://api.example.com/cartNoDifferent host (subdomains count)
https://shop.example.com:8443/cartNoDifferent port
https://shop.example.com:443/cartYes443 is the default for https
https://example.com/cartNoParent domain is a different host

What SOP blocks and what it allows

SOP is mainly about reading, not sending:

  • Cross-origin writes are generally allowed: links, redirects and form submissions to another origin work. This is exactly why CSRF exists.
  • Cross-origin embedding is allowed: <script src>, <img>, <link rel=stylesheet>, <video>, <iframe> can load from other origins.
  • Cross-origin reads are blocked: 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.

Relaxing SOP safely

  • CORS: the server opts in with 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.

Origin vs site (a common interview trap)

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.

Gotchas

  • SOP does not stop a request from being sent; a cross-origin POST with a form still reaches your server with cookies attached (subject to SameSite).
  • Scripts loaded via <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.
What the Same-Origin Policy does. SOP mostly blocks reading cross-origin data, not sending requests or embedding resources.
ActionCross-origin allowed?
Submit a form or follow a linkSend requestAllowed
Embed script, image, CSSLoad resourceAllowed
Read a fetch responseRead dataBlocked unless CORS
Access iframe DOMRead dataBlocked
Read localStorageRead dataBlocked, per origin

Code examples

Comparing origins in JavaScript

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.

Checking the sender in postMessage

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.

Key points

  • Origin = scheme + host + port; all three must match exactly.
  • SOP blocks cross-origin reads; it does not block sending requests or embedding resources.
  • CORS and postMessage are the controlled ways to relax SOP.
  • Same-site (scheme + eTLD+1) is not the same as same-origin; SameSite cookies use site.
  • document.domain is deprecated and ignored by default in Chromium browsers.

Common follow-up questions

  • If SOP blocks cross-origin reads, why is CSRF still possible?
  • How does CORS relax the Same-Origin Policy?
  • What is the difference between same-site and same-origin?
  • Why does drawing a cross-origin image taint a canvas?

Interview tips

  • Give the three components immediately, then walk through two or three example URLs.
  • Point out that SOP is about reading, not sending; linking this to CSRF impresses interviewers.
  • Mention the origin vs site distinction to show depth.

Concepts covered

Same-Origin Policy, Origin, CORS, Same-Site vs Same-Origin, Cross-Origin Reads

More Web Security (OWASP, JWT, XSS, CSRF) questions