Skip to content

tower: forward a sub-path to a local web IDE server + afx ide lifecycle #1668

Closed

Description

@amrmelsayed

Problem

Tower's tunnel client delivers every tunnel-borne request to exactly one place: Tower's own port. There is no way to expose a second local HTTP+WebSocket service (for example a VS Code server-web build serving a browser IDE) through the same tunnel. The only workaround today is to stop Tower and let another process take over the tunnel, which takes the dashboard offline.

The Codev IDE fork has validated, end to end, that a server-web workbench behind Tower and the cloud proxy boots in the browser with the Codev sidebar in IDE mode. What it needs from Tower is small and generic.

Scope

  1. Prefix forward. A configured prefix (e.g. /ide/) on Tower routes to http://127.0.0.1:<port>/… — plain HTTP in servers/tower-routes.ts handleRequest() and WebSocket upgrades in servers/tower-websocket.ts (raw pipe; the workbench opens two WS connections under its base path with ?reconnectionToken=). Strip the prefix before forwarding, or pass it as x-forwarded-prefix — the VS Code server honours x-forwarded-prefix / x-forwarded-host / x-forwarded-proto.
  2. Header hygiene on the forward. Strip x-forwarded-port and x-original-host (a leaked upstream port makes the browser dial the wrong WebSocket port); stamp x-forwarded-host / x-forwarded-proto from the tunnel's public authority when tunnel-borne.
  3. Origin guard. isWebSocketAllowed (utils/server-utils.ts) must accept the tunnel's public host for the forwarded prefix, as it does for the dashboard.
  4. Lifecycle. afx ide start | stop | status [--workspace <path>] spawns the local server (--host 127.0.0.1 --port <p> --server-base-path <prefix> --connection-token-file <tower-owned file> --accept-server-license-terms), records the port, and Tower auto-starts it when the prefix is hit if configured. Mirror the pattern of apps/vscode/src/tower-starter.ts.
  5. Optional but cheap: gzip/brotli on the forward for text responses. The workbench boot is ~21 MB uncompressed and the VS Code server sends no compression; compressing here avoids relying on the relay's edge.
  6. Config surface. Where the prefix → port mapping lives (~/.agent-farm/ config or per-workspace), and /api/tunnel/* stays local-only as today.

Non-goals

  • Serving the IDE's static assets from anywhere other than the local server (a static/dynamic split is a relay-side decision).
  • Unifying Tower's PTY bridge with the workbench's own terminals.

Acceptance

  • With a local VS Code server-web build on port N and the prefix configured, https://<relay>/t/<tower>/ide/ boots the workbench through the tunnel while the dashboard at /t/<tower>/ keeps working; both remote WebSockets connect with remoteAuthority = the relay's host; no ws://localhost attempts.
  • afx ide status reports port + PID; afx ide stop tears it down; Tower restart does not orphan it.

Auth model (added after architect review — this supersedes the "Origin guard" bullet's silence on auth)

The forwarded prefix is key-authenticated, not a public carve-out. It sits behind Tower's request-auth choke point like every other non-public route (advisory GHSA-xvjp-7748-v88v: shared local key + Host allowlist). Tunnel-borne requests already arrive stamped with the local key and a rewritten Host by TunnelClient (#1588), so the IDE prefix needs no exception: the check runs first, the forward runs second. Do not add the prefix to the public-route list; the only sanctioned carve-out precedent is the nonce-authenticated OAuth callback (#1571), and the IDE path has no equivalent one-shot secret.

Consequences for the design:

  • The forward is a post-auth handler. Loopback callers on the laptop must present the local key too, exactly as they do for /api/*; the IDE server's own connection token (Tower-owned token file) is a second, inner layer and not a substitute.
  • Build the auth decision behind a swappable seam. tower: per-session remote credential for cloud-proxied Tower access #1589 (per-session remote credential) is expected to replace the local-key stamp for remote browsers. The forward should call one "is this request authorised for prefix P?" function whose implementation can move from local-key to per-session credential without touching the proxying code. Header stripping/stamping happens after that seam, never before.
  • The origin guard (isWebSocketAllowed) remains in addition to, not instead of, the key check.

Activity

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

    @amrmelsayed
    CollaboratorAuthor

    Routing note from the tower contract seat (resent here after an afx send truncation — fittingly, the #1567-class defect; the running local Tower predates PR #1644):

    The forward must compose with Tower's request-auth choke point, not bypass it. Since the 3.3.1 hardening (GHSA advisory), every non-public route requires the shared local key plus the Host check — one HTTP choke point all routes pass through — and #1588 stamps those onto tunnel-borne requests. An /ide/ prefix that skips the key check would reopen the exact hole 3.3.1 closed, through the same tunnel. So the origin-guard section here should state explicitly which of the two sanctioned shapes the IDE path takes:

    1. Key-authenticated like every other route (the tunnel client already stamps the key on tunnel-borne requests, so a browser arriving via the cloud proxy can pass the check), or
    2. A deliberately public carve-out with its own authentication, following the Fix #1570: allow the cloud OAuth callback through Tower's local-key check #1571 precedent (the OAuth callback's nonce-authenticated public route) — narrow, explicit, and documented as such.

    Additionally: build the forward's auth behind a swappable seam. #1589 (per-session remote credential for cloud-proxied access) is open and is expected to replace the shared-local-key stamp for remote browsers; a forward hard-wired to today's key shape would need rework the moment that lands.

  3. amrmelsayed commented on Sep 11, 2026

    @amrmelsayed
    CollaboratorAuthor

    Scope addition from the fork side, agreed in principle by the fork's owner: besides routing, Tower should install the IDE server on demand — download the release artifact matching the host platform/arch and the installed Codev version into Tower's own directory, verify its checksum, and keep it (the Remote-SSH pattern: the client never builds anything on the host). The fork is adding a CI pipeline that publishes those artifacts as GitHub Release assets (codev-ide-server-<platform>-<arch>-<version>-<sha>.tar.gz + .sha256). Suggest afx ide install [--version] alongside start|stop|status, with the artifact base URL configurable. Routing (this issue as filed) stays the un-substitutable half; install-on-demand can be its own sub-lane.

  4. amrmelsayed commented on Sep 11, 2026

    @amrmelsayed
    CollaboratorAuthor

    Contract-seat notes on the install-on-demand scope addition (endorsed overall; routing-first sequencing agreed — the forward remains the un-substitutable half and should land first):

    1. Name what sha256 does and doesn't buy. The .sha256 ships from the same origin as the tarball, so verification protects transfer integrity, not source compromise. That's acceptable for a GitHub-Releases origin — but the issue should say so explicitly rather than let "verified" imply more. If stronger provenance is ever wanted, that's a separate decision (signing), not a bigger hash.
    2. The configurable artifact base URL is a trust switch, not a convenience knob. Pointing it elsewhere means "install and run binaries from this new origin." Recommended posture: non-default base URL is logged loudly at install time and shown in afx ide status; codev doctor surfaces it. A silent config edit should never silently change where executable code comes from.
    3. Fail loud on version mismatch. "Artifact matching installed Codev version" must mean exact-match-or-error — no nearest-version auto-pick. A version skew between Tower and the IDE server is exactly the class of drift the lockstep bump exists to prevent (vscode: bump the engine floor to ^1.128 — Cursor rebased its VS Code base from 1.105 to 1.128 #1608's rationale applies).
    4. Disk lifecycle. Keeping installed artifacts (Remote-SSH pattern) is right; add the small bookkeeping: afx ide status lists installed versions, and either an afx ide install --prune or a doctor note covers stale ones. Not a blocker — just don't let ~/.agent-farm become an unbounded binary graveyard silently.

    FYI received re codev-cloud's prefix CSP + forwarded headers being live in production — noted that once the forward lands, the manual launcher path retires entirely.

  5. amrmelsayed commented on Sep 11, 2026

    @amrmelsayed
    CollaboratorAuthor

    On it! Working on this with the PIR protocol (plan + dev-approval gates before PR).

  6. added 10 commits that reference this issue on Sep 11, 2026
  7. 6 remaining items

  8. amrmelsayed commented on Sep 15, 2026

    @amrmelsayed
    CollaboratorAuthor

    Dev-run finding (Tower on builder/pir-1668, real server-web build, cloud tower): /t/<tower>/ide/ returns 403 Forbidden.

    Root cause: Tower spawns the IDE server with --connection-token-file (ide-record.ts → ensureIdeConnectionToken), but ide-forward.ts never presents that token upstream — buildForwardHeaders, forwardIdeHttp and forwardIdeWebSocket carry no tkn/cookie handling. So Tower's own key check passes and the VS Code server then rejects the request.

    Local repro against Tower on 4100 (with codev-tower-key):

    request upstream result
    GET /ide/ 403
    GET /ide/?tkn=<token> 302 (token accepted, cookie redirect)
    GET /ide/ + Cookie: vscode-tkn=<token> 200

    With key + cookie supplied by the client, a headless Chromium boot through the forward brings up the full workbench in ~5 s with the explorer populated — so the rest of the forward (headers, WS upgrade, prefix routing) is sound. The defect is only the missing token.

    Fix options for the builder:

    • (a) Inject the token on every forwarded request — merge vscode-tkn=<token> into the Cookie header in buildForwardHeaders (HTTP and WS upgrade alike). The server embeds the token into the page when the root request carries it, so the management/ext-host WebSocket handshake (msg1.auth) then validates too.
    • (b) Spawn with --without-connection-token and drop the token file, since plan §D2 already makes the Tower key the whole boundary.

    Either way, do not rely on the browser holding the token: the server's own Set-Cookie uses Path=<server-base-path> (/ide), which never matches /t/<tower>/ide/ through the relay. (a) keeps defense in depth; recommended.

    Merge is on hold until this is fixed and the dev-run repeated.

  9. amrmelsayed commented on Sep 15, 2026

    @amrmelsayed
    CollaboratorAuthor

    Fix ruling from the tower contract seat on the dev-run 403 (report above): option (a) — inject vscode-tkn=<token> into the Cookie header in buildForwardHeaders for both HTTP and WS upgrade, merge-not-clobber. Option (b) rejected for a reason the report doesn't state: the connection token is the auth boundary for direct 127.0.0.1:8200 access — --without-connection-token would leave an unauthenticated any-folder IDE open to any local process, bypassing Tower's key entirely. §D2's key-is-the-boundary applies to the forward path; the token guards the path that skips Tower. Both stay. The report's Path=/ide cookie observation is exactly why forward-layer injection is the correct fix layer (the browser can never hold the cookie through the relay). Regression test required: injected cookie on both paths + merge semantics. Merge holds until codev-ide's cloud re-test passes, per the owner.

  10. added 9 commits that reference this issue on Sep 15, 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