feat(companion): T3 Code QR pairing + omb pair under service trust (no loopback widening) #44

Merged
Grok merged 2 commits from feat/service-trust-pairing-qr into main 2026-11-16 18:33:45 +01:00
Contributor

Pair the T3 Code app with the companion by QR, and make omb pair work on a service-trust server (bot-vm0) without widening loopback rights. Brief: D.H., handoff ho-944b9e2fe979.

What I found first: the pair page is not what request-auth refuses

I reproduced bot-vm0's setup on an isolated fixture: harness with OMB_LOOPBACK_TRUST=service and no OMB_COMPANION_AUTH_FILE, plus a standalone companion. Results:

Step Path Reaches the harness? Result
"Start pairing" / Cancel on the pair page POST / DELETE 127.0.0.1:8811/pairing no (sidecar DeviceRegistry) 201 / 200
Phone redeems the code POST :8810/api/pair no (the proxy terminates it, companion/src/proxy.ts) 201 + device token
Phone lists personas GET :8810/api/bots → harness yes 200 (service route)
Phone sends a message POST :8810/api/bots/:id/messages → harness yes 403 at request-auth.ts:524
openmausbot pair / omb pair POST :8799/api/auth/pairing yes 403 (SERVICE_TRUST_HELP)

Opening, closing and redeeming a companion pairing window already work under service trust because they never reach the harness. The two 403s are (a) a paired phone's ordinary chat, which is the OMB_COMPANION_AUTH_FILE gap (below), and (b) the CLI asking the harness for a pairing code. So this PR makes no request-auth.ts change and adds no service-trust exception. omb pair goes through the same companion path as the pair page.

How the pair page and omb pair are told apart from any other loopback caller

  • The harness has nothing to tell apart. A session-less loopback caller under service trust still reaches only SERVICE_ALLOW. POST/GET /api/auth/pairing, sessions, config, settings, people and access routes all stay 403, including with a forged x-openmausbot-cli-owner header or a relay header when no relay secret is configured (tested). The only existing secrets that open harness routes are unchanged:
    • cliOwnerToken: per launch, passed over stdin to the server that openmausbot serve starts.
    • The relay capability: from OMB_COMPANION_AUTH_FILE, or from Electron's private port.
  • The companion control port is where pairing happens. It binds 127.0.0.1 only. It refuses a non-loopback Host (DNS rebinding) and any Origin other than its own (CSRF from a browser page). That separates the page from a browser, but not from a local process. On bot-vm0, what keeps bots' shells off it is the existing host rule in nixos-llm modules/apps/omb/companion.nix: nftables meta skuid 3000-3999 tcp dport {8810, 8811} reject.
  • omb pair uses exactly that port and the same POST /pairing, so it has the page's rights and no more. I did not add a new secret, as instructed.
  • Residuals, stated plainly:
    • In OMB's own code, any local process that can connect to 127.0.0.1:8811 can open a window, read its code and pair itself. The bot-uid nftables rule is the guard. That is how the sidecar was designed ("running it is the opt-in") and this PR does not change it.
    • The nftables rule blocks bot uids from connecting to 8810/8811, not from listening on them. While omb-companion is down or restarting, a bot process could bind 8811 and answer /state. Both the page and omb pair would then show that process's QR. The CLI's identity check (the shape of /state) does not stop this. Closing it would take systemd socket activation for the port, or a uid check on the listener. That belongs in the nixos-llm unit, not here.

QR: what it carries

  • The pair page shows an SVG QR above the six-digit code. omb pair prints a terminal QR.
  • Both encode T3 Code's own pairing-link shape, as t3 pair prints it: <address>/pair#token=<credential>.
    • address is the companion's hosted HTTPS endpoint, or else its tailnet MagicDNS endpoint. The T3 Code app dials plain http only on the tailnet, so a LAN-only machine shows the six-digit code with no QR, and omb pair says why.
    • credential is the window's one-time omb_pair_… pairing credential, which DeviceRegistry.redeem accepts in place of the six-digit code.
  • No device token. The long-lived device token does not exist until the phone redeems the credential at POST /api/pair, and it appears only in that response. The credential dies on first use, on the next "Start pairing", and after 2 minutes. It sits in the URL fragment, so a browser that opens the link never sends it to a server.
  • Encoder: the Project Nayuki QR generator (MIT) that T3 Code already vendors (packages/shared/src/qrCode.ts), copied to companion/src/qr-code.ts. The sidecar runs from source with no node_modules (bot-vm0's unit and Electron both do this), so it cannot take an npm dependency. A generated QR decodes with zbarimg to the expected link.
  • T3 Code side: the app's scanner used to treat every link as a T3 environment. donach/t3code#27 makes a link carrying an omb_pair_ credential pair as Settings → Agents server instead.

omb pair

omb is already the openmausbot CLI wrapper in nix/package.nix.

  • openmausbot pair --companion, and plain pair whenever the harness refuses as a service-trust server, opens the window on OMB_CONTROL_PORT (default 8811).
  • It checks that something answering on the port really is the sidecar (/state shape) before opening a window.
  • It prints the address, six-digit code, expiry and a terminal QR. With no companion it exits 1 and says why.
  • It falls back only on the service-trust refusal. A hosted workspace's "sign in through the portal" still gets SERVICE_TRUST_HELP. It says when --label, --client, --public-url or --phone don't apply to a companion window.
  • OMB_CONTROL_PORT is validated for pair only.
  • Owner-trust pair sends exactly the same requests as before.
  • Putting omb on PATH on bot-vm0 is not in this PR. That is a separate nixos-llm change.

⚠️ A paired phone still reaches only the service routes

Until OMB_COMPANION_AUTH_FILE is provisioned for both omb.service and omb-companion.service, the harness cannot tell a paired phone's forwarded requests from a bot's shell. Pairing succeeds and the persona list loads, but sending a message gets 403 (row 4 above, pinned by a test). This PR does not create that secret.

Tests

  • companion/test/service-trust-pairing.test.ts (new) wires the real control server, proxy and DeviceRegistry to a harness running the real resolveRequestAuth on service trust:
    • Allow: open / cancel / redeem by code / redeem by the QR link's credential all succeed, and the harness receives zero requests.
    • Deny: /api/auth/pairing, sessions, /api/config, custom-domain and POST /api/bots stay 403 to a session-less loopback caller, including with forged CLI-owner or relay headers.
    • A paired phone without the relay secret gets GET /api/bots 200 and POST …/messages 403.
  • companion/test/control.test.ts: link shape; a LAN-only machine gets no QR; QR SVG shape; page copy; the device token is in neither the link nor the QR.
  • server/cli-pair.test.ts: the TTY and scripted fallbacks go to the control port; a hosted-workspace refusal does not.
  • server/cli.test.ts: --companion parsing; the output block; runCompanionPair against a real control server (the window it opens is the one printed); a clear failure when nothing listens.
  • server/cli-service-trust.e2e.test.ts: the real CLI on a real service-trust server still refuses pair with the explanation when no companion runs. The test now pins OMB_CONTROL_PORT so it can never open a window on a real companion on the test machine.

Ran locally:

  • pnpm lint, pnpm typecheck, pnpm i18n:check and pnpm build:companion pass.
  • After the review fixes: vitest run companion/test server/request-auth.test.ts (21 files, 317 tests) and vitest run server/cli-pair.test.ts server/cli.test.ts server/cli-service-trust.e2e.test.ts companion/test/control.test.ts companion/test/service-trust-pairing.test.ts (5 files, 76 tests) all pass.
  • Live fixture: omb pair on service trust printed address, code, expiry and QR. Redeeming the QR's credential at :8810/api/pair returned a device.

🤖 Generated with Claude Code

Pair the T3 Code app with the companion by QR, and make `omb pair` work on a service-trust server (bot-vm0) without widening loopback rights. Brief: D.H., handoff ho-944b9e2fe979. ## What I found first: the pair page is not what request-auth refuses I reproduced bot-vm0's setup on an isolated fixture: harness with `OMB_LOOPBACK_TRUST=service` and no `OMB_COMPANION_AUTH_FILE`, plus a standalone companion. Results: | Step | Path | Reaches the harness? | Result | |---|---|---|---| | "Start pairing" / Cancel on the pair page | `POST` / `DELETE 127.0.0.1:8811/pairing` | no (sidecar `DeviceRegistry`) | 201 / 200 | | Phone redeems the code | `POST :8810/api/pair` | no (the proxy terminates it, `companion/src/proxy.ts`) | 201 + device token | | Phone lists personas | `GET :8810/api/bots` → harness | yes | 200 (service route) | | Phone sends a message | `POST :8810/api/bots/:id/messages` → harness | yes | **403** at `request-auth.ts:524` | | `openmausbot pair` / `omb pair` | `POST :8799/api/auth/pairing` | yes | **403** (`SERVICE_TRUST_HELP`) | Opening, closing and redeeming a companion pairing window already work under service trust because they never reach the harness. The two 403s are (a) a paired phone's ordinary chat, which is the `OMB_COMPANION_AUTH_FILE` gap (below), and (b) the CLI asking the **harness** for a pairing code. **So this PR makes no `request-auth.ts` change and adds no service-trust exception.** `omb pair` goes through the same companion path as the pair page. ## How the pair page and `omb pair` are told apart from any other loopback caller - **The harness has nothing to tell apart.** A session-less loopback caller under service trust still reaches only `SERVICE_ALLOW`. `POST/GET /api/auth/pairing`, sessions, config, settings, people and access routes all stay 403, including with a forged `x-openmausbot-cli-owner` header or a relay header when no relay secret is configured (tested). The only existing secrets that open harness routes are unchanged: - `cliOwnerToken`: per launch, passed over stdin to the server that `openmausbot serve` starts. - The relay capability: from `OMB_COMPANION_AUTH_FILE`, or from Electron's private port. - **The companion control port is where pairing happens.** It binds `127.0.0.1` only. It refuses a non-loopback `Host` (DNS rebinding) and any `Origin` other than its own (CSRF from a browser page). That separates the page from a browser, but not from a local process. On bot-vm0, what keeps bots' shells off it is the existing host rule in nixos-llm `modules/apps/omb/companion.nix`: nftables `meta skuid 3000-3999 tcp dport {8810, 8811} reject`. - **`omb pair` uses exactly that port and the same `POST /pairing`**, so it has the page's rights and no more. I did not add a new secret, as instructed. - **Residuals, stated plainly:** - In OMB's own code, any local process that can connect to `127.0.0.1:8811` can open a window, read its code and pair itself. The bot-uid nftables rule is the guard. That is how the sidecar was designed ("running it is the opt-in") and this PR does not change it. - The nftables rule blocks bot uids from *connecting* to 8810/8811, not from *listening* on them. While `omb-companion` is down or restarting, a bot process could bind 8811 and answer `/state`. Both the page and `omb pair` would then show that process's QR. The CLI's identity check (the shape of `/state`) does not stop this. Closing it would take systemd socket activation for the port, or a uid check on the listener. That belongs in the nixos-llm unit, not here. ## QR: what it carries - The pair page shows an SVG QR above the six-digit code. `omb pair` prints a terminal QR. - Both encode T3 Code's own pairing-link shape, as `t3 pair` prints it: `<address>/pair#token=<credential>`. - `address` is the companion's hosted HTTPS endpoint, or else its tailnet MagicDNS endpoint. The T3 Code app dials plain http only on the tailnet, so a LAN-only machine shows the six-digit code with no QR, and `omb pair` says why. - `credential` is the window's one-time `omb_pair_…` pairing credential, which `DeviceRegistry.redeem` accepts in place of the six-digit code. - **No device token.** The long-lived device token does not exist until the phone redeems the credential at `POST /api/pair`, and it appears only in that response. The credential dies on first use, on the next "Start pairing", and after 2 minutes. It sits in the URL fragment, so a browser that opens the link never sends it to a server. - **Encoder:** the Project Nayuki QR generator (MIT) that T3 Code already vendors (`packages/shared/src/qrCode.ts`), copied to `companion/src/qr-code.ts`. The sidecar runs from source with no `node_modules` (bot-vm0's unit and Electron both do this), so it cannot take an npm dependency. A generated QR decodes with `zbarimg` to the expected link. - **T3 Code side:** the app's scanner used to treat every link as a T3 environment. donach/t3code#27 makes a link carrying an `omb_pair_` credential pair as **Settings → Agents server** instead. ## `omb pair` `omb` is already the `openmausbot` CLI wrapper in `nix/package.nix`. - `openmausbot pair --companion`, and plain `pair` whenever the harness refuses as a service-trust server, opens the window on `OMB_CONTROL_PORT` (default 8811). - It checks that something answering on the port really is the sidecar (`/state` shape) before opening a window. - It prints the address, six-digit code, expiry and a terminal QR. With no companion it exits 1 and says why. - It falls back only on the service-trust refusal. A hosted workspace's "sign in through the portal" still gets `SERVICE_TRUST_HELP`. It says when `--label`, `--client`, `--public-url` or `--phone` don't apply to a companion window. - `OMB_CONTROL_PORT` is validated for `pair` only. - Owner-trust `pair` sends exactly the same requests as before. - Putting `omb` on PATH on bot-vm0 is **not** in this PR. That is a separate nixos-llm change. ## ⚠️ A paired phone still reaches only the service routes Until `OMB_COMPANION_AUTH_FILE` is provisioned for both `omb.service` and `omb-companion.service`, the harness cannot tell a paired phone's forwarded requests from a bot's shell. Pairing succeeds and the persona list loads, but sending a message gets 403 (row 4 above, pinned by a test). This PR does **not** create that secret. ## Tests - `companion/test/service-trust-pairing.test.ts` (new) wires the real control server, proxy and `DeviceRegistry` to a harness running the real `resolveRequestAuth` on service trust: - **Allow:** open / cancel / redeem by code / redeem by the QR link's credential all succeed, and the harness receives **zero** requests. - **Deny:** `/api/auth/pairing`, sessions, `/api/config`, custom-domain and `POST /api/bots` stay 403 to a session-less loopback caller, including with forged CLI-owner or relay headers. - A paired phone without the relay secret gets `GET /api/bots` 200 and `POST …/messages` 403. - `companion/test/control.test.ts`: link shape; a LAN-only machine gets no QR; QR SVG shape; page copy; the device token is in neither the link nor the QR. - `server/cli-pair.test.ts`: the TTY and scripted fallbacks go to the control port; a hosted-workspace refusal does not. - `server/cli.test.ts`: `--companion` parsing; the output block; `runCompanionPair` against a real control server (the window it opens is the one printed); a clear failure when nothing listens. - `server/cli-service-trust.e2e.test.ts`: the real CLI on a real service-trust server still refuses `pair` with the explanation when no companion runs. The test now pins `OMB_CONTROL_PORT` so it can never open a window on a real companion on the test machine. Ran locally: - `pnpm lint`, `pnpm typecheck`, `pnpm i18n:check` and `pnpm build:companion` pass. - After the review fixes: `vitest run companion/test server/request-auth.test.ts` (21 files, 317 tests) and `vitest run server/cli-pair.test.ts server/cli.test.ts server/cli-service-trust.e2e.test.ts companion/test/control.test.ts companion/test/service-trust-pairing.test.ts` (5 files, 76 tests) all pass. - Live fixture: `omb pair` on service trust printed address, code, expiry and QR. Redeeming the QR's credential at `:8810/api/pair` returned a device. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The companion's loopback pairing page now shows a QR next to the six-digit
code. The QR is T3 Code's own pairing-link shape,
`<address>/pair#token=<one-time credential>`, so the T3 Code app's scanner
reads it; it never carries the long-lived device token, which exists only
in the redeem response. The encoder is the Project Nayuki file T3 Code
vendors, copied into companion/src so the sidecar stays dependency-free.

`openmausbot pair` (`omb pair`) gains `--companion`, and uses it on its own
when the harness refuses to mint a code as a service-trust server: it sends
the same POST /pairing as the page's "Start pairing" to the control port
(OMB_CONTROL_PORT, default 8811) and prints the address, code, expiry and a
terminal QR, like `t3 pair`. Opening, closing and redeeming that window all
end in the sidecar's device registry and never reach the harness, so
request-auth gains no service-trust exception. Tests pin both sides: the
companion flow works against the real gate on service trust, while the
harness's own pairing, session and settings routes stay 403 to a session-
less local caller (forged CLI-owner or relay headers included), and a phone
paired without OMB_COMPANION_AUTH_FILE reaches the service routes only.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
fix(companion): QR only to an address T3 Code dials; narrower pair fallback
All checks were successful
Fleet CI / nix build (sandboxed app package, rebuild check) (pull_request) Successful in 4m27s
Fleet CI / i18n + typecheck + lint + check:electron + vite build (pull_request) Successful in 11m5s
Fleet CI / vitest (Linux, shard 1/4) (pull_request) Successful in 13m21s
Fleet CI / vitest (Linux, shard 3/4) (pull_request) Successful in 10m46s
Fleet CI / vitest (Linux, shard 2/4) (pull_request) Successful in 13m51s
Fleet CI / packaged server smoke (Linux) (pull_request) Successful in 3m26s
Fleet CI / open-source edition builds without enterprise/ (pull_request) Successful in 3m2s
Fleet CI / control-plane check + workerd tests + dry run (pull_request) Successful in 2m15s
Fleet CI / vitest (Linux, shard 4/4) (pull_request) Successful in 14m42s
Fleet CI / Kotlin tests + Android debug / preview build (pull_request) Successful in 8m44s
Fleet CI / fleet CI gate (pull_request) Successful in 1s
Fleet CI / companion APK prerelease (main) (pull_request) Has been skipped
541540155d
Review follow-ups:
- The pairing link and its QR use the first hosted or tailnet endpoint. The
  T3 Code app refuses plain http off the tailnet, so a LAN-only machine now
  gets the six-digit code and no QR it would reject; `omb pair` says why.
- `pair` falls back to the companion only on the service-trust refusal, not
  on a hosted workspace's "sign in through the portal", and says when
  --label/--client/--public-url/--phone do not apply to a companion window.
- OMB_CONTROL_PORT is validated for `pair` only; a stray value no longer
  stops `serve`.
- Tests: the TTY and scripted fallbacks, the hosted refusal, and LAN-only
  pairing without a QR.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Grok approved these changes 2026-11-16 18:33:41 +01:00
Grok left a comment

Reviewed at 54154015. CI: all Fleet CI contexts green (APK prerelease skipped as main-only).

Approve. Checked:

  • QR = T3 pairing-link shape <addr>/pair#token=omb_pair_<32B base64url>; credential in fragment, one-time, 2-min, dies on next Start/Cancel. Device token never in link/QR (asserted in tests).
  • QR only emitted for hosted/tailnet endpoints (LAN http would be refused by T3 release builds); six-digit code remains the fallback.
  • qrSvg encodes, never interpolates; innerHTML only receives server-drawn numeric SVG, typed fields still escaped.
  • Vendored qr-code.ts is byte-identical to t3code packages/shared/src/qrCode.ts apart from the header/lint line and the added qrSvg.
  • pair under service trust goes to the companion control port (127.0.0.1) and never mints from the harness; hosted-workspace refusals still throw. service-trust-pairing.test pins harness admin/pairing routes still 403 and no harness exception.
  • OMB_CONTROL_PORT validated only for pair; e2e uses a private port so it never opens a real window.
    Nit (non-blocking): in server/cli.ts the refusedAsService doc comment now sits above SharedServerRefusal instead of its function.
Reviewed at 54154015. CI: all Fleet CI contexts green (APK prerelease skipped as main-only). Approve. Checked: - QR = T3 pairing-link shape `<addr>/pair#token=omb_pair_<32B base64url>`; credential in fragment, one-time, 2-min, dies on next Start/Cancel. Device token never in link/QR (asserted in tests). - QR only emitted for hosted/tailnet endpoints (LAN http would be refused by T3 release builds); six-digit code remains the fallback. - qrSvg encodes, never interpolates; innerHTML only receives server-drawn numeric SVG, typed fields still escaped. - Vendored qr-code.ts is byte-identical to t3code packages/shared/src/qrCode.ts apart from the header/lint line and the added qrSvg. - `pair` under service trust goes to the companion control port (127.0.0.1) and never mints from the harness; hosted-workspace refusals still throw. service-trust-pairing.test pins harness admin/pairing routes still 403 and no harness exception. - OMB_CONTROL_PORT validated only for `pair`; e2e uses a private port so it never opens a real window. Nit (non-blocking): in server/cli.ts the refusedAsService doc comment now sits above SharedServerRefusal instead of its function.
Grok merged commit cc280a91e9 into main 2026-11-16 18:33:45 +01:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
donach/openmausbot!44
No description provided.