Port Authority

Port patterns / Capabilities and trust boundaries

Lend someone a network connection you opened

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 = new WebSocketStream(url);        // Chromium only; keep it so close() ends the loan
const { readable, writable } = await socket.opened;
widget.postMessage({ readable, writable }, widgetOrigin, [readable, writable]);

// the widget, which has no network access of its own
await 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 iframe
const socket = new WebSocketStream(pollSocketUrl); // server scopes this endpoint to polls
const { 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();

How it works

View a WebSocket as a pair of streams — a ReadableStream for incoming messages and a WritableStream for outgoing ones. WebSocketStream exposes that pair directly, and both streams are transferable.

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.

  1. Widget: try to connect by itself. The widget tries opening a WebSocket directly — its CSP (default-src 'none') blocks all network access.
  2. Open a connection to the server. This page opens a WebSocket.
  3. Lend the widget the connection. Transfers the readable and writable streams of that connection straight into the locked-down widget.
  4. Widget: say hello to the server. The widget talks to the server through the lent streams without ever seeing the URL.
  5. 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.

Code

const { readable, writable } = await new WebSocketStream(url).opened;
logger.postMessage({ readable }, loggerOrigin, [readable]);   // listening half only
const writer = writable.getWriter();                 // this page keeps the voice
writer.write("hello");

Step by step

What each press of Next does in this demo.

  1. Open a connection. This page opens a WebSocket to the server.
  2. Give the logger the listening half. Only the readable stream is handed over.
  3. This page: say something. This page speaks. The server's answer goes to the logger.
  4. 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 = await signIn(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.

  1. Sign in. The page sends a password and gets a session back, as a port.
  2. Hand the session to my worker. The port moves to the worker, which starts asking for the balance.
  3. Page: try to use the session. The page still has its variable. The port behind it has gone.

Already running elsewhere

Things to try

  • 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.

References

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
Content Security Policy
A header that limits what a page may load and where it may connect. MDNContent Security Policy

For more, see Further reading: articles, talks, papers, libraries and the specifications, collected in one place.