Security Requirements for SharedArrayBuffer
You'll learn why using SharedArrayBuffer requires strict security headers and what threat it mitigates.
When JavaScript introduced SharedArrayBuffer for sharing memory between threads, it inadvertently created a high-precision timing channel that attackers could exploit for Spectre attacks—a hardware vulnerability that allows reading sensitive data from other processes by measuring execution timing with extreme precision.
The Spectre Vulnerability Connection
Spectre attacks rely on measuring tiny timing differences (nanoseconds) to infer secret data. Normally, JavaScript's timing APIs are deliberately coarse to prevent this. However, SharedArrayBuffer gave attackers a workaround: one thread can increment a counter in shared memory while another thread reads it, creating a perfect high-resolution timer that bypasses browser protections.
Required Security Headers
To use SharedArrayBuffer, your web page must be served with two HTTP headers:
-
Cross-Origin-Opener-Policy: same-origin(COOP) – Isolates your page in its own browsing context, preventing other origins from directly accessing it. -
Cross-Origin-Embedder-Policy: require-corp(COEP) – Ensures all resources loaded by your page explicitly opt-in to being embedded, preventing cross-origin information leaks.
These headers create a secure context where the browser can guarantee your page is sufficiently isolated from other origins, eliminating the Spectre attack surface.
What Happens Without Them?
If you try to create a SharedArrayBuffer without these headers, modern browsers will throw an error or return undefined, preventing the security risk entirely.
Key Takeaway: SharedArrayBuffer requires COOP and COEP headers to create a secure, cross-origin isolated environment that prevents Spectre-style timing attacks by ensuring your page cannot leak sensitive information to or from other origins.