Implements ADR 0001 (docs/adr/0001-phone-first-pairing.md): the console is
meant to be opened from a phone, so reachability stops being a mode and the
pairing link becomes the only access control that matters.
Binding / CORS
- `PI2_HOST` defaults to `0.0.0.0`; `PI2_ACCESS_MODE` (local|lan|tailscale) is
deleted, together with its parser, `AccessMode` type and LAN probe.
- `src/core/network.ts` is the single source of truth for reachable addresses:
kind classification, ordering (public → lan → other → tailscale → loopback,
physical NICs first, link-local skipped), bind-aware `networkBases()`,
`primaryBase()` and `localOrigins()`. Config, the pairing route, doctor and
the CLI all consume it, so the CORS allowlist always matches the pairing URL.
- `/api/v1/info` and `daemon.json` report `host` instead of `access`; doctor's
`access` check becomes `bind` (loopback ok, wildcard warns unless an `https:`
public URL is configured).
Pairing
- `POST /api/v1/pair` only mints, requires a token, and returns the code, the
chosen URL, the expiry, the nonce, the QR matrix and every usable base.
`base` may pin one of those bases; anything else is a 400.
- `POST /api/v1/pair/redeem` is the public half, rate limited per client
(5/min) and globally (50/h) → 429 `E_RATE_LIMITED` with `retry-after`.
- `GET /pair/:code` answers 302 to `/connect?code=…&auto=1`, so a scanned link
needs no JavaScript to reach the console.
- `src/core/qr.ts` (moved out of the CLI) is the one encoder: byte mode, EC
level M, versions 1–10, rows without a quiet zone, shared by the HTTP mint
response and the terminal renderer.
- `pi2 pairing` prints a human report plus a half-block terminal QR and keeps
`--base`; `--json` and `--public-url` are gone (the Neovim client calls the
HTTP mint directly).
Device scopes
- `Scope`, `SCOPES`, `DEFAULT_SCOPES`, `DeviceRecord.scopes` and the `scopes`
field in device/hello payloads are removed: nothing enforced them, and a
paired device now holds the same rights as the local browser. A permission
model, if wanted, gets its own ADR.
Breaking: `PI2_ACCESS_MODE`, `info.access`, `pi2 status --json`'s `access`,
`pi2 pairing --json`/`--public-url`, `POST /api/v1/pair {code}`, and the
`scopes` field. All in-repo callers and the e2e drivers are updated; the plugin
side (`:PiWebAccess`, `env.PI2_ACCESS_MODE`, text-format parsing) follows in
pi2.nvim.
Verification: root typecheck clean; 369/370 tests (the one failure is the
pre-existing supervisor timing flake, 24/24 in isolation ×3); `smoke.sh` green;
web suite 299/299; visual matrix 24/24.
109 lines
5.2 KiB
Markdown
109 lines
5.2 KiB
Markdown
# ADR 0001 — Phone-first binding and zero-input pairing
|
||
|
||
- Status: accepted (implementing)
|
||
- Date: 2026-10-26
|
||
- Supersedes: `PI2_ACCESS_MODE` (`local` / `lan` / `tailscale`)
|
||
|
||
## Context
|
||
|
||
The console has to be usable from a phone: open the link, done. Two things blocked
|
||
that:
|
||
|
||
1. **Reachability was an opt-in mode.** `PI2_ACCESS_MODE` picked the bind address
|
||
(`local` → loopback), and CORS only allowed the single detected LAN address in
|
||
`lan` mode. A phone that typed `http://192.168.2.10:7676` was refused by the
|
||
origin check, and the default pairing URL was `http://127.0.0.1:7676/pair/…`,
|
||
which is unscannable from another device.
|
||
2. **Pairing was a form, not a link.** The QR on the status page was a *fake*
|
||
pattern (`web/src/features/status/qr.ts` derived it from a seed), so scanning
|
||
it did nothing. The redemption endpoint and the mint endpoint shared one path
|
||
(`POST /api/v1/pair`), distinguished by whether `body.code` was present — the
|
||
authorization rule was implied by a field, not by the route.
|
||
|
||
Access control is already carried by the device token that a pairing code mints.
|
||
The bind address is not a security boundary we want to maintain.
|
||
|
||
## Decision
|
||
|
||
### 1. Bind every interface by default
|
||
|
||
- `PI2_HOST` defaults to `0.0.0.0`; setting it (e.g. `127.0.0.1`) narrows the bind.
|
||
- CORS derives from the addresses the daemon is actually reachable on: loopback plus
|
||
every non-internal IPv4 at the bound port, plus `PI2_PUBLIC_URL`'s origin, plus
|
||
`PI2_ORIGINS`. `PI2_ACCESS_MODE` and its three modes are deleted outright (the
|
||
repo is pre-1.0 and the plugin that set the env var ships from the same project).
|
||
- `/api/v1/info` and `daemon.json` report `host` instead of `access`.
|
||
|
||
### 2. One network source of truth
|
||
|
||
`src/core/network.ts` owns address classification and ordering:
|
||
|
||
| kind | rule |
|
||
|---|---|
|
||
| `public` | the configured `PI2_PUBLIC_URL` (never a NIC) |
|
||
| `lan` | private IPv4 (10/8, 172.16/12, 192.168/16) |
|
||
| `other` | any other routed IPv4 |
|
||
| `tailscale` | `tailscale*` interface or 100.64/10 address |
|
||
| `loopback` | 127/8 |
|
||
|
||
Ordering: `public` → `lan` → `other` → `tailscale` → `loopback`, physical interface
|
||
names before virtual ones (`docker*`, `veth*`, `br-*`, `virbr*`, `tun*`, `wg*`, …),
|
||
link-local (169.254/16) never advertised. `networkBases()` respects `bindHost`:
|
||
a loopback bind advertises only loopback, a specific address advertises that
|
||
address plus loopback, `0.0.0.0`/`::` advertises everything reachable.
|
||
|
||
Config (CORS), the pairing route (URL + `bases[]`) and `pi2 doctor` all consume it.
|
||
|
||
### 3. Pairing is mint → link → redeem
|
||
|
||
```
|
||
POST /api/v1/pair (authenticated) → {code,url,expiresAt,nonce,qr,bases[]}
|
||
POST /api/v1/pair/redeem (public, rate limited) → {token,device}
|
||
GET /pair/:code (public) → 302 /connect?code=…&auto=1
|
||
```
|
||
|
||
- The mint picks the URL server-side (`base` may be supplied, but only a value
|
||
present in `bases[]` is accepted) and returns the QR matrix, so no client
|
||
re-derives either.
|
||
- `qr` is byte-mode, EC level M, versions 1–10, `rows` without a quiet zone;
|
||
renderers own margin and colours. One encoder in `src/core/qr.ts`.
|
||
- Redeem is unauthenticated by design (the code *is* the capability) and now
|
||
rate limited: 5 attempts/minute/IP and 50/hour globally, over the limit → 429.
|
||
- Codes stay 8 characters from a 32-symbol alphabet, single use, 10 minutes.
|
||
|
||
### 4. Devices carry no scope list
|
||
|
||
Scopes were declared (`read`/`steer`/`control`/`admin`, plus a bogus `apply` in the
|
||
web client) but never enforced anywhere — HTTP and WS only checked that the token
|
||
was valid. Rather than invent an enforcement matrix, this ADR drops the concept:
|
||
a paired device and the local browser hold the same rights as `local-cli`, and any
|
||
fine-grained permission model is a separate feature with its own ADR.
|
||
|
||
Removed with it: `Scope`, `SCOPES`, `DEFAULT_SCOPES`, `DeviceRecord.scopes`,
|
||
`CreateDeviceInput.scopes`, `web`'s `PAIR_SCOPES`/`DEFAULT_DEVICE_SCOPES`, and the
|
||
`scopes` field in `/api/v1/devices` responses and the stored pair metadata.
|
||
|
||
### 5. Zero-input web flow
|
||
|
||
`/pair/<CODE>` (both the daemon's 302 and the SPA `/#/pair/CODE` route) lands on
|
||
`/connect?code=<CODE>&auto=1`, which redeems against its own origin with the
|
||
localized default device name and redirects to `next`/`/workspaces`. The code and
|
||
`auto` flag are stripped from the URL afterwards. A rejected or expired code falls
|
||
back to the manual form with host, port and code prefilled.
|
||
|
||
The in-console camera scanner (`web/src/features/connect/scan-dialog.tsx`) is
|
||
deleted: phones scan with the system camera, desktops type the code.
|
||
|
||
## Consequences
|
||
|
||
- A daemon started without configuration is reachable from the LAN. `pi2 doctor`
|
||
reports the bind, and warns (not fails) when a non-loopback bind has no `https:`
|
||
public URL.
|
||
- `pi2 pairing` prints a human-readable report and a terminal QR; it no longer
|
||
emits `--json`. The Neovim client calls `POST /api/v1/pair` directly.
|
||
- Breaking for anyone parsing `info.access`, `pi2 status --json`'s `access`,
|
||
`pi2 pairing --json`, or `POST /api/v1/pair {code}`; all in-repo callers are
|
||
updated in the same change.
|
||
- `docs/protocol.md` no longer claims `E_FORBIDDEN` covers scopes; it covers
|
||
origins and `PI2_WORKSPACE_ROOTS` only.
|