The widget on the right is locked down with default-src 'none' so it can't touch the network at all. This page opens a WebSocket and transfers the readable and writable streams straight into the widget (using WebSocketStream on Chromium, or a standard WebSocket wrapped in streams elsewhere). The widget can talk to that one socket without ever learning the URL or opening another, and closing the socket cuts it off instantly.
When you'd use this
A collaborative app runs third-party room widgets inside default-src 'none'iframes so they can't exfiltrate data over the internet, yet a poll widget still needs live votes from one specific backend channel. Open a narrowly scoped WebSocket on the host, lend its readable/writable streams to the iframe, and close the socket when the widget closes.
Code
const socket = newWebSocketStream(url); // Chromium only; keep it so close() ends the loanconst { readable, writable } = await socket.opened;
widget.postMessage({ readable, writable }, widgetOrigin, [readable, writable]);
// the widget, which has no network access of its ownawait writable.getWriter().write("hello");
const { value } = await readable.getReader().read(); // the server's answer
In practice
// chat app lending a poll-only WebSocket to a zero-network plugin iframeconst socket = newWebSocketStream(pollSocketUrl); // server scopes this endpoint to pollsconst { readable, writable } = await socket.opened;
widget.postMessage({ readable, writable }, widgetOrigin, [readable, writable]);
// inside the CSP-locked plugin (default-src 'none')const writer = writable.getWriter(), reader = readable.getReader();
await writer.write(JSON.stringify({ vote: "Tuesday" }));
for (;;) {
const { value, done } = await reader.read().catch(() => ({ done: true }));
if (done) break;
show(JSON.parse(value));
}
// removing the plugin closes the socket immediately
removeButton.onclick = () => socket.close();
The sandboxed widget has default-src 'none' in its CSP, so it has zero network access. Once this page hands it the two streams, it can talk over that one specific connection without ever seeing the server URL or opening a second socket. Closing the socket on the host page cuts the widget off immediately.
Step by step
What each press of Next does in the demo above.
Widget: try to connect by itself. The widget tries opening a WebSocket directly — its CSP (default-src 'none') blocks all network access.
Open a connection to the server. This page opens a WebSocket.
Lend the widget the connection. Transfers the readable and writable streams of that connection straight into the locked-down widget.
Widget: say hello to the server. The widget talks to the server through the lent streams without ever seeing the URL.
Close the connection. This page closes the socket, cutting the widget off immediately.
What you can build with it
Built and running below.
Give a logger the incoming half of a connection only
Read-only network taps by transferring only the ReadableStream.
A WebSocket seen as streams has two halves. This page keeps the half that speaks and gives a logger only the half that listens. The logger has no network access of its own. It records everything the server says, and it has no way to send the server anything, because it was never given anything to write into.
Open a connection. This page opens a WebSocket to the server.
Give the logger the listening half. Only the readable stream is handed over.
This page: say something. This page speaks. The server's answer goes to the logger.
Logger: try to speak. The logger has nothing to write into.
Sign in on the page, then hand the open session to a worker
Authenticating a session in a privileged host page and delegating the open stream to a worker.
The page signs in to an account service on another origin and gets back a port: the open session. The page does not need it. Its worker does, to keep the balance fresh in the background. So the page posts the port to the worker. The session is not copied and nobody signs in twice. The worker now holds it, and the page's own variable holds a dead port.
Code
const session = awaitsignIn(password); // a port to the account service
worker.postMessage({ session }, [session]); // the worker has it now
session.postMessage({ ask: "balance" }); // goes nowhere: this end has left
Step by step
What each press of Next does in this demo.
Sign in. The page sends a password and gets a session back, as a port.
Hand the session to my worker. The port moves to the worker, which starts asking for the balance.
Page: try to use the session. The page still has its variable. The port behind it has gone.
Have the widget try connecting by itself first to verify CSP blocks it.
Open the socket, lend the streams, send a message, then close the socket from the host page.
Gotchas
WebSocketStream is currently Chromium-only; elsewhere you wrap a normal WebSocket in streams on the host page.
Scope what the server accepts on that socket — lending a fully authenticated user socket lets the widget issue anything that socket permits.
Measured
Chromium 157: a frame locked down with default-src 'none' exchanges live messages over lent WebSocket streams. Other browsers are being filled in from the lab's results.
The web platform features this page mentions, with what each one is, its documentation on MDN and the specification that defines it.
WebSocket
A two-way connection between a page and a server that stays open. MDNWebSockets Standard
WebSocketStream
A WebSocket whose messages are read and written as streams, so a slow reader slows the sender. Chromium only at the time of writing. MDN
<iframe>
An element that shows another page inside this one. The inner page can be on a different origin, and the two can only talk by posting messages. MDNHTML Standard
ReadableStream
A source of data that is read a piece at a time. It can be moved to another page or worker. MDNStreams Standard
WritableStream
A destination that data is written to a piece at a time. It can be moved to another page or worker. MDNStreams Standard