Port Authority

Port patterns / Capabilities and trust boundaries

Cross-origin capability vault / secret broker

You shouldn't have to paste an API key or OAuth token into every third-party web app that needs to call a service on your behalf. Keep the secret inside your own vault origin and mint a metered MessagePort for each app — the same architecture powering this site's AI router (llmchannel.dev), generalised to any credential. Here Untrusted App receives a 2-call grant port. Vault checks the remaining budget and forwards the key plus prompt to a dedicated Call Worker, returning only the response. Once the second call completes, Vault closes the grant port automatically and the secret never leaves Vault's control.

When you'd use this

A client-side app wants to call an authenticated API — an LLM provider, email service or cloud backend — using the user's own account without ever asking the user to paste their secret key into the app. Keeping credentials inside a dedicated vault origin (like Port Authority's AI router) and lending out metered, revocable MessagePort grants bounds both blast radius and spend.

Code

// Vault origin: keep the API key here; hand the app a metered capability port
function mintGrant(limit, callWorkerPort) {
  let remaining = limit;
  const { port1, port2 } = new MessageChannel();
  port1.onmessage = async ({ data: { id, prompt } }) => {
    if (remaining <= 0) return port1.close();
    remaining--;
    const result = await callUpstream(callWorkerPort, { apiKey: VAULT_SECRET, prompt });
    port1.postMessage({ id, result, remaining });
    if (remaining === 0) port1.close(); // auto-revoke on exhaustion
  };
  return port2; // transfer this to the untrusted app — never VAULT_SECRET
}

In practice

// Third-party web app: ask the user's vault popup for a metered API capability
const popup = window.open("https://vault.example/grant", "", "popup");
const grantPort = await waitForGrantPort(popup, "https://vault.example");

// Send prompts over the port; the app never touches the user's API key
grantPort.onmessage = ({ data }) => renderAnswer(data.result);
grantPort.postMessage({ id: 1, prompt: "Summarise these release notes" });

// Inside vault.example, the grant handler checks remaining credits, calls its
// CSP-locked Call Worker with the stored key, and closes the port at zero.

How it works

Today's web trains users to copy raw API keys and OAuth tokens into random client-side apps. Once you paste a key into a site's input box, that origin can read it, exfiltrate it, or keep burning your credit long after you close the tab. This site's AI router (llmchannel.dev) flips that model around using a capability vault, and the exact same pattern works for any secret.

The secret stays inside one origin you trust — Your Vault Origin — stored in its own origin-scoped storage. When an untrusted app wants to make API calls, it asks the vault for a grant. You pick the scope and call budget, and the vault mints a fresh MessageChannel, keeps port1 wrapped in a quota counter, and transfers port2 to the app.

When the app posts a prompt down its grant port, Vault checks the remaining credits, decrements the counter, and delegates the actual network call to a dedicated Call Worker whose CSP restricts connect-src to the specific upstream API endpoint. Only the sanitised response goes back across the grant port to the app.

When the quota hits zero — or when you hit revoke — Vault calls port1.close(). The app never held the key, can't mint another call, and doesn't need to be trusted to clean up after itself.

Step by step

What each press of Next does in the demo above.

  1. App asks Vault for a 2-call grant. Untrusted App requests access. Vault mints a metered channel (limit: 2) and transfers the grant port to App — API_KEY stays inside Vault.
  2. Call #1 through the metered port. App sends prompt #1 down its grant port. Vault decrements remaining from 2 to 1, calls the CSP-locked Call Worker with the secret, and relays only the answer back.
  3. Call #2 uses the last credit. App sends prompt #2. Remaining drops from 1 to 0, Vault relays the final answer, and immediately closes the grant port.
  4. Call #3 after quota exhausted. App tries a third call on the same port. Nothing answers — the grant is closed, and App never saw a single byte of the API key.

What you can build with it

Other ideas

  • Bring-your-own-key AI apps like Port Authority's AI router (llmchannel.dev), letting static web apps call your LLM provider without ever seeing your key.
  • OAuth and webhook brokers that hand untrusted widgets a 5-call or 10-minute capability port instead of a broad bearer token.
  • Payment, email or cloud-storage powerboxes (in the spirit of Sandstorm Powerbox) where the user authorises one scoped action per channel.

Things to try

  • Step through the grant request and watch grant port (2) fly from Vault to Untrusted App while API_KEY = sk-secret-42 stays put.
  • Run Call #1 and Call #2 and watch the teal Call Worker handle both requests on a real Worker thread before Vault automatically cuts the yellow grant cable at zero credits.
  • Run Call #3 after exhaustion: Untrusted App's request goes nowhere, and neither Vault nor the Call Worker even sees it.

Gotchas

  • The vault origin must enforce a tight CSP on its Call Worker (for example, connect-src https://api.openai.com) and never let the untrusted app choose arbitrary request URLs or dump raw request headers back in error messages — otherwise the app can bounce the secret to an attacker-controlled server.
  • Rate-limit and inspect request payloads inside the vault before attaching credentials. A metered 2-call port still lets an app send a huge payload on those 2 calls unless you also cap tokens, bytes or allowed endpoints.
  • In cross-site iframes, storage partitioning means a hidden vault iframe won't always see the same IndexedDB keys as your top-level vault tab. Use a top-level popup opened via window.open() (as AI router does) so the vault runs in its own unpartitioned first-party origin.
  • Remember that closing the grant port is silent on the app's side, and any secret that's returned verbatim inside an upstream API error string could still leak unless Vault scrubs errors before forwarding.

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
Content Security Policy
A header that limits what a page may load and where it may connect. MDNContent Security Policy
<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
IndexedDB
A database in the browser that belongs to one origin. It can store most things that can be posted, including a CryptoKey. MDNIndexed Database API

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