# Same-Origin Policy

> A critical security mechanism restricting how a document or script loaded by one origin can interact with a resource from another origin.

Subject: Internet | Lesson 17 of 35 | 3 min read
Interactive lesson: https://codewithanimation.in/internet/same-origin-policy
Previous: [Idempotency](https://codewithanimation.in/internet/idempotency.md) | Next: [CORS](https://codewithanimation.in/internet/cors.md)

## What it is

The Same-Origin Policy (SOP) is a critical security mechanism built into modern web browsers.

It restricts how a document or script loaded by one origin can interact with a resource from another origin. Two URLs have the same origin if and only if they share the exact same **Protocol** (e.g., `http` vs `https`), **Port** (e.g., `80` vs `443`), and **Host** (e.g., `www.example.com` vs `api.example.com`).

## Why it matters

SOP matters because it provides the baseline security that protects the user's data.

If you are logged into your bank account in one tab, and you visit an evil hacker's website in another tab, SOP prevents the hacker's scripts from reading your bank account data or making authenticated reads on your behalf. Without SOP, any website could read your personal data from any other website you are logged into.

## How it works

SOP doesn't block everything; it categorizes interactions:

**Cross-origin reads are blocked**: You cannot read data from another origin (e.g., reading the DOM of an iframe from another site, or reading the JSON response of a `fetch()` request).

**Cross-origin writes are allowed**: You can send data to another origin (e.g., submitting an HTML `<form>`, clicking a link, or redirecting).

**Cross-origin embeds are allowed**: You can display resources from another origin (e.g., `<script src="...">`, `<img>`, `<video>`, `<iframe>`), but you cannot inspect their contents via JavaScript.

## Common mistake: Common mistake

**Thinking SOP protects the server.** Many developers believe SOP stops cross-origin requests from ever reaching the server. In reality, the browser often *sends* the request, the server might process it and return a response, but SOP steps in at the last millisecond to **block the browser's JavaScript from reading that response**. SOP protects the *user's browser* and session, not the back-end server.

## Tip: Pro tip

Remember the **Mailbox Model**. Treat an origin like a house with a mailbox. Anyone can drop a letter into your mailbox (cross-origin writes are allowed). You can look out your window at your neighbor's house (cross-origin embeds). But you are strictly forbidden from opening your neighbor's mailbox and reading their mail (cross-origin reads are blocked).

## Try it: Try it

Play the **Border Guard Sandbox**. Try dragging different actions across origins and watch the guard allow writes and embeds, but slam the shutter on reads!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/same-origin-policy

## Check yourself

### Question 1

Which of the following URLs has the same origin as http://store.company.com/dir/page.html?

- A. https://store.company.com/page.html
- B. http://store.company.com:81/dir/page.html
- C. http://news.company.com/dir/page.html
- D. http://store.company.com/dir2/other.html

Correct answer: D

An origin is defined strictly by the combination of protocol, host, and port. Changes to the directory path or file name do not affect the origin.

- Why not A: Different protocol (https instead of http).
- Why not B: Different port (81 instead of the default 80).
- Why not C: Different host (news instead of store).

### Question 2

Which cross-origin action is generally *allowed* by the Same-Origin Policy by default?

- A. Reading the text content inside a cross-origin <iframe>.
- B. Reading the JSON response of a cross-origin fetch() request.
- C. Drawing a cross-origin image to a <canvas> and reading its pixel data using JavaScript.
- D. Embedding an image using an <img> tag.

Correct answer: D

SOP typically allows cross-origin embeds (like images, scripts, CSS) and cross-origin writes (like form submissions), but blocks cross-origin reads.

- Why not A: SOP strictly blocks reading cross-origin DOMs to prevent data theft.
- Why not B: SOP blocks reading the response unless CORS headers explicitly relax the policy.
- Why not C: While embedding the image is allowed, trying to read its pixel data 'taints' the canvas, and reading is blocked by SOP.

### Question 3

What is the primary purpose of the Same-Origin Policy?

- A. To protect the server from receiving malicious requests.
- B. To prevent denial-of-service (DoS) attacks on web servers.
- C. To ensure websites load faster by isolating resources.
- D. To protect the user by preventing malicious websites from reading their sensitive data on other websites.

Correct answer: D

The SOP is a browser-level security mechanism. It ensures that a malicious script running on tab A cannot reach into tab B (e.g., your bank) and steal the data loaded there using your authentication cookies.

- Why not A: SOP does not stop requests from reaching the server; it only blocks the browser from reading the response.
- Why not B: SOP has nothing to do with DoS protection; a malicious site can still fire thousands of requests.
- Why not C: SOP is a security mechanism, not a performance optimization.

## Remember this

- Origin = Protocol + Host + Port.
- SOP blocks reads, but generally allows writes and embeds.
- SOP protects the user's browser, not the back-end server.
