Skip to content

ADR 0087 — Device pairing: per-device revocable tokens + short-TTL QR codes ​

Status: Accepted

Relates to: ADR 0066 (the operator/federation token tiers this extends), ADR 0086 (the mobile shell this exists to get onto a phone).

Context ​

Adding a phone to a running instance is entirely manual today: read the host's LAN or tailnet address off the terminal, type it into the phone, then type a bearer token. The console makes the last step worse — authToken() (apps/web/src/lib/api.ts) reads localStorage only, so there is no URL-borne credential at all and no way to shorten the flow without adding one.

Auth today is a single shared secret: _BEARER[0] in a2a_impl/auth.py, plus an optional federation token confined to /a2a + /v1 (ADR 0066). Every device that authenticates presents the same string.

That is the real problem with "add my phone". Adding implies removing. With one shared secret, revoking a lost phone means rotating the secret, which simultaneously logs out the desktop, the CLI, every script, and every other device. In practice nobody does that, so a lost phone stays authorised.

Decision ​

D1 — Per-device tokens, individually revocable ​

Amended by ADR 0121 D4 (2026-10-18): device tokens are operator-trust with device scope — new phone pairings can't pair devices, manage plugins, use secrets or write credential-plane config. Agent devices and pre-0121 rows are full.

A device registry joins the operator and federation tiers: a list of named devices, each holding its own token. Auth accepts the shared operator bearer or any non-revoked device token; both resolve to the operator tier.

Device tokens are operator, not a lesser tier, on purpose. A paired phone runs the full console — it needs the same surface the desktop does. The point of the split is identity and revocation, not reduced capability. Anything else would be security theatre: the console's own features (plugin install, config rewrite) are what make the tier meaningful, and a phone that can't use them isn't the product.

Revoking a device is a single delete and affects nothing else. The shared bearer keeps working unchanged, so the CLI, A2A callers, and existing setups are untouched — this is purely additive.

D2 — Store hashes, never tokens ​

The registry persists sha256(token), never the token itself. A leaked registry file cannot be replayed. The token is returned exactly once, at claim time, and never again — there is no "show token" affordance to build later.

Registry location is instance_root/devices.json, not config_dir. config_dir is the tier that gets seeded/shared between instances; device credentials must never cross an instance boundary, so they live at the per-instance tier alongside .instance-uid.

D3 — Pairing is a short-TTL, single-use code — never the token ​

The QR encodes a pairing code, not a credential. Embedding the bearer (or a device token) in a scannable image would create a permanent credential that leaks through every screenshot, screen-share and shoulder-surf, forever, with no way to tell it happened.

The code is:

  • ~190 bits of entropy (32 url-safe characters), so guessing is not a threat;
  • 120 seconds TTL;
  • single-use, consumed atomically on claim;
  • memory-only — never written to disk, so a restart invalidates every pending pairing. That is the desired behavior, not a limitation.

Claiming mints a new device token. The scanning device sends its own name (derived from its user agent, e.g. "iPhone"), so the operator sees what joined rather than an anonymous entry.

Amended 2026-10-18 (#4336 S2): one short, typeable code. A phone that can't scan (no camera permission, a code shown on a terminal or a screen share) had no way in short of copying the shared operator bearer, which is the thing D1 exists to avoid. So the device code is now the same shape as an agent code (ADR 0113 D2): 10 Crockford base32 characters shown as XXXXX-XXXXX, 300 s TTL, minted by the same function and folded on claim the same way (case, dashes/spaces, O→0, I/L→1). The QR still encodes …/app/#pair=<code>, and the Devices panel shows the same code as text beside it together with the console URL to open; the console's AuthGate gains "Have a pairing code?" to type it; protoagent pair --device prints code, URLs and a terminal QR for a headless box.

One code, not two. Keeping the 190-bit QR code and adding a typed one would leave the pairing exactly as strong as the weaker code, so the long code would buy nothing but a second thing to cancel and expire. One code keeps "single-use" literally true: scanning or typing it spends it.

Why ~50 bits is honest here. The guard was never entropy alone. The claim is single-use, the code lives 300 s, and the failed-claim counter is shared across both kinds: five consecutive misses drop every pending code. A prober gets about 5 guesses per code the operator issues, so the odds per issued code are ~5/2^50 (about 4×10⁻¹⁵). The TTL went from 120 s to 300 s because typing a code read off another screen takes longer than scanning; it matches the agent-code window for the same reason. The D4 residual risk (anyone who can see the screen during the window can claim) is unchanged in kind and only longer in time. The panel cancels the code when it closes.

Links issued before the amendment carried the long code. They can't still be pending (pending codes are memory-only, so the upgrade restart cleared them), and a long code is simply a miss against the new store. The #pair= mechanism itself is unchanged.

D4 — The claim endpoint is unauthenticated, and that is the sensitive part ​

Amended by ADR 0121 D3: the five-miss lockout that drops every pending code is replaced by a claim throttle (two token buckets, 429 with Retry-After); no request can cancel a pending code.

POST /api/pairing/claim cannot require auth — obtaining auth is its entire purpose. It is therefore on the public allowlist, which widens the unauthenticated surface and deserves to be called out rather than buried.

It is guarded by: the entropy and TTL above, single-use consumption, an immediate reject when no pairing is pending (the common case is a closed door), and a failed-attempt counter that invalidates all pending codes rather than allowing indefinite guessing. Constant-time comparison throughout.

Accepted residual risk: anyone who can see the operator's screen during the code's window (120 s originally, 300 s since the D3 amendment) can scan the code and claim a device. This is inherent to QR pairing and is why the window is short, the code single-use, and the resulting device visible and revocable in a list. The alternative — no pairing — leaves people hand-typing bearer tokens, which in practice means picking weak ones or reusing them.

D5 — The code travels in the URL fragment ​

The pairing URL is …/app/#pair=<code>. A fragment is never sent to the server, so it stays out of access logs, proxy logs and Referer headers. The console consumes it into localStorage and immediately strips it from the URL and history, so a shared screenshot of the address bar (or a back-button trawl) doesn't carry it.

A query string would be simpler and is rejected for exactly those reasons.

D6 — Pairing requires a reachable bind address, and says so when there isn't one ​

Amended by ADR 0121 D2/D6: "never a public address" still holds for discovered addresses; the operator-declared network.public_console_url (https, on an instance with a token) is the only exception.

Pairing is meaningless when the server is bound to loopback: the phone cannot reach the port no matter what it scans. The start endpoint enumerates candidate addresses (tailnet first, then LAN) and reports them; with none it returns a clear error rather than a QR that cannot work.

This must not nudge anyone toward PROTOAGENT_ALLOW_OPEN=1. The server already refuses a non-loopback bind with no token (server/__init__.py), and that guard stays — pairing is about adding devices to a secured instance, not about opening one up.

Amended after shipping. D6 as written assumed a server someone launched with flags, which is not how most people run this. The desktop app binds 127.0.0.1 by design — correct for a local app — so pairing was unusable in precisely the place it was asked for: it reported "no reachable address" and dead-ended, with the fix (edit two config keys, restart) nowhere in sight.

So a loopback-bound instance now also reports what it could be reached on (_pairable_addresses, deliberately split from the bind-filtered _candidate_hosts), and the Devices panel offers to bind there. Choosing an address writes network.bind (host layer) and, when the instance has no token, mints one first — the server refuses a non-loopback bind without one, so "expose it but leave it open" is not a state the flow can even produce.

The bind value is 0.0.0.0, not the address the operator picked. v0.104.1 wrote the chosen address and hung the desktop app on launch: uvicorn takes ONE host, a single non-loopback bind DROPS loopback, and the desktop webview reaches its own sidecar over http://127.0.0.1:<port>. So the app could no longer talk to the server it had just reconfigured. Picking "Tailnet" selects the address we advertise in the QR, not the only one we listen on — "loopback plus tailnet, nothing else" is not expressible in a single uvicorn host.

That means the instance really does listen on every interface, which is broader than "the address you pick" implied. The UI says so plainly rather than describing a tighter posture than it delivers; the token is what actually gates access.

Two properties this must keep:

  • Tailnet is offered above LAN and labelled as the safer pick. A tailnet address is reachable only by the operator's own devices; a LAN address is reachable by anything on that Wi-Fi. The UI says which is which rather than presenting them as equivalent.
  • The offer never contains a public address. _pairable_addresses allowlists tailnet + RFC1918 exactly as _candidate_hosts does — "make me reachable" must never become "expose me to the internet".

network.bind is restart-gated, so the flow ends on an explicit restart notice rather than pretending to have finished, and it names how to undo it (set the bind back to 127.0.0.1).

Amended 2026-10-18 (#4336 S4). Three changes, all within the two properties above:

  • The picked address is the bind. The server now opens its listening sockets itself (src/server/bind.py) and hands them to uvicorn.Server.run(sockets=…). A non-loopback IP bind listens on that IP and 127.0.0.1. That is the "loopback plus tailnet, nothing else" posture the original D6 couldn't express, so the panel writes the picked IP. It writes 0.0.0.0 only for an older server, one that doesn't report bind_interface. If the IP is gone at boot, the server serves loopback only and warns; it does not refuse to start.
  • Tailnet HTTPS is preferred (ADR 0121 D1). When tailscale serve proxies HTTPS to this instance, the offer leads with https://<machine>.<tailnet>.ts.net. Then come the MagicDNS name over http, the tailnet IP and the LAN IP. The panel can turn serve on (POST /api/pairing/tailscale/serve), and that path needs no bind change at all. Serve forwards from 127.0.0.1, and auth never trusts loopback (a2a_impl/auth.py). So enabling is refused until a token is set, and a tokenless instance refuses every *.ts.net Host unless PROTOAGENT_ALLOW_OPEN=1 (a2a_impl/hosts.py tailnet_proxy_refusal).
  • Restart from the panel. The restart notice offers Restart now (POST /api/restart). It is replaced by instructions when a re-exec can't apply the bind: a --host launch or a container.

Consequences ​

  • The unauthenticated surface grows by one endpoint. It is the only one, it is rate-limited and TTL-bound, and it is the price of not having people type bearer tokens into phones.
  • Device tokens are operator-tier, so a stolen unlocked phone is as dangerous as a stolen unlocked laptop. Revocation is the mitigation, which is the whole point of D1.
  • The registry is per-instance. A device paired to the dev sandbox is not paired to prod.
  • Losing devices.json logs out every paired device (hashes are unrecoverable by design). The shared bearer still works, so this is a recoverable state.
  • Nothing here changes the existing bearer/federation behavior. An instance that never pairs a device is byte-for-byte unaffected.

Part of the protoLabs autonomous development studio.