Catalog My learning Blog
Browser Security Headers: CORS, CSP and Cookies Explained · Phase 1 · Lesson 1 · 7 min read

What Is an Origin? Web Origins Explained

On this page
  1. Learning Objective
  2. Concept Explanation
  3. Worked Example: Reading the Origin the Browser Actually Computed
  4. Real-World Examples
  5. Review Questions & Answers

Learning Objective

Define an origin precisely as the tuple of scheme, host, and port, distinguish it from a site and from a domain, and explain why the browser treats the origin as the fundamental unit of trust for every piece of state it holds on a user's behalf.

Concept Explanation

An origin is the triple of scheme, host, and port: https, www.example.com, 443. Two URLs share an origin only if all three components match exactly. That means https://example.com and http://example.com are different origins because the scheme differs, https://example.com and https://app.example.com are different origins because the host differs even though the registrable domain is identical, and https://example.com and https://example.com:8443 are different origins because the port differs. There is no partial matching, no "close enough," and no subdomain inheritance: the comparison is a string equality check on three fields, and that bluntness is deliberate, because a security boundary that requires judgment is a security boundary that gets it wrong.

The reason this matters so much is that a browser is a hostile multi-tenant runtime. At any given moment it is executing code from your bank, from a news site, from an advertising network, and from whatever link a user clicked five minutes ago, all inside the same process tree, all with access to the same network stack and the same cookie jar. Unlike a server, which chooses what code it runs, a browser runs whatever arbitrary strangers send it, and it does so with the user's ambient credentials attached. The origin is the answer to the question "on whose behalf is this code running, and therefore what state is it allowed to touch."

Almost every piece of client-side state is partitioned by origin rather than by tab, window, or domain. localStorage, sessionStorage, and IndexedDB are keyed by origin. The DOM of a document is readable only by same-origin script. Permissions such as camera, microphone, geolocation, and notifications are granted per origin. Service worker registration and scope are per origin. When you see a browser feature that remembers something or grants something, the key it is stored under is almost always the origin string, which is why the exactness of the definition has such far-reaching consequences.

Cookies are the notable and historically awkward exception, and this exception causes a great deal of confusion. Cookies predate the origin concept and are scoped by domain and path rather than by origin, which means a cookie set by https://example.com with Domain=example.com is also sent to http://example.com and to https://sub.example.com. The scheme is not part of the cookie's identity at all by default, so a cookie set over HTTPS is, absent the Secure attribute, happily transmitted over plaintext HTTP. Much of the cookie hardening covered later in this course exists specifically to retrofit origin-like discipline onto a mechanism that was never designed with it.

The Same-Origin Policy was introduced in Netscape Navigator 2.0 in 1995, when JavaScript first gained the ability to script a document, and the version enforced today is essentially the same idea hardened over three decades of attacks. It is worth internalizing that the policy is not one switch but a family of related rules applied at many different points: DOM access, network response reading, storage access, and script access to another window's properties are each governed by their own checks that happen to share the same origin comparison. Learning the model means learning where each of those checks sits, which is what the rest of this phase covers.

URL pairSame origin?Why
https://example.com/a and https://example.com/bYesPath is not part of the origin; only scheme, host, and port are
https://example.com and http://example.comNoScheme differs, and an HTTP page can be tampered with in transit
https://example.com and https://app.example.comNoHost differs; there is no automatic subdomain inheritance
https://example.com and https://example.com:8443NoExplicit port differs from the scheme default of 443
https://example.com and https://EXAMPLE.comYesHost comparison is case-insensitive after URL normalization

Worked Example: Reading the Origin the Browser Actually Computed

The most reliable way to settle an argument about whether two URLs share an origin is to let the browser answer, since its URL parser has already applied normalization rules such as lowercasing the host and dropping the default port. The serialized origin the browser computes is also exactly the string it will put in the Origin request header and compare against Access-Control-Allow-Origin later in this course.

Code
// In any browser console:
new URL('https://Example.com:443/path?q=1').origin  // 'https://example.com'
new URL('https://example.com:8443/path').origin     // 'https://example.com:8443'
new URL('http://example.com/path').origin           // 'http://example.com'
new URL('blob:https://example.com/uuid').origin     // 'https://example.com'

// A sandboxed iframe without allow-same-origin gets the opaque origin,
// which serializes to the literal string 'null' and is equal to nothing,
// not even to another opaque origin:
// <iframe sandbox src="..."> -> document.origin === 'null'

// The same string the browser sends on a cross-origin fetch:
// Origin: https://example.com
An origin decomposed into its three components, scheme, host, and port, with four example URLs scored against a reference origin showing which single differing component makes each one cross-origin.
An origin decomposed into its three components, scheme, host, and port, with four example URLs scored against a reference origin showing which single differing component makes each one cross-origin.

Real-World Examples

  • The Same-Origin Policy was introduced in Netscape Navigator 2.0 in 1995 alongside the first release of JavaScript, specifically because the new ability for a page to script a document made it immediately obvious that a page from one site must not be able to read a document loaded from another, and the rule has remained the foundation of web security for three decades since.
  • Google Chrome shipped Site Isolation by default for desktop in Chrome 67 in May 2018, moving documents from different sites into separate operating system processes, because the Spectre family of speculative execution attacks disclosed in January 2018 showed that same-process memory could be read across the language-level origin boundary; the response was to back the logical origin boundary with a hardware-enforced process boundary rather than trusting the JavaScript engine alone.
Key TakeawaysAn origin is scheme plus host plus port, compared by exact equality with no subdomain or scheme flexibility. Almost all client-side state is keyed by that exact string, with cookies as the historical exception that is scoped by domain and path instead, which is why cookies need their own dedicated hardening attributes covered later in this course.

Review Questions & Answers

The scheme is part of the origin tuple, and the distinction is substantive rather than pedantic: content served over plaintext HTTP can be read and modified by any network intermediary, so treating it as equivalent to HTTPS content would let an attacker who can tamper with one insecure response reach into the state of the secure origin. Keeping them separate means a compromised HTTP page cannot read the HTTPS origin's storage or DOM.

Cookies are scoped by domain and path, a model that predates origins. A cookie with Domain=example.com is sent to every subdomain and, unless it carries the Secure attribute, over plaintext HTTP as well. That means any subdomain a company does not fully control, and any single insecure endpoint, becomes a path to read or overwrite cookies belonging to the main site, which is exactly the gap the Secure attribute and the __Host- prefix were designed to close.

The Same-Origin Policy is enforced by the browser's own code, which means it only holds if the process running that code cannot read memory it is not supposed to. Spectre showed that speculative execution could leak memory contents across language-level boundaries inside a shared process, so the logical check could be bypassed without ever violating it in the JavaScript sense. Site Isolation restores the guarantee by putting different sites in different operating system processes, so there is no shared address space left to leak from.

The browser has no way to know who owns what. Subdomains are frequently delegated to third parties, assigned to staging environments with weaker controls, or left dangling and claimable by an attacker after a service is decommissioned. Because the browser cannot distinguish a trusted internal subdomain from a hijacked one, it treats every distinct host as a distinct origin and requires the two parties to opt into sharing explicitly, through CORS or postMessage, rather than inferring trust from a shared registrable domain.