How Same-Origin Policy Isolates Storage and DOM

The Same-Origin Policy (SOP) is a critical browser security mechanism that restricts scripts running on one web origin from accessing sensitive resources belonging to another origin. By enforcing strict boundaries based on protocol, hostname, and port, SOP prevents malicious websites from reading unauthorized Document Object Model (DOM) data, hijacking user sessions, or accessing isolated client-side storage mechanisms like LocalStorage and IndexedDB.

Defining an Origin

A web origin is defined by the combination of three components: 1. Protocol (Scheme): e.g., https:// 2. Host (Domain): e.g., example.com 3. Port: e.g., 443 (implicit for HTTPS) or 8080

Two URLs share the same origin only if all three components match exactly. For example, https://example.com/app and https://example.com/login share an origin, whereas http://example.com (different protocol), https://sub.example.com (different host), and https://example.com:8080 (different port) are treated as distinct origins.

DOM Isolation Across Domains

When a webpage embeds another page using an <iframe> or opens a new window via window.open(), the browser maintains strict DOM segregation:

Client-Side Storage Isolation

The browser creates isolated sandbox environments for client-side storage, ensuring that data stored by one origin is inaccessible to scripts executing in another:

Through these combined constraints, the Same-Origin Policy ensures that interactions between disparate web domains remain isolated, preventing unauthorized state extraction and cross-site data tampering.