Skip to the page

The browser's security model · core

Origin, site and the same-origin policy

An origin is scheme + host + port, a site is scheme + registrable domain (eTLD+1), and the same-origin policy stops a page from READING another origin's responses, DOM and storage, while still letting it SEND many cross-origin requests.

The notes

What you must know

  • Origin = scheme + host + port. https://shop.example.com and https://api.example.com are different origins. :443 is the default port for https, so it does not change the origin.
  • Site = scheme + registrable domain (eTLD+1, using the Public Suffix List). shop.example.com and api.example.com are the SAME site. user1.github.io and user2.github.io are DIFFERENT sites because github.io is a public suffix. http and https are different sites (schemeful same-site).
  • Same-origin policy (SOP): a page cannot read another origin's fetch/XHR responses (unless CORS allows), cannot access its DOM in an iframe, and cannot read its localStorage or IndexedDB.
  • SOP does not stop sending: forms, <img>, <script>, <link> and simple fetches can all go cross-origin. That is why CSRF exists.
  • Cookies use their own, older rules (domain and path, and SameSite uses 'site', not 'origin'). Don't mix the two ideas.
  • Embedding is allowed (scripts, images, iframes), reading is not. A cross-origin script runs with YOUR page's origin and full access.

The red pen

Common traps: mistake → fix

  • Mistake: "Same-origin policy blocks cross-origin requests"

    Fix: it mostly blocks reading the response; many requests are still sent.

  • Mistake: "api.example.com and example.com are the same origin"

    Fix: different host means different origin, but they are the same site.

  • Mistake: "SameSite cookies are about origins"

    Fix: SameSite uses the site (scheme + eTLD+1), so subdomains of one site are same-site.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is an origin? Is http://example.com:80 the same origin as https://example.com?
  2. What does the same-origin policy block and what does it allow?
  3. What is the difference between 'same-origin' and 'same-site'? Why does it matter for cookies?
  4. Why can a malicious page still make your browser send a POST to your bank?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview