SharedArrayBuffer is not defined: fixing cross-origin isolation
Our in-browser video trimmer built on threaded WebAssembly died with SharedArrayBuffer is not defined in production. The page was not cross-origin isolated. Adding COOP and COEP on that route fixed it, and broke two embeds we then had to repair.
We added a clip trimmer to our video library: users cut a section from an uploaded MP4 in the browser before sending it. It used a threaded WebAssembly build of a media library. Locally it worked. On staging and production the trimmer button spun forever, and the console showed:
Uncaught ReferenceError: SharedArrayBuffer is not defined
In Safari the same page logged the same ReferenceError. Firefox was identical.
Reproducing it
Open the console on any page served without special headers:
typeof SharedArrayBuffer; // "undefined" on a normal page
self.crossOriginIsolated; // false
On our dev server, the Vite config already sent two headers we had copied from the library's README months before. Nobody had added them to the production nginx config.
What we ruled out
Browser support. All current engines support
SharedArrayBuffer; the constructor is hidden, not missing.HTTPS. Cross-origin isolation requires a secure context. Production was HTTPS.
Bundling. We suspected the production build stripped a polyfill. There is no polyfill for shared memory; the bundle was fine.
Worker scope. The error appeared on the main thread before any worker started.
Root cause
Browsers expose SharedArrayBuffer only to documents that are cross-origin isolated. That requires the document to be served with Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp (or credentialless where supported). Our dev server sent both; production sent neither, so crossOriginIsolated was false and the constructor was not defined.
What is happening under the hood
After the Spectre class of CPU side-channel attacks, browsers removed SharedArrayBuffer in 2018, because shared memory plus a worker incrementing a counter makes a high-resolution timer. It came back behind a guarantee: the page must prove that no cross-origin resource shares its process unless that resource opted in.
COOP
same-originputs the page in its own browsing context group. Cross-origin windows it opens, or that open it, lose theirwindow.openerlink.COEP
require-corpmakes the page refuse to load cross-origin subresources unless they sendCross-Origin-Resource-Policyallowing it or are loaded with CORS.COEP
credentiallessis a softer variant: cross-origin no-CORS requests are sent without cookies, so they can load without CORP. Chromium and Firefox support it; check Safari support for your target versions before relying on it.
When both conditions hold, self.crossOriginIsolated is true, SharedArrayBuffer is defined, and performance.now() gets finer resolution.
Environment
Component | Version / value |
|---|---|
Chrome | 142 |
Firefox | 145 |
Safari | 26.1 |
Web server | nginx 1.28 in front of a Node 22 app |
Dev server | Vite 7 with COOP/COEP set in |
Feature | Threaded WebAssembly media library, 4 worker threads |
Minimal reproduction
# production: nothing special
location / {
try_files $uri /index.html;
}
// app code
const memory = new WebAssembly.Memory({ initial: 256, maximum: 4096, shared: true });
// shared: true needs SharedArrayBuffer; throws on a non-isolated page
Who is affected
Threaded WebAssembly builds (Emscripten pthreads, Rust with atomics), audio worklets sharing ring buffers, and any code calling
new SharedArrayBuffer().Pages that also embed third-party iframes, images, scripts or fonts, which isolation may block.
Pages that open OAuth or payment popups and expect
window.openermessaging.
The fix
Send the headers only on the routes that need shared memory. We isolated the trimmer at /studio/* and left the rest of the site alone.
location /studio/ {
add_header Cross-Origin-Opener-Policy "same-origin" always;
add_header Cross-Origin-Embedder-Policy "require-corp" always;
try_files $uri /studio/index.html;
}
# Static assets loaded by /studio must also be allowed under require-corp.
location /assets/ {
add_header Cross-Origin-Resource-Policy "same-origin" always;
expires 30d;
}
Same-origin assets load fine under require-corp. Cross-origin assets are the hard part.
Repairing the embeds that broke
Turning on COEP broke two things immediately:
Thumbnails from our CDN subdomain (
media.example.com). The browser blocked them with:net::ERR_BLOCKED_BY_RESPONSE.NotSameOriginAfterDefaultedToSameOriginByCoepFix: the CDN now sends
Cross-Origin-Resource-Policy: cross-originon image responses. As an alternative, load them withcrossorigin="anonymous"and serve CORS headers.The "Sign in with provider" popup. COOP
same-originseveredwindow.opener, so the popup could not post the result back. Fix: we moved sign-in out of/studio(users authenticate before they reach it), so the isolated route never opens the popup. Where you cannot do that,same-origin-allow-popupskeeps opener access for popups you open, but it does not make the page cross-origin isolated, so it does not solve theSharedArrayBufferrequirement on the same page.
Third-party iframes need their own COEP-compatible headers, or the credentialless attribute on the iframe in Chromium. Our help-widget iframe did not support either, so we removed it from /studio.
Rolling out safely with report-only
Before enforcing, we ran COEP in report-only mode for a week on /studio:
add_header Cross-Origin-Embedder-Policy-Report-Only 'require-corp; report-to="coep"' always;
add_header Reporting-Endpoints 'coep="https://example.com/reports/coep"' always;
The reports listed every resource that would have been blocked, which is how we found the help widget.
Alternatives and trade-offs
Single-threaded WebAssembly build. No headers needed, slower trims. We keep it as a fallback when
crossOriginIsolatedis false.credentiallessCOEP. Fewer embed breaks in browsers that support it, but behaviour differs across engines, so test each one.Server-side trimming. Upload first, trim on the server. Removes the browser constraint at the cost of bandwidth and compute.
How to test the fix
curl -sI https://example.com/studio/ | grep -i cross-origin
# cross-origin-opener-policy: same-origin
# cross-origin-embedder-policy: require-corp
test('studio is cross-origin isolated', async ({ page }) => {
await page.goto('/studio/');
expect(await page.evaluate(() => self.crossOriginIsolated)).toBe(true);
expect(await page.evaluate(() => typeof SharedArrayBuffer)).toBe('function');
});
Confirming in production
The trimmer reports which build it loaded:
trimmer_build{variant="threads"} 97.6%
trimmer_build{variant="single"} 2.4% (older browsers, embedded webviews)
trim_duration_s_p50 threads: 3.1 single: 9.8
Chrome DevTools, Application panel, Frames section shows "Cross-Origin Isolated: Yes" for the studio frame, along with the COOP and COEP values.
Regression guard
The Playwright test above runs against staging after every deploy.
A header check in our deploy pipeline fails if
/studio/lacks either header.COEP reports stay enabled in report-only mode on the rest of the site, so new embeds that would break isolation show up before we extend it.
Security implications
Cross-origin isolation is a security feature, and the fix strengthens the studio page: other origins cannot hold a reference to it through window.opener, and only resources that opted in can load into it. Do not "fix" blocked embeds by proxying third-party content through your own origin to dodge CORP; that defeats the protection. Keep isolation scoped to the routes that need it, because COOP breaks popup-based flows such as OAuth and payments.
Prevention checklist
Put COOP and COEP in production config, not only in the dev server.
Scope isolation to routes that need shared memory.
Roll out COEP in report-only mode first.
Serve
Cross-Origin-Resource-Policyon your own CDN responses.Feature-detect with
crossOriginIsolatedand ship a fallback.
References
MDN, SharedArrayBuffer security requirements: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements
MDN, Cross-Origin-Embedder-Policy: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Embedder-Policy
MDN, Cross-Origin-Opener-Policy: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Opener-Policy
MDN, crossOriginIsolated: https://developer.mozilla.org/en-US/docs/Web/API/Window/crossOriginIsolated
web.dev, Making your website cross-origin isolated using COOP and COEP: https://web.dev/articles/coop-coep
Add a label
Embed this post
<iframe src="https://hibug.me/sharedarraybuffer-undefined-cross-origin-isolation" width="700" height="420" frameborder="0" loading="lazy"></iframe>
I build backend services in Go, Node.js and Python, mostly APIs and workers behind high-traffic products. Over the years I have become the person who gets the "it only fails under load" tickets. Those bugs are almost always about concurrency or resource limits: goroutines that never exit, a blocked event loop, a connection limit nobody knew existed. I write each one up with a small program that reproduces it, an explanation of the runtime behaviour behind it, the fix and the test or metric that proves it, so the next engineer does not need a load test to find it.
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.