Files
t3code/docs/cloud/environment-auth.md
T
Theo BrowneandClaude Fable 5 03a9e682f1 feat: silent pairing for local web-dev servers
Local web dev previously required scraping a one-time, 5-minute
/pair#token URL from dev-runner stdout for every fresh browser context,
which made parallel-instance work and agent browser verification
painful. The web app now authenticates automatically against eligible
dev servers via a guarded dev-only endpoint.

Server: POST /api/auth/dev-pairing-token mints a one-time administrative
credential (subject dev-auto-bootstrap, hidden from pairing-link
listings) only when the server is web-mode, loopback-bound
(loopback-browser policy), and configured with a loopback dev URL.
Requests must carry an Origin exactly matching the dev origin (survives
the Vite proxy, unlike Host) and a loopback Host header (DNS-rebinding
defense). Ineligible configs answer an explicit 404 — the handler is
always mounted so the SPA/redirect wildcard can't shadow it.

Startup: eligible dev servers now open the plain dev URL instead of
minting a startup pairing credential the silent path would leave
unconsumed in the URL and scrollback.

Web: the auth-gate bootstrap silently tries the endpoint when
unauthenticated in dev (never in hosted/desktop contexts, never when an
explicit #token is present) and falls back to the pairing screen on any
failure. import.meta.env.DEV keeps the request out of prod bundles.

Extracted host parsing into netHost.ts (auth/http.ts importing the old
helper from http.ts would have been a module cycle; the old helper also
mishandled Host headers with ports).

Docs: AGENTS.md browser-verification section, dev-instance auth notes in
scripts.md (plus stale T3CODE_STATE_DIR fix), and a security spec in
environment-auth.md.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-13 18:12:16 -07:00

7.2 KiB

Environment Authentication Profile

The environment server and the relay use separate credentials, issuers, and trust boundaries. They intentionally use a similar OAuth-shaped model so that permission checks and token exchange behavior can be audited against established concepts.

Authorization Model

Environment authorization is capability-based. A session carries zero or more OAuth-style scope strings:

Scope Permission
orchestration:read Read snapshots, status, events, configuration, and filesystem/VCS state.
orchestration:operate Dispatch user operations and mutate environment-side workspace state.
terminal:operate Create, attach, input, resize, clear, restart, and terminate terminals.
review:write Read review diff previews used to compose review feedback.
access:read Inspect pairing links and client sessions.
access:write Create or revoke pairing links and client sessions.
relay:read Inspect managed relay connectivity.
relay:write Link, configure, or unlink managed relay connectivity.

Ordinary pairing links grant the four client-operation scopes and read access to managed relay connectivity: orchestration:read orchestration:operate terminal:operate review:write relay:read. The desktop bootstrap credential and command-line administrative bootstrap credentials additionally grant access:read access:write relay:write.

Authentication Flows

Browser Session

POST /api/auth/browser-session consumes a one-time bootstrap credential and creates a browser session cookie. The cookie is an HTTP transport adapter for the same scoped session model; the response never exposes the session secret to browser JavaScript.

Bearer Access Token

Non-browser clients use POST /oauth/token with an application/x-www-form-urlencoded body:

grant_type=urn:ietf:params:oauth:grant-type:token-exchange
subject_token=<bootstrap credential>
subject_token_type=urn:t3:params:oauth:token-type:environment-bootstrap
requested_token_type=urn:ietf:params:oauth:token-type:access_token
scope=orchestration:read orchestration:operate terminal:operate review:write relay:read

Clients may additionally submit client_label, client_device_type, and client_os extension parameters so the authorized-clients UI can identify the device that established the session. These are presentation hints only; the environment derives transport metadata such as IP address and user agent from the request and does not use these fields for authorization.

The response has the token-exchange shape:

{
  "access_token": "<opaque session token>",
  "issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "orchestration:read orchestration:operate terminal:operate review:write relay:read"
}

Requested scopes must be a subset of the one-time bootstrap credential grant. An ordinary paired client therefore cannot exchange its grant for access:read, access:write, or relay:write.

WebSocket Ticket

POST /api/auth/websocket-ticket accepts any authenticated session and returns a short-lived, single-purpose WebSocket ticket. This keeps bearer tokens and browser cookies out of WebSocket URLs while allowing the socket handshake to authenticate. The ticket carries its session's scopes; each RPC method then enforces orchestration:read, orchestration:operate, terminal:operate, review:write, or access:read as appropriate. Review feedback submission currently dispatches an orchestration operation, so clients performing it also need orchestration:operate. Creating a ticket is not authorization to call every RPC method.

Dev-Mode Silent Pairing

POST /api/auth/dev-pairing-token lets the web app authenticate automatically against a local development server, so contributors and coding agents never handle pairing codes in dev. It mints an ordinary one-time, five-minute bootstrap credential (administrative scopes, subject dev-auto-bootstrap, hidden from the pairing-links listing) that the client immediately exchanges through the normal browser-session flow.

The endpoint answers 404 dev_pairing_not_available unless all of the following hold:

  • the server runs in web mode with a configured dev URL (bun run dev),
  • the dev URL's hostname is loopback (--dev-url never becomes an auth root),
  • the auth policy is exactly loopback-browser (excludes desktop dev and any non-loopback bind host).

Eligible requests are then authenticated per-request: the browser-attached Origin header must exactly equal the dev origin (this survives the Vite dev proxy, which rewrites Host but not Origin), and the parsed Host header must be loopback (DNS-rebinding defense for direct requests). Failures answer 403 dev_pairing_request_rejected. Because the payload is JSON, the request is preflighted, so unauthorized web origins cannot even trigger credential issuance. Local non-browser processes can forge these headers, but they can already read the dev state directory directly, so no privilege is gained.

When silent pairing is eligible, server startup opens the plain dev URL instead of minting a /pair#token=... startup credential (the silent path would leave that administrative token unconsumed in the URL and terminal scrollback). All other startup shapes are unchanged, and the /pair screen remains the flow for production, remote access, and explicit pairing links; an explicit #token in the URL always takes precedence over silent pairing.

Standards Alignment

  • Bearer access tokens are used through the Authorization: Bearer scheme from RFC 6750.
  • The token endpoint profiles the request and response vocabulary from OAuth 2.0 Token Exchange (RFC 8693), including subject_token, requested_token_type, access_token, issued_token_type, and token_type.
  • Scope values follow the OAuth 2.0 scope model from RFC 6749: space-delimited, unordered capabilities with subset checking during exchange.

This is intentionally not a general-purpose OAuth authorization server. The environment bootstrap token type is private, the bootstrap cookie and WebSocket connection-token routes are product-specific adapters, and the API returns its typed HttpApi errors rather than implementing every OAuth error response surface.

Upgrade Behavior

Migration 031_AuthAuthorizationScopes is a hard cutover from role-bearing auth records to scoped records. It deletes existing pairing links and sessions while leaving non-authentication environment state unchanged. Upgraded clients must pair again; old owner or client credentials are never silently mapped to new capabilities.

Relay Boundary

Relay-managed tunnels use their own tokens and keys. The relay can reuse scope parsing and token-exchange conventions, but an environment access token is not a relay token and cannot be presented to the relay.