CCyberwiki

Same-origin policy

A script on https://attacker.example can call fetch("https://mail.example.com/inbox"), and the browser sends the request. The script cannot read the answer. The same-origin policy withholds it: a document or script loaded from one origin may read and change content from that same origin, and gets only narrow, fixed access to anything from another.1 Without it, any page a user opened could read their signed-in webmail or company intranet and pass the contents on.1

Origins

An origin is the scheme, host and port of an address taken together. Two addresses share an origin only when all three match; the path plays no part.1

Table 1. Addresses compared with https://www.example.com/app/page.html
AddressResultReason
https://www.example.com/blog/Same originOnly the path differs
https://www.example.com:443/Same origin443 is the default port for https
http://www.example.com/app/page.htmlCross-originDifferent scheme
https://www.example.com:8443/Cross-originDifferent port
https://api.example.com/Cross-originDifferent host

RFC 6454 wrote the concept down in December 2011, after the model had grown "largely organically".2 The scheme is in the tuple because otherwise an attacker on the network could tamper with a page fetched over plain HTTP and use it against the HTTPS pages of the same host, defeating TLS. The full host name counts, not the registered domain, because trust does not follow the DNS tree: student pages under example.edu/~student/ should not share a protection domain with grades.example.edu.2

Some addresses have no host to compare. A data: URL receives a fresh, unique origin, so it is not even same-origin with itself, and most browsers give file:/// pages opaque origins as well. An about:blank page takes the origin of the document that created it.21

Sending, embedding and reading

Links, redirects and form submissions may target any origin. A page may also embed content from elsewhere: run a script, apply a stylesheet, show an image, play media or frame a page from elsewhere. What the policy blocks is reading. A page cannot inspect the bytes of a response from another origin unless that origin opts in through CORS.12

A hostile page can therefore submit a form to another site from the user's browser; that is cross-site request forgery, and one defence is a token in the request that the hostile page has no way to read.21 Embedding leaks some information on its own: a page learns the height and width of an image it embeds and whether a resource loaded at all.2 Framing is permitted by default; the X-Frame-Options header refuses it (see Clickjacking).1

A script holding a pop-up or frame from another origin can close it, set its location or send it a message, but cannot read the document inside.1 Storage is split the same way: localStorage and IndexedDB data written by one origin are invisible to every other.1

Authority inside an origin

Content within one origin does not carry equal power. An image is passive and can reach nothing. An HTML document carries the full authority of its origin, and every script it contains or imports can use every resource in that origin. The browser tells the two apart by media type, which is why a site that serves uploaded files as image/png takes less risk than one that serves them as text/html.2

Cross-site scripting works through that gap. The policy grants authority by where a document came from, not by who wrote each line in it, so text from an attacker that ends up in the page as script runs with the site's own rights.2

Cookies use a different boundary

Cookies predate the origin concept and isolate on different terms, a mismatch that has led to vulnerabilities.2 A page may set a cookie for its own domain or for a parent domain, as long as the parent is not a public suffix, and a page reading a cookie cannot see where it was set, or whether that happened over plain HTTP.1 Browsers group addresses into sites for cookies: the scheme plus the registrable domain. https://app.example.com and https://api.example.com are cross-origin but same-site, while project.github.io is a site of its own because github.io is on the public suffix list.3 The flags that narrow a cookie's reach are in Cookie attributes.

Crossing on purpose

Two pages from different origins can still exchange data with window.postMessage. The sender names the origin allowed to receive the message, and the browser delivers it only when scheme, host and port all match. Passing "*" removes that check, and a message sent that way reaches a hostile page if the target window has meanwhile been sent to another address.4 The receiving side checks the sender before it trusts the data:

// Parent page on https://www.example.com
frame.contentWindow.postMessage({ type: "theme", value: "dark" }, "https://widget.example.net");

// Inside the frame on https://widget.example.net
window.addEventListener("message", (event) => {
  if (event.origin !== "https://www.example.com") return;
  if (event.data?.type !== "theme") return;
  applyTheme(event.data.value);
});

A page that expects no messages should register no message listener. One that does should check the shape of the data after the origin, so that a flaw in the trusted sender does not turn into script injection on the receiving side.4

The older route was document.domain: two subdomains that both set it to their shared parent were treated as same-origin, with the port ignored. On a host that gives each customer a subdomain, that opened a page to other sites on the same service.5 The setter is deprecated.1 On 30 May 2023 Chrome announced that the setter would stop working by default in version 115; a page that still depends on it must send Origin-Agent-Cluster: ?0.5

Spectre and Site Isolation

In 2018 browsers often ran several sites' JavaScript in one process. In January 2018 Google Project Zero disclosed Spectre and Meltdown, which abuse the speculative execution of processors so that untrusted code may read any memory in its own process. A page could then in principle read data from a cross-site frame or pop-up sharing its process, outside the reach of the policy.6

Chrome 67, in July 2018, enabled Site Isolation for 99% of desktop users: each renderer process holds documents from one site, and the operating system keeps processes apart. Cross-Origin Read Blocking keeps cross-site HTML, XML and JSON out of a page's process when it is requested as an image or script, and protects best when servers send the correct Content-Type and nosniff. The extra processes cost about 10 to 13% more memory in real workloads.6

In SecFlash

SecFlash reports CORS headers that open responses to other origins on the Security page. SF-SEC-114 means the server copies any requesting origin into Access-Control-Allow-Origin, and SF-SEC-113 that it trusts the null origin, which any site can send from a sandboxed frame. SF-SEC-112 flags * paired with credentials: browsers refuse that pair, but it shows the server was set up to share logged-in answers with anyone.

See also

References

  1. a b c d e f g h i j k "Same-origin policy". MDN Web Docs. Mozilla.
  2. a b c d e f g h i Barth, A. (December 2011). "RFC 6454: The Web Origin Concept". IETF.
  3. back Kitamura, Eiji. "Same-site" and "same-origin". web.dev. Google.
  4. a b "Window: postMessage() method". MDN Web Docs. Mozilla.
  5. a b Chrome for Developers (2022, updated 30 May 2023). "Chrome will disable modifying document.domain to relax the same-origin policy". Chrome for Developers. Google.
  6. a b Reis, Charlie (11 July 2018). "Mitigating Spectre with Site Isolation in Chrome". Google Online Security Blog.