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

Same-Origin Policy Explained

On this page
  1. Learning Objective
  2. Concept Explanation
  3. Worked Example: The Same Request, Sent Two Ways
  4. Real-World Examples
  5. Review Questions & Answers

Learning Objective

Explain the single most misunderstood property of the Same-Origin Policy, that it restricts reading cross-origin responses rather than sending cross-origin requests, and trace how that asymmetry is the direct cause of both CSRF and the need for CORS.

Concept Explanation

The sentence worth memorizing is this: the Same-Origin Policy is a read restriction, not a send restriction. A page on https://evil.example can absolutely cause your browser to issue a request to https://bank.example, with your cookies attached, and the request will reach the bank's server and be processed by it. What the browser refuses to do is hand the response content back to the JavaScript running on evil.example. The request happens, the side effects happen, and only the reply is withheld. Almost every confused conversation about browser security comes from somebody assuming the opposite.

This asymmetry exists for a historical reason that is not going away. The web was built around cross-origin embedding from the beginning: an <img> tag pointing at another domain, a <script> tag pulling a library from a CDN, a <link> to a stylesheet, an <iframe> embedding a widget, and a <form> that posts to a payment processor are all cross-origin requests, and all of them long predate any notion of a security policy. Breaking them would break the web, so the policy was drawn around what script can observe rather than around what the network layer may do.

The consequence is a security model with two distinct failure surfaces. Anything that is dangerous purely because the request arrives, a state-changing POST that transfers money, deletes a record, or changes an email address, is not protected by the Same-Origin Policy at all. That entire class of problem is called Cross-Site Request Forgery, and it needs its own defenses, SameSite cookies and CSRF tokens, both covered in this course's fourth phase. Anything that is dangerous because the response content is secret, reading your inbox or your account balance, is protected by the read restriction, and CORS exists to let a server deliberately relax exactly that restriction for callers it trusts.

Within the read restriction, it helps to distinguish three categories of thing a page can do with a cross-origin resource. It can embed it, meaning the browser fetches and renders it but script never sees the bytes: an image renders, a script executes, a stylesheet applies, an iframe displays. It can send to it, meaning a request with side effects goes through: a form POST, an image request with a query string, a fetch in no-cors mode. And it can read it, meaning script receives the actual response body and headers, which is the operation the policy blocks by default and which CORS selectively permits.

Even the embed category leaks more than people expect, which is why later lessons in this course cover read blocking, cross-origin isolation, and resource policy. An image that loads successfully tells the embedding page that the resource exists and what its dimensions are. A script that executes can be observed for errors. Load timing can reveal whether a resource was cached. These are side channels rather than direct reads, and the class of attack built on them is known as XS-Leaks; the modern isolation headers in this course's final phase exist largely to close them.

OperationAllowed cross-origin by default?What this implies
Embed an image, script, stylesheet, iframe, or fontYesThe request is sent and the resource is used, but script cannot read the bytes
Submit an HTML form with POSTYesSide effects occur on the target server, which is the root of CSRF
fetch() and read the response bodyNoBlocked unless the server opts in with CORS response headers
Read another origin's DOM through an iframe referenceNoBlocked; postMessage is the sanctioned alternative
Read another origin's localStorage or IndexedDBNoStorage is partitioned by origin with no opt-in mechanism at all

Worked Example: The Same Request, Sent Two Ways

The example below shows the asymmetry concretely. Both snippets run on https://evil.example and both cause the bank to receive an authenticated request. Only one of them is stopped by the browser, and it is not the dangerous one.

TypeScript
<!-- Running on https://evil.example -->

<!-- 1. SENDING is allowed. The browser issues a cross-origin POST with the
     user's bank.example cookies attached, and the server processes it.
     Nothing in the Same-Origin Policy prevents this. -->
<form action="https://bank.example/transfer" method="POST" id="f">
  <input type="hidden" name="to" value="attacker">
  <input type="hidden" name="amount" value="5000">
</form>
<script>document.getElementById('f').submit();</script>

<!-- 2. READING is blocked. The request below is also sent and also
     processed by bank.example, but the browser refuses to let this
     script see the reply. -->
<script>
fetch('https://bank.example/api/balance', { credentials: 'include' })
  .then(r => r.text())   // never reached
  .catch(e => console.log(e));
// Console: "Access to fetch at 'https://bank.example/api/balance' from
// origin 'https://evil.example' has been blocked by CORS policy: No
// 'Access-Control-Allow-Origin' header is present on the requested resource."
</script>
A cross-origin request crossing the browser boundary in full and reaching the server, with the response stopped on the way back to script, showing why side effects still occur and why CSRF is a separate problem from CORS.
A cross-origin request crossing the browser boundary in full and reaching the server, with the response stopped on the way back to script, showing why side effects still occur and why CSRF is a separate problem from CORS.

Real-World Examples

  • The 2008 academic paper "Robust Defenses for Cross-Site Request Forgery" by Adam Barth, Collin Jackson, and John Mitchell documented live CSRF vulnerabilities in production systems including the New York Times, ING Direct, YouTube, and MetaFilter, with the ING Direct flaw allowing an attacker to transfer funds out of a victim's account purely by getting them to visit a page; every one of those attacks worked because the Same-Origin Policy never blocked the request from being sent in the first place.
  • A widely reported Gmail vulnerability in 2007 allowed an attacker to add a mail filter to a victim's account through a forged cross-origin request, silently forwarding all incoming mail to an address the attacker controlled; the attacker never read a single response from Gmail, because they did not need to, which illustrates precisely why the read restriction alone is not a defense against state-changing requests.
Common PitfallReading a cross-origin response is blocked by default; sending a cross-origin request is not. Confusing the two leads teams to assume CORS protects their API from forged requests, when in reality CORS governs only whether a browser hands a response to script, and the forged request has already been received and processed by the server regardless.

Review Questions & Answers

A CORS policy controls whether the browser releases the response to the calling script. It does not prevent the request from being dispatched or from reaching the server with the user's cookies attached, so any state-changing effect of that request still occurs. A simple cross-origin form POST or a fetch in no-cors mode will be processed by the server normally; the attacker simply never sees the reply, which for a transfer, deletion, or settings change is irrelevant to them. CSRF must be blocked with SameSite cookies, CSRF tokens, or origin checks performed by the server itself.

Cross-origin sending is structurally load-bearing for the web. Images, scripts, stylesheets, fonts, iframes, and form posts to third parties all predate any security policy and all depend on a browser being willing to issue a request to one origin from a document served by another. A policy that blocked cross-origin requests outright would break essentially every page on the internet, so the boundary was drawn at the point where it could be enforced without breaking compatibility, namely what script is permitted to observe.

Embedding means the browser fetches the resource and uses it for its intended purpose, rendering an image, executing a script, applying a stylesheet, without ever exposing the raw bytes to the embedding page's JavaScript. Reading means handing the response body and headers to script. The browser allows embedding because the resource's content stays inside the browser's own rendering machinery, where the embedding page cannot extract it, while reading would put another origin's private data directly into attacker-controlled code.

Embedding still exposes observable metadata even when it hides content. Whether a load succeeds or fails, how long it takes, what dimensions an image has, whether a script throws an error, and how much memory or processing a resource consumes are all measurable by the embedding page, and each of those signals can be correlated with private state, for example whether a particular user is logged in or whether a search returned results. XS-Leaks are attacks built entirely from such side channels, and closing them is the motivation behind the COOP, COEP, and CORP headers covered in this course's final phase.