Port Authority

Port patterns / Capabilities and trust boundaries

Per-plugin virtual filesystem (Plan 9 namespaces over a port)

Instead of handing every plugin a shared storage API and hoping access control checks never slip, borrow Plan 9's per-process namespaces and WASI preopened directories. Each sandboxed plugin receives a single MessagePort representing only the subtree it's allowed to see: Docs Reader gets /docs read-only, Drafts Agent gets /drafts read-write, and neither can traverse into /secrets. Closing a plugin's port revokes its mount on the spot without touching any other plugin.

When you'd use this

A browser-based IDE, note editor or agent host runs multiple third-party plugins against a shared workspace and wants least-privilege file access without maintaining a brittle central ACL table. Minting one confined MessagePort per subtree means each plugin literally can't name files outside its mount, and revoking one plugin's access is a single port.close().

Code

// Host: mint a confined mount port for one subtree and mode ("ro" | "rw")
function mount(root, mode) {
  const { port1, port2 } = new MessageChannel();
  port1.onmessage = ({ data: { id, op, path, content } }) => {
    const full = resolveInside(root, path); // rejects "..", symlinks & sibling prefixes
    if (!full) return port1.postMessage({ id, error: `EACCES: outside ${root}` });
    if (op === "write" && mode !== "rw") return port1.postMessage({ id, error: "EROFS: read-only" });
    if (op === "read") return port1.postMessage({ id, ok: true, content: files.get(full) });
    files.set(full, content);
    port1.postMessage({ id, ok: true });
  };
  return { port: port2, revoke: () => port1.close() };
}

In practice

// Host workspace: mount a read-only /docs port for one plugin and /drafts (rw) for another
const docsMount = createVfsMount(workspaceFiles, "/docs", "ro");
const draftsMount = createVfsMount(workspaceFiles, "/drafts", "rw");

docsPlugin.contentWindow.postMessage({ mount: "/docs" }, docsOrigin, [docsMount.port]);
draftsAgent.contentWindow.postMessage({ mount: "/drafts" }, agentOrigin, [draftsMount.port]);

// Later, when the user disables the AI agent or switches branches:
draftsMount.revoke(); // closes host port; Docs Reader's /docs mount keeps working

How it works

Plan 9's best idea was per-process namespaces: a program doesn't get a global filesystem guarded by user IDs — it only sees the directories explicitly mounted into its namespace. WASI preopened directories and vscode.dev webview mounts follow the same principle, and MessagePort brings it straight to browser plugins.

Instead of exposing a global storage bridge where every plugin passes arbitrary paths and hopes the host checks permissions properly, the host mints one MessageChannel per mount. The host's listener on port1 closes over two variables — root and mode — and transfers port2 to the plugin.

Every path arriving on that port is lexically canonicalised (stripping null bytes, collapsing . and .., and checking the prefix with a trailing slash so /docs-evil can't spoof /docs). Requests outside the subtree get EACCES, writes on a read-only mount get EROFS, and files under /secrets don't even exist from either plugin's point of view.

Revocation is one line: port1.close(). Because each plugin holds its own channel, revoking Drafts Agent's write mount cuts it off immediately while Docs Reader's read-only mount carries on untouched.

Step by step

What each press of Next does in the demo above.

  1. Mount /docs (read-only). Host creates a port strictly confined to /docs in read-only mode and transfers it to Docs Reader.
  2. Mount /drafts (read-write). Host creates a second port confined to /drafts in read-write mode and transfers it to Drafts Agent. Neither plugin can reach /secrets.
  3. Docs Reader reads /docs. Docs Reader reads /docs/readme.md, then tries ../secrets/keys.env and writing /docs/pwn.md. Both escapes are refused right at the host port boundary.
  4. Drafts Agent writes /drafts/summary.md. Drafts Agent creates /drafts/summary.md (watch Host's file tree update live), then tries reading /secrets/keys.env and gets EACCES.
  5. Revoke Drafts Agent's mount. Host closes its end of the /drafts port. Drafts Agent's next write gets silence and times out, while Docs Reader's /docs mount stays intact.

What you can build with it

Other ideas

  • In-browser IDEs and WebContainer-style workspaces mounting specific project folders into untrusted language servers or AI coding agents.
  • Plugin ecosystems where a summariser gets read-only access to /docs and an exporter gets write-only access to /out.
  • Wrapping the Origin Private File System (OPFS) or File System Access handles behind a strict policy port for sandboxed widgets.

Things to try

  • Step through both mounts, then watch Docs Reader read /docs/readme.md and get stopped cold on ../secrets/keys.env and write /docs/pwn.md.
  • In Step 4, watch Host's visible file tree gain /drafts/summary.md live when Drafts Agent writes inside its namespace, while /secrets/keys.env still returns EACCES.
  • In Step 5, revoke Drafts Agent's mount and watch its follow-up write time out with no reply.

Gotchas

  • Never use a naive path.startsWith(root) check without canonicalising .. segments and checking for a trailing / (or exact equality) — otherwise /docs/../secrets or /docs2/foo slips right past.
  • If your backing store supports symlinks (or maps to real disk via the File System Access API), lexical path checks aren't enough: walk directory handles segment by segment with getDirectoryHandle() rather than joining strings.
  • Closing the host port doesn't fire a close event in the plugin. Always put timeouts on filesystem RPCs so a revoked plugin fails cleanly instead of hanging.
  • A port is still a bearer token: if Drafts Agent forwards its /drafts port to another frame, that frame inherits write access to /drafts until you close the host end.

References

The web platform features this page mentions, with what each one is, its documentation on MDN and the specification that defines it.

MessagePort
One end of a message channel. Whatever is posted into it comes out of the other end, and it can be sent to another page, frame or worker. MDNHTML Standard
MessageChannel
Makes a new pair of joined ports, port1 and port2. MDNHTML Standard
Origin private file system
Origin-scoped file storage hidden from the user's normal filesystem. A worker can open a file in it for fast, exclusive synchronous access. MDNFile System Standard

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