Skip to content

Lesson 1 of 9 · 24m

The Same-Origin Policy as a Mental Model

Learn what an origin actually is, which browser features use that exact tuple, and why the SOP is a set of object-access rules rather than a single switch.

The first browser security mistake I still see senior engineers make is saying “same-origin policy” as if it were one rule. It is a family of rules that decide when one document may read, write, or even observe another web resource. The center of the model is simple: an origin is the tuple of scheme, host, and port. The danger is everything around that center.

Compute the origin before debating the bug

These are different origins:

https://app.example.com:443
http://app.example.com:80
https://sub.example.com:443
https://app.example.com:8443

The scheme matters. The registrable domain is not enough. The port matters, even when the browser hides default ports in the UI. Before calling anything “cross-origin,” write down both tuples. Half of confused security analysis disappears right there.

The policy is about interaction, not isolation theater

The SOP does not prevent one origin from navigating to another, embedding many kinds of content, or sending requests. It primarily prevents reading sensitive results and manipulating protected objects across the boundary.

// In an attacker-controlled document:
fetch('https://bank.example/account')
// The request may be sent with ambient authority depending on mode,
// but the response is not readable unless the bank explicitly allows it.

That distinction matters. A confused deputy attack can succeed even when the response is unreadable. CSRF is not an SOP bypass; it is abuse of requests the browser is allowed to send.

DOM access is the cleanest illustration

Two windows or frames can usually reach each other's window object because the platform needs controlled communication. What they cannot do is arbitrarily inspect each other across origins.

const child = window.open('https://billing.example/invoice');
child.postMessage('hello', 'https://billing.example');

// Direct DOM access throws or is blocked cross-origin:
child.document.querySelector('#total');

postMessage is explicit cross-origin communication. The target origin argument is not decoration. Passing * tells every listening origin that the message is safe to disclose.

Cookies do not follow the exact origin model

Cookies predate much of the modern platform and use domain, path, scheme, and SameSite semantics instead of pure origin matching. A host-only cookie is not the same as a broad .example.com cookie:

Set-Cookie: sid=abc123; Path=/; Secure; HttpOnly; SameSite=Lax
Set-Cookie: prefs=dark; Domain=example.com; Path=/; SameSite=None; Secure

This means sibling subdomains can sometimes affect each other through cookie scope even when they are distinct origins. Origin isolation and cookie isolation overlap, but they are not interchangeable.

Field notes

  • Treat redirects as part of the origin chain; a harmless start URL can end in a powerful origin.
  • file: URLs have implementation-specific and historically messy origin behavior. Never build security assumptions around them.
  • Default ports are normalized: https://example.com and https://example.com:443 are the same origin.
  • document.domain relaxation is legacy and increasingly restricted. Do not design modern isolation around it.
  • A blocked read does not prove a side effect did not happen. Analyze write, navigation, timing, and embedding channels separately.

Was this lesson helpful?

Sign in to rate