Repository navigation
Description
Problem
Connecting a Tower to Codev Cloud fails with a bare Unauthorized page. The cloud OAuth flow ends with cloud.codevos.ai redirecting the browser to
http://localhost:4100/api/tunnel/connect/callback?nonce=<uuid>&token=<token>
and Tower answers 401. Tower log at the moment of failure (3.3.1, started 2026-08-28):
[2026-09-01T04:00:29.026Z] [WARN] 401 GET /api/tunnel/connect/callback?nonce=…&token=… — missing or invalid key
Reproduced on the owner's machine 2026-09-01 after a dashboard Disconnect (which deregisters server-side and removes the cloud config) followed by Connect. Any fresh registration — first-time connect, or reconnect after a disconnect — hits this; only the "smart reconnect" path (credentials still on disk, no OAuth round-trip) still works, which is why the 2026-08-19 reconnect did not surface it.
Root cause
PR #1421 ("Enforce request authentication on the Tower local API", advisory GHSA-xvjp-7748-v88v, shipped in 3.3.1) put every route behind the shared local key except a narrow public allowlist — isPublicRoute() in packages/codev/src/agent-farm/utils/server-utils.ts (/health, /api/version, the dashboard shell, /workspace/<enc>/ static assets, the annotator shell + vendor files).
GET /api/tunnel/connect/callback is not on that allowlist. It is a top-level browser navigation (a cross-site redirect from the cloud), which structurally cannot carry the codev-tower-key header — the same reason the annotator shell earned its carve-out. So isRequestAllowed() rejects it in handleRequest (tower-routes.ts) before handleTunnelEndpoint ever runs.
The route is already self-authenticating: the handler (tower-tunnel.ts connect/callback branch) consumes a single-use nonce with a 5-minute TTL (lib/nonce-store.ts) that can only be minted by an authenticated POST /api/tunnel/connect. A keyless request with no valid nonce gets a 400 ("Invalid or expired registration link") and no registration ever happens. The key check adds nothing here and breaks the flow.
Fix (prescribed — no design decisions left open)
- In
isPublicRoute()add an exact-match carve-out forGET /api/tunnel/connect/callback, with a comment stating why it is safe (browser redirect cannot carry the header; the nonce is the credential; the handler 400s without a valid nonce). Keep it exact-match — do not open/api/tunnel/or any prefix. - Tests:
server-utils/ auth tests: keylessGET /api/tunnel/connect/callbackis allowed; keylessPOST /api/tunnel/connect,GET /api/tunnel/status,POST /api/tunnel/disconnectremain rejected (the carve-out must not widen).tower-tunnel.test.ts: the callback with an unknown/expired nonce still returns 400 (proves the public route is nonce-gated).
- Update the public-allowlist doc comment in
server-utils.tsand thearch.mdnote on the local-key model to list the OAuth callback among the header-less browser-navigation entry points.
Out of scope: any other change to the auth model, the nonce store, or the tunnel flow.
Testing
- Unit tests above.
- Manual end-to-end on a local Tower:
afx tower disconnect→ dashboard Connect → complete the cloud OAuth → callback lands on the "Connected to Codev Cloud" page andafx tower statusshows the tunnel connected. (Needs a release —@nextis fine — to reach an installed 3.3.1 Tower; note the release need in the PR.)
Notes for the PR
- cc @amrmelsayed as the author of Enforce request authentication on the Tower local API #1421 — please tag him in the PR description so he is aware of the regression and can sanity-check the carve-out against the advisory's threat model.
- Lesson for the review checklist (add to the PR's lessons entry): a public-route carve-out review must enumerate every browser-navigation entry point (redirects, iframes,
<script>/<link>loads) that structurally cannot carry a header. Enforce request authentication on the Tower local API #1421's 3-way consult and the architect review both walked the API surface and missed the OAuth redirect.
Interim workaround for operators: click Connect, land on the Unauthorized page, and within 5 minutes replay that exact URL with the key:
curl -sS -H "codev-tower-key: $(cat ~/.agent-farm/local-key)" "<callback URL>".
Activity
waleedkadous commentedon Sep 1, 2026
On it! Working on a fix now.
[Bugfix #1570] Fix: allow the keyless cloud OAuth callback through th…
[Bugfix #1570] Test: regression for the keyless OAuth callback carve-out
[Bugfix #1570] Thread: keyless OAuth callback carve-out
[Bugfix #1570] Thread: PR opened, CMAP 3x APPROVE
Merge pull request #1571 from cluesmith/builder/bugfix-1570
waleedkadous commentedon Sep 1, 2026
Completion stats
Fixed in PR #1571 (merged 2026-09-01, merge commit
b5c7c85), protocol BUGFIX.isPublicRoute()exact-match branch) + ~10 lines of commentserver-utils.ts(fix + allowlist doc comment),request-auth.test.ts,tower-tunnel.test.ts,arch.md, builder threadserver-utils.tsto HEAD — the two new auth assertions fail withexpected false to be true@cluesmith/codevpackage: 5340 tests, 0 failures; build greenWhat shipped
if (pathname === '/api/tunnel/connect/callback') return true;— exact match, deliberately not an/api/tunnel/prefix. The route authenticates itself with the single-use 5-minute nonce that only an authenticatedPOST /api/tunnel/connectcan mint, so a keyless request without one 400s and registers nothing.Also verified during review, and recorded in the PR:
TunnelClientalready refuses to proxy any/api/tunnel/path inbound (BLOCKED_PATH_SEGMENT,lib/tunnel-client.ts:71), so the carve-out is unreachable from the cloud side — it opens the path only to a browser on the local machine, which is exactly the OAuth redirect it exists for.arch.mdnow lists the OAuth callback among the header-less browser-navigation entry points, and carries the review lesson: a public-route carve-out review must enumerate every entry point that structurally cannot carry a header (redirects, iframes,<script>/<link>loads) — not walk the API surface. #1421's 3-way consult and architect review both walked the routes and still missed the redirect.Two things still open
@nextis fine) to reach an installed 3.3.1 Tower. The real check —afx tower disconnect→ dashboard Connect → complete cloud OAuth → "Connected to Codev Cloud" +afx tower statusshowing connected — has not been run. This issue auto-closed on merge; that round-trip is still owed.rejectIfProxiedis absent from the callback branch (tower-tunnel.ts:373) whereconnect/disconnectboth call it. Pre-existing and doubly mitigated (proxy-side block above + the nonce gate), so out of scope here — flagged in the PR for @amrmelsayed's read against the advisory's threat model.Merge pull request #1572 from cluesmith/records/1570-porch-records