Same-Origin Policy (SOP)
The bedrock security boundary of the web: how browsers isolate untrusted origins.
The Same-Origin Policy (SOP) is a critical browser security mechanism that restricts how a document or script loaded from one origin can interact with resources from another origin. It prevents malicious sites from reading confidential data from banking or social portals you are logged into.
Without SOP, any website could run `fetch("https://your-bank.com/account/balance")` in the background, and the browser would automatically include your session cookies, allowing third-party sites to steal sensitive data.
SOP is enforced automatically by the user agent. Developers must structure cross-origin resource sharing deliberately via CORS and keep sensitive cookies scoped appropriately.
Technical Deep Dive
The Origin Tuple: Protocol + Host + Port
Two URLs share the same origin if and only if their protocol, host (domain), and port match bit-for-bit.
CROSS-ORIGIN BLOCKED: Protocol mismatch ('http' vs 'https'). The browser strictly isolates JavaScript execution. Attempting to inspect DOM or response bodies triggers a SecurityError DOMException.
What SOP Allows vs What SOP Blocks
SOP permits cross-origin writes (sending requests, submitting forms, linking) and embedding (images, scripts, styles), but strictly restricts cross-origin reads (reading response body, inspecting DOM of iframes, accessing cross-origin localStorage).
Interactive Mental Model Simulation
Live VisualizerCode Implementation & Patching
// On attacker.com
fetch('https://target-bank.com/api/user/transactions')
.then(res => res.json())
.then(data => exfiltrateToEvil(data)); // BLOCKED by SOP: read permission denied// Server explicitly declares cross-origin read policy // If public, server responds with CORS header: Access-Control-Allow-Origin: https://trusted-partner.com // Otherwise, the browser forbids attacker.com from reading the response.
Browsers enforce origin isolation on reading data. While the network request may succeed on the wire, the response body is kept inaccessible from client script unless CORS permits it.
Test Your Knowledge on Same-Origin Policy (SOP)
Challenge yourself with realistic scenario questions in the Web Security Arena.