feat(auth): name a configured routed site origin in Desktop sign-in answers

Merged
AG
2d ago
+321-13

What

A deployment can be reached through an edge router on another site origin that forwards <site>/cloud/... to this service. Those requests arrive with this service's own Host, so the sign-in answers named the API or dashboard origin. Agent Relay Desktop compares those values with the site it signed in to and refuses the sign-in when they differ (browser handoff api_url, device verification_uri, device and enrollment token api_url).

This adds one optional setting, SELFHOST_ROUTED_SITE_URL (an https origin without credentials, path, query or fragment). When a request carries exactly one x-original-host header whose value, trimmed and lower-cased, equals that origin's host, seven Desktop/CLI answers name the configured origin instead of the Host-selected one. Unset (the default), nothing changes.

Design

  • Exact configured value, never the header. The header only selects between configured origins. Nothing from it reaches a response. A repeated header (Node joins repeats with , ), an array value, or any other host falls back to the Host-selected origin.
  • No router-held proof. A caller that reaches the service directly can send x-original-host itself. The setting is confined to answers where that is harmless (see the table); every place where the origin would matter to someone other than the caller keeps the Host-selected origin.
  • The router must overwrite x-original-host with its own site's host instead of passing a client's copy through. This is documented, but nothing here depends on it for safety.

Every caller of siteBaseUrl, and the call made for each

CallerUses routed originWhy
Browser handoff loopback redirect, api_url query value (cli/login)yesThe redirect target is still the stored loopback redirect_uri; api_url is a query value read by the Desktop that started the sign-in. A direct caller that forges the header only makes its own Desktop see the configured public origin.
POST auth/desktop/session, apiUrlyesJSON to the bearer that called; no-store.
POST auth/desktop/workspaces, apiUrlyesSame.
POST auth/device/start, verification_uri and verification_uri_completeyesJSON to the unauthenticated caller that started its own device grant. The caller opens the URL itself; it is the configured origin plus a fixed path and its own user code. The approve route then redirects to the configured dashboard approval page, unchanged.
POST auth/device/token, api_urlyesJSON to the holder of the device code.
POST auth/enrollment/token, api_urlyesJSON to the holder of the enrollment key.
POST auth/token/refresh, apiUrlyesJSON to the holder of the refresh token.
MCP handler wiring (the endpoint URL the MCP surface reports, e.g. describe_capabilities)noClients store and reuse this value, so it stays the Host-selected origin.
Identity routes: OIDC redirect_urinoRegistered per origin at the identity provider.
Identity routes: emailed sign-in, password reset and verification linksnoThey carry one-time tokens and go to a mailbox, not to the caller. A forged header must never choose where such a link points, and the root sign-in page on a routed site is not necessarily this service.
Identity routes: password form pagesnoBrowser pages; see below.

In all seven "yes" rows the value goes only to the caller that sent the header, is an operator-configured public origin, scopes no cookie, is not stored, and is never the target of a redirect this service sends. Telling a direct caller the routed site's name tells it where the operator already publishes the service.

Not covered

  • Browser origin checks and cookies are unchanged. The Desktop consent form (like every cookie-authenticated write) still admits only the API and dashboard origins, so a browser sign-in page served through the router is not made to work by this setting alone. That needs its own decision, because it widens an origin allow-list instead of naming a URL.
  • The device and enrollment-key sign-ins and every session-bearer call are covered.

Tests

test/desktop-site-origin.test.ts: through the router (browser handoff, exchange, refresh, device start and token); direct through each configured site with the setting on; header variants (case and space variant matches; another host, a comma-joined value, two values and a repeated value fall back, and the foreign host never appears in any answer); setting unset; MCP endpoint URL unchanged; workspace switch; enrollment key exchange. test/config.test.ts: the setting's parsing and refusals.

Local, one job at a time: tsc -p tsconfig.json --noEmit clean; test/desktop-site-origin.test.ts 13/13; test/config.test.ts 24/24.

No migration. Docs: docs/repository-guide.md (setting table and a new section), openapi.yaml, CHANGELOG.md.

Held with merge-train:hold: not to be merged without the maintainer's word.

๐Ÿค– Generated with Claude Code


Autofix with [code]smith
Open in [code]smith ยท Need help on this PR? Tag @codesmith-bot with what you need.


Summary by cubic

Fixes Desktop sign-in for deployments reached through an edge router on another site origin. Those requests arrive with this service's own Host, so the sign-in answers named the API or dashboard origin, and a Desktop that signed in through the routed site refused them.

Adds optional SELFHOST_ROUTED_SITE_URL (an https origin). When a request carries exactly one x-original-host header naming that origin's host, seven Desktop/CLI sign-in answers โ€” browser handoff api_url, desktop session and workspace switch apiUrl, device start verification_uri and verification_uri_complete, device and enrollment token api_url, and refresh apiUrl โ€” name the configured origin instead. Unset (the default), nothing changes.

  • The header only selects the configured value and is never echoed: a mismatch, a repeated value, or an array falls back to the Host-selected origin, and the foreign host never appears in any answer.
  • Emailed links, OIDC redirect URIs, the MCP endpoint URL, cookies, and browser origin checks are unchanged, so a consent page served through the router still admits only the API and dashboard origins.
  • The router must overwrite x-original-host with its own site's host.