Sub topic 3
Enabling Shared Memory in Browsers with Cross-Origin Isolation
This chapter assumes you know how HTTP response headers work and have already created a SharedArrayBuffer and posted it to a worker. In a browser, that code throws a ReferenceError or fails to share memory unless the page is cross-origin isolated. This chapter explains why the requirement exists and how to meet it.
Why browsers gate shared memory
Shared memory lets one thread increment a counter in a tight loop while another reads it. That gives a timer precise to a few nanoseconds. Spectre-style attacks need a timer that precise to measure cache effects and read memory they should not see. To limit the risk, browsers expose SharedArrayBuffer only when the page's process holds no cross-origin data that it hasn't been allowed to load. Cross-origin isolation is the guarantee that the process meets that condition.
The two headers
Serve the top-level document with both of these headers:
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp| Header | What it does |
|---|---|
| `Cross-Origin-Opener-Policy: same-origin` | Puts the page in its own browsing context group, so cross-origin windows it opens, or that opened it, can't keep a reference to it. |
| `Cross-Origin-Embedder-Policy: require-corp` | Blocks any cross-origin subresource that does not opt in through CORS or a `Cross-Origin-Resource-Policy` header. |
Cross-Origin-Embedder-Policy: credentialless also works in Chromium and Firefox. It loads no-CORS cross-origin resources without cookies instead of blocking them.
A local server that sends the headers
This Node.js server sets both headers on every response, worker scripts included. Run it next to an index.html and a worker.js.
const http = require('node:http');
const fs = require('node:fs');
const path = require('node:path');
const types = { '.html': 'text/html', '.js': 'text/javascript' };
http.createServer((req, res) => {
const urlPath = req.url === '/' ? '/index.html' : req.url.split('?')[0];
const file = path.join(__dirname, path.normalize(urlPath));
fs.readFile(file, (err, data) => {
if (err) {
res.writeHead(404);
res.end('Not found');
return;
}
res.writeHead(200, {
'Content-Type': types[path.extname(file)] || 'application/octet-stream',
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'require-corp',
});
res.end(data);
});
}).listen(8080, () => console.log('http://localhost:8080'));Isolation also requires a secure context. http://localhost counts as one. Any other host needs HTTPS.
Detecting isolation at runtime
Check crossOriginIsolated before you create shared memory, so a missing header produces a clear message instead of a confusing failure:
if (!globalThis.crossOriginIsolated) {
console.warn('SharedArrayBuffer is unavailable: page is not cross-origin isolated');
}Common errors
- Headers on the wrong response. The headers must be on the HTML document. Setting them only on scripts does nothing.
- Third-party resources break. Under
require-corp, images, scripts, and iframes from CDNs or ad networks fail to load unless they sendCross-Origin-Resource-Policy: cross-originor are fetched with CORS (crossoriginattribute plusAccess-Control-Allow-Origin). Look in the console for errors about blocked resources. - OAuth or payment popups stop working. COOP
same-origincuts the link between your page and popups from other origins, sowindow.openermessaging fails. Move those flows to a page that is not isolated, or check whethersame-origin-allow-popupsfits your case. That value alone does not isolate the page. - Embedded iframes. A cross-origin iframe must itself opt in, and the parent must grant it
allow="cross-origin-isolated"for the iframe to be isolated as well.
Exercise
Your page sends COOP same-origin and COEP require-corp, and crossOriginIsolated is true, but a logo image from https://cdn.example.com no longer appears. Give two fixes.
Show answer
Have the CDN send Cross-Origin-Resource-Policy: cross-origin on the image, or load it with CORS by adding crossorigin="anonymous" to the img tag and having the CDN send Access-Control-Allow-Origin. A third option is to switch to COEP credentialless where the browser supports it.
Exercise
Why does setting the two headers only on worker.js leave SharedArrayBuffer unavailable on the page?
Show answer
Isolation is decided by the top-level document's response. The HTML document itself must carry COOP and COEP for the page and its workers to be cross-origin isolated.