Skip to content

tower: cloud-proxied requests are 401'd — tunnel client forwards Host: cloud.codevos.ai and stamps no local key (second half of the #1421 regression; every Cloud page/API call fails) #1586

Closed

Description

@waleedkadous

Problem

Using a Tower through Codev Cloud fails with Unauthorized on every page and API call, even though registration succeeds (post-#1570) and the tunnel is connected. Tower log (3.3.2, 2026-09-02):

[WARN] 401 GET / — disallowed Host "cloud.codevos.ai"
[WARN] 401 GET /api/projects — missing or invalid key

This is the second half of the #1421 regression (Tower API authentication, shipped in 3.3.1): the first half blocked the OAuth callback (#1570, fixed in 3.3.2); this half blocks every request that arrives through the tunnel. It went unnoticed because connecting was broken first, so nobody reached this step until 3.3.2.

Root cause (proven from code, packages/codev/src/agent-farm/lib/tunnel-client.ts)

  1. Host is forwarded verbatim. proxyHttpRequest copies every non-pseudo, non-hop-by-hop inbound header into the localhost request — including Host: cloud.codevos.ai. isAllowedHost() (server-utils.ts) rejects it. The WebSocket CONNECT path (handleWebSocketConnect, ~line 737) explicitly sets Host: localhost:<port> and its comment claims "the HTTP proxy path already lets Node default Host to localhost" — false whenever an inbound host header is present, which it always is.
  2. No key is stamped. Tunnel-borne requests carry no codev-tower-key, and Tower's isRequestAllowed() gives the (unforgeable, stripped-and-restamped) x-codev-tunnel-proxy marker no exemption — the marker is only used for /api/tunnel/* rejection and log attribution (tower-tunnel.ts:319). So even with Host fixed, every keyed route 401s.

Trust model (arch.md §Tower API Authentication, unchanged): the cloud edge (codevos.ai) authenticates the remote user; the local key authenticates local actors to their own Tower. TunnelClient runs inside the Tower process and is a local actor — it legitimately possesses the key.

Fix (prescribed)

In proxyHttpRequest (mirror what handleWebSocketConnect already does):

  1. Rewrite Host: drop any inbound host and set Host: localhost:<localPort>. Fix the misleading comment on the WS path.
  2. Stamp the local key: strip any inbound codev-tower-key / legacy codev-web-key (anti-forgery, same pattern as stampProxyMarker), then set codev-tower-key to the local key (getExpectedKey() / ensureLocalKey() from server-utils — same process, same file). Do this in the same helper as the proxy marker so the two stamps cannot drift.
  3. WebSocket path: verify the browser-supplied Sec-WebSocket-Protocol: codev-key.<KEY> subprotocol survives the H2 CONNECT → localhost upgrade untouched (the remote dashboard shell is served by the local Tower with the key injected, exactly as for a local browser, so subsequent API/WS calls carry it themselves). Add a test if none pins it.
  4. Keep the existing /api/tunnel/* refusal for tunnel-borne requests exactly as is — the key stamp must NOT let a cloud actor reach management routes (test it).

Out of scope: rotating/scoping the key for remote sessions (the remote browser receives the same injected key a local browser does — no worse than pre-#1421, when tunnel access was fully unauthenticated locally; a per-session remote credential is a design follow-up for @amrmelsayed's trust-model read).

Testing

  • Unit: a proxied request with Host: cloud.codevos.ai and no key reaches the local handler with Host: localhost:<port> and a valid codev-tower-key; an inbound forged key/marker is replaced, not passed through; /api/tunnel/disconnect through the tunnel is still refused.
  • Existing tunnel suites stay green.
  • Field (architect-driven, post-release): open the Tower through cloud.codevos.ai → dashboard loads, workspace list populates, a terminal WebSocket attaches.

Release

Ride the 3.3.3 hotfix with #1584 — cloud access is fully broken on every released version since 3.3.1 without it.

cc @amrmelsayed — author of #1421; please sanity-check the stamp-in-tunnel-client shape against the advisory's threat model (it keeps the key check as the single choke point and adds no Tower-side exemption).

Refs: #1421 (source), #1570/PR #1571 (first half), #1374 (tunnel-borne management refusal).

Activity

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

    @waleedkadous
    ContributorAuthor

    On it! Working on a fix now.

  3. added 2 commits that reference this issue on Sep 2, 2026
    ff2c822
    cfb3290
  4. added a commit that references this issue on Sep 3, 2026
  5. added a commit that references this issue on Sep 3, 2026
    3591ad1
  6. waleedkadous commented on Sep 3, 2026

    @waleedkadous
    ContributorAuthor

    Completion stats

    Fixed in PR #1588, merged 2026-09-03 11:21 UTC by @amrmelsayed.

    Protocol BUGFIX (strict)
    Source change tunnel-client.ts: +46 / −13, one file
    Test change bugfix-1586-tunnel-auth.test.ts: +220, 6 tests
    Total 266 insertions / 13 deletions across 2 files
    CMAP gemini APPROVE · codex APPROVE · claude APPROVE (all HIGH, no blocking issues)
    CI 7/7 green
    Suite 5383 passed, 48 skipped, 0 failed

    Root cause

    Two independent failures in proxyHttpRequest, either alone fatal:

    1. Host forwarded verbatim. The header loop copied Host: cloud.codevos.ai into the localhost request. isAllowedHost() rejects it — and because isRequestAllowed() runs the Host guard ahead of the public-route allowlist, even the keyless dashboard shell at GET / 401'd, not just the API. The handleWebSocketConnect comment claiming the HTTP path "already lets Node default Host to localhost" was false: Node only defaults Host when none is supplied, and the cloud edge always supplies one.
    2. No local key stamped. The x-codev-tunnel-proxy marker carries no auth exemption — it exists only for /api/tunnel/* refusal and log attribution — so keyed routes had nothing to check.

    Reproduced against a live Tower before any code was written: cloud Host + valid key → 401; localhost + no key → 401; localhost + key → through.

    Fix

    stampProxyMarker → stampLocalHeaders(headers, localPort): strips any inbound host, codev-tower-key, codev-web-key and x-codev-tunnel-proxy, then stamps Host: localhost:<port>, the marker, and getExpectedKey(). All three set in one helper called by both proxy paths, so they cannot drift; stripping first means a cloud-side actor can neither forge nor suppress any of them.

    Trust model unchanged — no Tower-side exemption was added, the key check stays the single choke point, and the /api/tunnel/* refusal is untouched.

    Tests

    bugfix-1586-tunnel-auth.test.ts runs the real isRequestAllowed in the local server, pinning authorization end-to-end rather than by header shape. Three of six fail without the fix; three are invariants, green both ways:

    • /api/tunnel/* refused through the tunnel — root and workspace-scoped form
    • browser codev-key.<KEY> subprotocol survives the H2 CONNECT → localhost upgrade
    • fail-closed: with no readable local key, the forged inbound key is still stripped and none is stamped

    Notes

  7. added 3 commits that reference this issue on Sep 3, 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/towerbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions