HAMZA KAREEM / CONCEPTS hamzawi.dev

Lesson 1552 min read

Cross-Origin Isolation (COOP + COEP)

why SharedArrayBuffer is undefined only in production

Web developmentweb development / security & performance

The symptom

You ship an in-browser video trimmer built on ffmpeg.wasm. It works perfectly on localhost:5173. In production:

Uncaught ReferenceError: SharedArrayBuffer is not defined

Same bundle, same browser, same Chrome version. Nothing in your code changed.

The wrong guesses

  • "Browser support" — no, Chrome/Firefox/Safari all ship SharedArrayBuffer.
  • "HTTPS" — prod is already HTTPS, and localhost is treated as secure anyway.
  • "A build flag stripped it" — it's a global, not an import.

The real difference: SharedArrayBuffer is gated behind cross-origin isolation, and some dev servers set the enabling headers for you while your CDN or nginx does not.

The two headers

Since Spectre, letting one origin share memory with a worker is only allowed if the browser can prove no cross-origin document is in your process and no cross-origin resource leaked into it. You assert that with two response headers on the top-level HTML document:

Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp

COOP severs window.opener links to other origins. COEP says: every subresource must explicitly opt in to being embedded. Both present → window.crossOriginIsolated === true → SharedArrayBuffer, performance.measureUserAgentSpecificMemory(), and unthrottled performance.now() resolution unlock.

The trap that bites on day two

You add the headers, SharedArrayBuffer appears — and your Stripe iframe, Google Fonts and S3 images all go blank. require-corp blocks every cross-origin subresource that doesn't say yes, and a plain 200 is not yes. Each one must send:

Cross-Origin-Resource-Policy: cross-origin

…or be fetched with CORS. Most third parties don't send CORP, so this is where teams revert the whole thing.

The ladder that actually works

  1. Use credentialless instead, which lets no-cors subresources load by stripping cookies from the request:
Cross-Origin-Embedder-Policy: credentialless

Your public images and fonts load again; anything needing credentials still breaks.

  1. For resources you control, add Cross-Origin-Resource-Policy: cross-origin at the edge.
  2. For a third-party iframe you cannot change, mark it as deliberately uncontained:
<iframe src="https://js.stripe.com/..." credentialless></iframe>
  1. Easiest escape hatch: put only the wasm tool on its own isolated route or subdomain, and leave your marketing pages un-isolated.

Verify in two places, not one

# headers actually served?
curl -sI https://app.example.com/editor | grep -i cross-origin

// the only check that matters
console.log(crossOriginIsolated, typeof SharedArrayBuffer)

Checking typeof SharedArrayBuffer alone misleads you: it is also defined in a non-isolated page in some older builds, but new SharedArrayBuffer(8) then throws or the buffer refuses to cross postMessage.

Your exercise

Serve a page with COOP+COEP via a 4-line local server, confirm crossOriginIsolated === true, then add <img src="https://picsum.photos/200">. Watch it fail in the Network panel with net::ERR_BLOCKED_BY_RESPONSE.NotSameOriginAfterDefaultedToSameOriginByCoep. Now switch to credentialless and explain in one sentence why the image loads but a cookie-authenticated avatar URL still would not.