Skip to content

tower: cloud OAuth callback (GET /api/tunnel/connect/callback) is 401'd by the local-key check — Connect to Codev Cloud fails with 'Unauthorized' (regression from #1421) #1570

Closed

Description

@waleedkadous

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)

  1. In isPublicRoute() add an exact-match carve-out for GET /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.
  2. Tests:
    • server-utils / auth tests: keyless GET /api/tunnel/connect/callback is allowed; keyless POST /api/tunnel/connect, GET /api/tunnel/status, POST /api/tunnel/disconnect remain 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).
  3. Update the public-allowlist doc comment in server-utils.ts and the arch.md note 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 and afx tower status shows the tunnel connected. (Needs a release — @next is fine — to reach an installed 3.3.1 Tower; note the release need in the PR.)

Notes for the PR

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

  1. added
    area/tower
    on Sep 1, 2026
  2. waleedkadous commented on Sep 1, 2026

    @waleedkadous
    ContributorAuthor

    On it! Working on a fix now.

  3. added 4 commits that reference this issue on Sep 1, 2026
  4. added a commit that references this issue on Sep 1, 2026
    b5c7c85
  5. waleedkadous commented on Sep 1, 2026

    @waleedkadous
    ContributorAuthor

    Completion stats

    Fixed in PR #1571 (merged 2026-09-01, merge commit b5c7c85), protocol BUGFIX.

    Fix 1 line of logic (isPublicRoute() exact-match branch) + ~10 lines of comment
    Total diff 165 insertions, 4 deletions across 5 files
    Files changed server-utils.ts (fix + allowlist doc comment), request-auth.test.ts, tower-tunnel.test.ts, arch.md, builder thread
    Commits 5 (fix, tests, 3 thread updates)
    Tests added 3 cases / 22 new assertions
    Regression proof Verified fails-without / passes-with by reverting server-utils.ts to HEAD — the two new auth assertions fail with expected false to be true
    Suite Full @cluesmith/codev package: 5340 tests, 0 failures; build green
    CMAP gemini=APPROVE, codex=APPROVE, claude=APPROVE — all HIGH confidence, zero blocking issues
    Consultation cost $1.54 / 4.7 min across 3 lanes (codex $0.44, claude $1.10, gemini n/a)
    Phases investigate → fix → pr, single pass, no rework iterations

    What 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 authenticated POST /api/tunnel/connect can mint, so a keyless request without one 400s and registers nothing.

    Also verified during review, and recorded in the PR: TunnelClient already 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.md now 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

    1. Manual end-to-end is unverified. It needs a release (@next is 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 status showing connected — has not been run. This issue auto-closed on merge; that round-trip is still owed.
    2. rejectIfProxied is absent from the callback branch (tower-tunnel.ts:373) where connect/disconnect both 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.
  6. added a commit that references this issue on Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/tower

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions