# csp & iframe sandbox studio

- Status: `experiment`
- Track: [browser containment & sandbox boundaries](https://aifoc.us/labs/browser-containment/)
- Layer: 1. Execution Containment & Browser Security
- Updated: 2 December 2026
- Live demo: https://csp-iframe-sandbox-studio.kinlan-test.workers.dev
- Source: https://github.com/PaulKinlan/csp-iframe-sandbox-studio

## Inspired by

- [the browser is the sandbox](https://aifoc.us/the-browser-is-the-sandbox/)
- [shipping a prompt](https://aifoc.us/shipping-a-prompt/)
- [extensible web apps](https://aifoc.us/extensible-web-apps/)

## Why this matters

Arguments about browser sandboxing often rely on assumptions about double-nested iframes, `<meta>` CSP tags, `<iframe csp>` (CSPEE), and `blob:` Workers without testing them across two separate web origins. Client APIs such as `navigator.sendBeacon()` can even return `true` when CSP silently cancelled the request. This studio mounts a separate companion origin (`csp-sandbox-buffer-origin`) and a SQLite Durable Object canary so every header, framing pattern, `MessagePort` bridge, and Worker setup is measured against real browser enforcement.

## What this opens up

Clear architectural decisions for plugin hosts, untrusted HTML previews, and generated UI panels in standard browser tabs: use a faster single cross-origin iframe with strict HTTP headers when you control the origin, a double-iframe buffer when wrapping uncontrolled third-party pages, and a narrowed revocable `MessagePort` for safe host calls.

## How the tests work

1. In **1. Policy & Sandbox Builder**, choose one of the six presets (or customize all 9 `<iframe sandbox>` flags, CSP directives, `Connection-Allowlist`, and `<iframe csp>`) and click **Run Active Preset in 14-Vector Test**.
2. In **2. 14-Vector Leak Test**, run all 14 network, subframe, navigation, storage, and DOM breakout probes and click **Compare Single vs Double Latency**; zero hits in **DO Canary** confirms the boundary held outside the tab.
3. Switch to **3. Capability Bridge (Hawser)** to fire allowed reads, disallowed `write()` calls, `constructor` prototype walks, `AbortSignal` cancellations, and instant port revocation across a narrowed Hawser `MessagePort`, an unfiltered proxy, and raw `window.postMessage`.
4. Open **4. Web Worker Matrix (4 Setups)** and click **Run All 4 Worker Topologies** to compare `fetch()`, `WebSocket`, `indexedDB`, OPFS, `BroadcastChannel`, and `importScripts()` across same-origin HTTP, same-origin `blob:`, opaque-iframe `blob:`, and cross-origin buffer Workers.


## Hypothesis

- **Claim:** Pairing a distinct buffer origin (`csp-sandbox-buffer-origin`), strict HTTP CSP (`sandbox` + `frame-src 'none'` + `connect-src 'none'`), and a prototype-locked, method-allowlisted, revocable `MessagePort` (`hawser.js` pattern) provides verifiable network and capability containment across both single-frame and double-iframe topologies, whereas `blob:` Workers inherit parent network policy unless isolated behind an opaque origin or separate Worker HTTP CSP.
- **Confirms it:** Locked configurations record zero hits in the `ContainmentTelemetry` Durable Object across all 14 breakout and leak probes; the Hawser `MessagePort` bridge blocks `__proto__`/`constructor`/`prototype` traversal and stops immediately on `port.close()`; and the 4-topology Worker matrix clearly separates HTTP-CSP and cross-origin isolation from same-origin `blob:` inheritance.
- **Bounds or disproves it:** Double-iframe `<meta>` CSP injection fails to tighten restrictions already stripped by an outer sandbox, `<iframe csp>` CSPEE is ignored in non-Chromium engines, or an unfiltered `MessagePort` proxy permits prototype/method escape across the buffer boundary.


## Under the hood

[CSP & Iframe Sandbox Studio](https://csp-iframe-sandbox-studio.kinlan-test.workers.dev) ([GitHub](https://github.com/PaulKinlan/csp-iframe-sandbox-studio)) runs across two separate Cloudflare Worker origins—the primary host Worker (`csp-iframe-sandbox-studio`) and a companion cross-origin buffer Worker (`csp-sandbox-buffer-origin`, defined in `src/buffer-worker.ts` and `wrangler.buffer.jsonc`)—paired with a SQLite Durable Object (`ContainmentTelemetry`) that records real canary hits and `Reporting-Endpoints` violation posts.

The four workbench benches exercise distinct parts of the browser security model: (1) a live compiler for all 9 HTML `sandbox` flags, HTTP CSP directives (including `CSP: sandbox` and Trusted Types), `<iframe csp>` [CSPEE](https://w3c.github.io/webappsec-cspee/), `Connection-Allowlist`, `COOP`/`COEP`/`CORP`, and 6 pushback presets; (2) a 14-vector breakout harness with single-frame vs. double-iframe mount latency timing; (3) a cross-origin `DemoVault` bridge pitting a prototype-locked, method-allowlisted, revocable `MessagePort` (`allow: ["read", "slowRead"]`, `FORBIDDEN = new Set(["__proto__", "constructor", "prototype"])`, `AbortSignal`, instant `port.close()`) against an unfiltered proxy and raw `window.postMessage`; and (4) a 4-topology Worker matrix comparing same-origin HTTP Workers (`/workers/locked-worker.js`), same-origin `blob:` Workers, opaque-iframe `blob:` Workers, and cross-origin buffer Workers (`/buffer-worker.js`).


## Key terms & further reading

- [<iframe sandbox> flags](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/iframe#sandbox) — Nine opt-in tokens that lock an embedded iframe into a throwaway origin unless relaxed (avoid pairing `allow-scripts` with `allow-same-origin` on your own origin).
- [Content Security Policy (CSP Level 3)](https://w3c.github.io/webappsec-csp/) — An immutable HTTP response header controlling which scripts, nested frames (`frame-src`), images, and network connections a document or Worker may perform.
- [CSP Embedded Enforcement (CSPEE)](https://w3c.github.io/webappsec-cspee/) — The Chromium-only `<iframe csp="...">` attribute that asks an embedded third-party page to agree to a minimum CSP before rendering.
- [Connection-Allowlist (WICG)](https://wicg.github.io/connection-allowlists/) — A single deny-by-default HTTP header that blocks outbound connections—including redirects, prefetches, and WebRTC—unless their URL matches an explicit allowlist.
- [MessagePort capability bridge (Hawser)](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort) — A private two-way channel transferred into a sandboxed frame that exposes only allowlisted methods, blocks prototype-chain escapes, and revokes access immediately via `port.close()`.
- [Cloudflare Durable Object canary](https://developers.cloudflare.com/durable-objects/) — A server-side SQLite state store recording real arriving HTTP/WebSocket requests and CSP violation reports so containment is verified outside the browser.

## Open questions to iterate on

- When does a single cross-origin frame plus a narrowed `hawser.js` `MessagePort` beat a double-iframe topology on mount latency and operational simplicity?
- Which `Connection-Allowlist` directives supersede `connect-src 'none'` for non-fetch channels like prefetch and WebRTC across Chromium milestones?

## Iteration log

- **2026-12-02:** Deployed dual-origin Cloudflare Worker studio (`csp-iframe-sandbox-studio` + `csp-sandbox-buffer-origin`) with 9-flag sandbox & CSPEE configurator, 6 pushback presets, 14-vector `ContainmentTelemetry` DO harness, `hawser.js`-pattern vs. unfiltered `MessagePort` comparator, and 4-topology Worker Isolation Matrix.

## Primitives tested

- `<iframe sandbox>`
- `CSP Level 3`
- `CSP Embedded Enforcement (<iframe csp>)`
- `Connection-Allowlist`
- `MessagePort`
- `Dedicated Worker CSP`
- `Reporting-Endpoints`
- `Durable Object SQLite`

## Connected labs in other tracks

- [agent containment lab](https://aifoc.us/labs/browser-containment/agent-containment-lab/) — Tests 7 single-origin containment tiers, `Connection-Allowlist` rules, and CaMeL-style taint tracking.
- [slice capability mesh](https://aifoc.us/labs/browser-containment/slice-capability-mesh/) — Isolates OPFS storage slices behind opaque-origin iframes and non-extractable AES-GCM keys.
- [malleable worker plugin shell](https://aifoc.us/labs/agent-meshes/malleable-worker-shell/) — Applies Worker isolation topologies to user-editable typed JSON UI slot plugins.
- [<web-island> multi-origin host shell](https://aifoc.us/labs/hypermedia-and-islands/web-island-host-shell/) — Composes real cross-origin widgets over `MessageChannel` with `ResizeObserver` sizing.
- [messageport intent & acp bridge](https://aifoc.us/labs/agent-bridges-and-discovery/messageport-intent-acp-bridge/) — Uses attenuated `MessagePort` handoffs for single-tool web intents and ACP session brokering.

---
Canonical: https://aifoc.us/labs/browser-containment/csp-iframe-sandbox-studio/
