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

Cross-Origin Embedding and Opaque Responses

On this page
  1. Learning Objective
  2. Concept Explanation
  3. Worked Example: A JSON Hijacking Target and Its Three Fixes
  4. Real-World Examples
  5. Review Questions & Answers

Learning Objective

Explain what an opaque response is, how the classic JSON hijacking attack extracted data through a <script> tag despite the Same-Origin Policy, and how modern browsers use Cross-Origin Read Blocking and Opaque Response Blocking to stop sensitive bytes from ever entering the renderer process.

Concept Explanation

When a page embeds a cross-origin resource that it is not allowed to read, the browser gives the page an opaque response: a response object whose status reads as 0, whose headers are empty, and whose body cannot be accessed. Opaque responses are what fetch returns in no-cors mode, and they are the mechanism behind every "you can use it but you cannot look at it" case. They are genuinely useful, a service worker can cache an opaque response and serve it later without ever inspecting it, but they are also the reason a request that leaks nothing to script can still be sent with full credentials.

The <script> tag is the historically dangerous embedding form because script is not merely fetched, it is executed in the embedding page's own context. That execution is not a read of the response in the Same-Origin Policy's sense, but it produces observable effects in the embedder's JavaScript environment, which is a distinction attackers have exploited for two decades. The family of attacks is called Cross-Site Script Inclusion, or XSSI, and its most famous member is JSON hijacking.

JSON hijacking worked like this. An endpoint returned a bare JSON array of the logged-in user's private data. An attacker included that endpoint with a <script src> tag, so the browser fetched it with the victim's cookies and then evaluated the response as JavaScript. In older engines, a bare array literal at the top level of a script could be captured, originally by overriding the Array constructor and later by defining setters on Object.prototype, so the attacker's page could read the contents of a response the Same-Origin Policy was supposedly protecting. Jeremiah Grossman published exactly this attack against Gmail's contact list endpoint in January 2006, and the industry response, prefixing JSON responses with an infinite loop or a sequence such as a right-parenthesis-and-brace guard so that evaluating them as script fails, is why Google's JSON APIs still emit a guard prefix today.

That kind of defense is per-endpoint and easy to forget, so browsers eventually moved the protection into the network stack itself. Cross-Origin Read Blocking, introduced alongside Site Isolation, inspects cross-origin responses that are being delivered for an embedding context and refuses to hand the bytes to the renderer process at all when the response looks like data rather than a legitimately embeddable resource. The heuristic combines the declared Content-Type, an X-Content-Type-Options: nosniff header if present, and lightweight content sniffing: an HTML, XML, or JSON response requested as a script or an image is blocked before it reaches the process, and the embedding page sees an empty result. Firefox implements the same idea under the name Opaque Response Blocking.

The practical takeaways for an application developer are concrete. Always send X-Content-Type-Options: nosniff, because without it the browser may sniff a response into a type the server never intended and the read blocking heuristic becomes far weaker. Always serve JSON with an accurate application/json Content-Type. Never return sensitive data from an endpoint that responds to a simple GET with no unguessable parameter, because any page anywhere can cause that GET to be made with the user's cookies. And prefer the Cross-Origin-Resource-Policy header, covered in this course's final phase, which lets a resource declare outright that it must not be loaded cross-origin at all.

Embedding formWhat the embedder can observeHistoric leak
<img src>Load success or failure, natural width and height, load timingExistence and size oracles used in XS-Leaks
<script src>Everything the script does in the embedder's own JavaScript contextJSON hijacking and other XSSI variants
<link rel=stylesheet>Computed styles applied to the embedder's DOMCSS-based data exfiltration through attribute selectors
<iframe src>Load success, frame count, and navigation side effects onlyFraming and XS-Leak oracles, mitigated by frame-ancestors
fetch(url, {mode: 'no-cors'})Nothing; the response is opaque with status 0 and no headersStill sends the request with credentials, so side effects occur

Worked Example: A JSON Hijacking Target and Its Three Fixes

The vulnerable endpoint below is dangerous not because of what it returns but because of how it can be requested: a bare GET, authenticated by ambient cookies, returning a top-level array. Each fix closes a different part of the chain.

HTTP
// VULNERABLE: a top-level JSON array, returned on a plain GET, with the
// user's session cookie as the only authentication
GET /api/contacts HTTP/1.1
Host: mail.example
Cookie: session=...

HTTP/1.1 200 OK
Content-Type: application/json

[{"name":"Alice","email":"alice@example.com"}, ...]

// The attack page simply embeds it as script:
// <script src="https://mail.example/api/contacts"></script>

// FIX 1 - guard prefix, so evaluating the body as JavaScript throws.
// Google's JSON APIs still do this; the client strips the prefix.
HTTP/1.1 200 OK
Content-Type: application/json
X-Content-Type-Options: nosniff

)]}',
[{"name":"Alice","email":"alice@example.com"}]

// FIX 2 - never return a bare top-level array; wrap it in an object so it
// is not a valid standalone JavaScript expression statement.
{"contacts": [{"name":"Alice"}]}

// FIX 3 - require a header the browser cannot attach cross-origin without
// a preflight, which turns the request into a CORS-governed one.
GET /api/contacts HTTP/1.1
X-Requested-With: XMLHttpRequest
A cross-origin response arriving at the browser and being routed three ways: rendered as an embeddable resource, returned to script as an opaque response, or dropped by Cross-Origin Read Blocking before it reaches the renderer process at all.
A cross-origin response arriving at the browser and being routed three ways: rendered as an embeddable resource, returned to script as an opaque response, or dropped by Cross-Origin Read Blocking before it reaches the renderer process at all.

Real-World Examples

  • In January 2006 Jeremiah Grossman published a JSON hijacking attack against Gmail, in which a malicious page could include Gmail's contact-list endpoint with a script tag and read the victim's entire address book by overriding the Array constructor before the response was evaluated; Google's fix, prefixing JSON responses with a guard sequence that makes them invalid as standalone JavaScript, is still visible in Google API responses today.
  • Chrome introduced Cross-Origin Read Blocking as part of the Site Isolation work shipped in Chrome 67 in May 2018, explicitly as a Spectre mitigation: because speculative execution attacks could read anything in a renderer process's address space, the team concluded that keeping sensitive cross-origin data out of the process entirely was a stronger guarantee than any in-process check, and Firefox later shipped an equivalent under the name Opaque Response Blocking.
Best PracticeSend X-Content-Type-Options: nosniff on every response and an accurate Content-Type on every API response. Without nosniff, a browser may sniff a JSON or HTML response into a type the server never intended, which both defeats read blocking heuristics and reopens content-type confusion attacks that the correct header closes for free.

Review Questions & Answers

It never performed a read in the policy's sense. The attacker embedded the endpoint with a script tag, which the browser fetches with the victim's cookies and then executes in the attacker's own JavaScript context. By pre-arming that context, originally by replacing the Array constructor and later by installing setters on Object.prototype, the attacker could capture the values as the response was evaluated. The data arrived as executing code, not as a readable response body, which is the loophole the policy did not cover.

Per-endpoint defenses such as guard prefixes and wrapper objects depend on every server author knowing about and applying them consistently, which does not scale. More importantly, after Spectre the threat model changed: any sensitive data that merely enters a renderer process is potentially readable through speculative execution side channels, regardless of what the JavaScript-level policy says. Blocking the bytes before they cross the process boundary is a guarantee the renderer cannot undermine.

An opaque response is what a no-cors fetch returns: a response object with status 0, no readable headers, and an inaccessible body. It successfully prevents the calling script from seeing any content. It is not a complete defense because the request itself was still dispatched, with the user's cookies attached, so any state change the server performs in handling that request still happens, and coarse signals such as whether the load succeeded and how long it took remain observable.

Read blocking decides whether to withhold a response partly from its declared Content-Type and partly from sniffing the bytes. Without nosniff, a browser is permitted to override a declared type based on content, which both creates content-type confusion attacks in its own right and makes the blocking decision less reliable, since a response the server labelled as data could be treated as a legitimately embeddable type. With nosniff, the declared type is authoritative, so the block decision is deterministic.