Skip to content
Module 1 of 8Foundations
Estimated time: 15 min

Same-Origin Policy (SOP)

The bedrock security boundary of the web: how browsers isolate untrusted origins.

Core Concept & Mental Model

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.

Threat Vector & Impact

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.

Defensive Architecture & Mitigation

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

Interactive 2026 Origin Tuple Comparator

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.

Explore Core Scenarios:
Tuple Component
URL A
URL B
Verdict
1. Protocol / Scheme
http://
https://
2. Host / Domain
example.com
example.com
3. Port Number
:80
:80
Path / Query(Ignored by SOP)
/
/
PASS
VERDICT: CROSS-ORIGIN (DIFFERENT ORIGIN)

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 Visualizer
VISUALIZATION: SAME-ORIGIN POLICY
Origin A (bank.com)
User DataCookies, localStorage
Origin B (evil.com)
Malicious Script
A script on 'evil.com' tries to read data from 'bank.com'.

Code Implementation & Patching

Vulnerable Code (Exploitable)
// 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
Secure Patched Code (Hardened)
// 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.
Security Analysis & Core Architectural Rule

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.

Launch Practice Arena