Networking / Same-origin policy

Same-origin policy

What stops one website’s script from reading another site’s data in your browser? Follow the rule from the definition of an origin to its two official exceptions.

Intermediate7 steps~7 min
Asking page — the page whose script wants data
Other origin — another site’s page, frame or server
Browser — runs the pages and enforces the rule
// THE SHORT VERSION

In 30 seconds

A page’s origin is the scheme, host and port of its address, for example https://bank.example.

The same-origin policy is a browser rule: a script may read data only from the origin its own page came from. That stops a page you happen to open from reading your bank or mail with the logins your browser holds.

Linking, embedding and sending to other origins stay allowed. When two origins do want to share, a server can opt in with CORS headers, and windows can exchange messages with postMessage.

Same-origin policy, step by step

STEPS01 / 07

Simplified. Browsers now also limit cross-site cookies (SameSite), a separate defence. CORS requests that are not “simple” are first cleared with a preflight OPTIONS request.

STEP 01 / 07 An origin is three things
THE MATH
origin = scheme + host + port https + bank.example + 443
  1. Step 1: An origin is three things. Every page and every response belongs to an origin: the scheme, the host and the port of its URL, taken together. The path does not count. Here the origin is https://bank.example, with port 443 usually left unwritten because it is the default for https.
  2. Step 2: Same origin or not?. Two URLs share an origin only if all three parts match exactly. A different path is still the same origin. Switching to http, moving to a subdomain such as api.bank.example, or using another port each makes a different origin.
  3. Step 3: The danger without a rule. Why this matters: your browser holds your bank login, and has traditionally sent the bank’s cookie with any request to the bank, whichever page started it. If a script on other.example could read the reply, every site you visit could read your account as you.
  4. Step 4: The rule: no reading across. The same-origin policy is the browser’s answer: a script may read data only from its own origin. The request to the bank may still go out, but the browser withholds the reply from the script. The page on other.example learns nothing.
  5. Step 5: What stays allowed. The rule blocks reading, not all contact. A page may link to other origins, embed their images, scripts and frames, and send them forms. It cannot read the pixels, look inside the frame or see the reply. Because sending is allowed, forged requests (CSRF) need their own defences.
  6. Step 6: CORS: the server opts in. Sometimes reading across origins is wanted, as when app.example calls its API on api.example. With CORS the browser states the asking origin, and the server answers with a header naming who may read. If the names match, the browser hands the reply to the script.
  7. Step 7: postMessage: a narrow door. Two windows from different origins, such as a shop page and a payment frame inside it, cannot touch each other’s contents. postMessage gives them a narrow channel: one posts a message naming the origin it is meant for, and the receiver checks which origin sent it before trusting it.
VALUES SO FAR
schemehttpspart
hostbank.examplepart
port443part
same if····rule
read····rule
embed····rule
CORS····exception
windows····exception

Spotted a mistake on this page?