Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): port refusals survive T3 Connect's relay #39763

Mergedjuliusmarmingemerged 1 commit intomain fromrefs/pull/39763/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

Over T3 Connect, a desktop app connected to another machine couldn't tell whether that machine shares its loopback, so it hid 'Forward to this machine' and port forwarding failed with 'can't be reached'. The environment answered a refused loopback port with HTTP 502 and a marker header, and Cloudflare, in front of the relay, swaps every 502 for its own page and drops the header. The environment now answers 403 with the same header, the status it already uses for its other port refusal, and the relay passes 403s through untouched.

Fixes pingdotgg/t3code#39750

Reproduction on main

Commit e74b795535 (origin/main). Linked a fresh server from main to the T3 Connect test stage (t3 connect link --headless --base-dir <scratch>, headless Clerk sign-in as the factory test account), then t3 serve --port 38917; the managed cloudflared tunnel registered (QUIC) and t3 connect status reported it reachable. Minted a pairing token (t3 pair --scope terminal:operate --scope preview:operate) and exchanged it at https://.t3coderelay.com/api/auth/browser-session for a session cookie. Then ran a driver that mirrors LoopbackTunnel.dialDestination's probe (ws 8.22 WebSocket to /api/preview/tunnel?port=, reading unexpected-response). Direct to loopback (http://127.0.0.1:38917, port 58209): 502 with x-t3-tunnel-port-refused: 1 and body 'Nothing accepted a connection on loopback port 58209.' Through the relay (ports 58209, 58210 and 1): 502 with server: cloudflare, body 'error code: 502', and no x-t3-tunnel-port-refused header. The server's own trace shows it sent 502 for those relayed requests, so Cloudflare replaced the origin's 5xx response. A 403 from the same route (the insufficient-scope error) passed through the relay with the server's own body, so only the 5xx is rewritten. Since the desktop treats the probe as remote only when it sees that header, sharesLoopback fails over T3 Connect, locality becomes unknown, the forward button is hidden and forwardPort reports 'can't be reached'. I did not drive the desktop GUI against the relay; the probe driver stands in for that call.

Evidence

Real Linux Electron apps, 1100×780, light theme, fresh synthetic state, and separate server loopback namespaces connected through the real T3 Connect test stage. On both sides, open Thread details → Servers and Ctrl-click localhost:58209. Base hides the forwarding control and fails; head restores the control and creates a working forward. The dedicated Forward button on head also succeeds, and a separate browser loads the remote page through desktop localhost.

BeforeAfter
391f93fc8fcd17ca740e
Before: forwarding control missingAfter: forwarding control restored
Before: environment cannot be reachedAfter: dedicated control forwards successfully
Before interactionSame interaction on head, dedicated Forward button

After: remote demo reached through the desktop's local listener

The relay probe returned Cloudflare 502 without the marker on base, versus 403 with x-t3-tunnel-port-refused: 1 on head. The head forward returned HTTP 200 and the remote demo body. Runtime observations.

Remaining behavior: Ctrl-click on head creates a working forward but still displays an additional 'Unable to open in browser' error; see the same-interaction recording. The dedicated Forward control works.

Scripts: seed, capture, desktop driver, relay probe. Evidence uploaded using the ghdrop skill.

Not shown:

  • Automatic external-browser opening is not fixed in the exercised Ctrl-click path: head creates a working forward but then shows 'Unable to open in browser'. Dedicated forwarding and loading the resulting localhost address were verified.
  • The server and desktop used separate network namespaces on one physical Linux host rather than two physical machines; the real Cloudflare relay and distinct loopbacks were exercised.

Tests

  • vp test run src/preview/PreviewTunnel.test.ts (apps/server): 9 passed, including the updated 'answers a port nothing listens on with 403, marked as about the port'
  • vp test run src/preview/LoopbackTunnel.test.ts (apps/desktop): 16 passed
  • vp run check:changed: format, lint, knip, typecheck (3 packages) and tests (2 files) all pass
  • CI on cd17ca7: all 8 required checks passed

Not in this change

  • The desktop test fixture listenRefusingEndpoint still refuses with 502; it checks only the header, so it is still a valid stand-in for a proxy's 502 without the header.
  • End-to-end verification through the T3 Connect relay and on mobile devices was not run; it relies on the reporter's observation that a 403 from this route passes through intact.

Who did the work

  • Written by Claude Opus 5.5, in Claude Code
  • Reviewed by GPT-6.1 Sol, in Codex, and by Claude Opus 5.5, each on its own
  • Proof captured by GPT-6.1 Sol and checked by Claude Opus 5.5

Run by the t3code factory in T3 Code.

    1. tanstack-source added labelsbug

    2. Thread transfer impact

      ✅ Thread transfer remains within every enforced ceiling.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      CodexThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      CodexLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      CodexLive turn WebSocket decoded21.5 KiB21.5 KiB0 B (0.0%)24.4 KiB✅
      CodexLive turn messages110 (0.0%)8✅
      ClaudeTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      ClaudeThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      ClaudeLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      ClaudeLive turn WebSocket decoded21.9 KiB21.9 KiB0 B (0.0%)24.4 KiB✅
      ClaudeLive turn messages110 (0.0%)8✅

      Other scenarios

      All 64 scenario measurements match main.

      All 64 scenario measurements
      ProviderScenarioMetricMain baselineThis PRImpactPR ceiling
      Codexshort turn resumeWebSocket decoded10.5 KiB10.5 KiB0 B (0.0%)12.7 KiB✅
      Codexshort turn resumeWebSocket messages220 (0.0%)2✅
      Codexlong turn running, cold openHTTP decoded281.7 KiB281.7 KiB0 B (0.0%)302.7 KiB✅
      Codexlong turn running, cold openrows75750 (0.0%)75✅
      Codexlong turn running, history pageHTTP decoded92.4 KiB92.4 KiB0 B (0.0%)107.4 KiB✅
      Codexlong turn running, history pagerows75750 (0.0%)75✅
      Codexlong turn finished, cold openHTTP decoded381.5 KiB381.5 KiB0 B (0.0%)410.2 KiB✅
      Codexlong turn finished, cold openrows1511510 (0.0%)151✅
      Codexlong turn finished, socket cold openWebSocket decoded381.6 KiB381.6 KiB0 B (0.0%)410.2 KiB✅
      Codexlong turn finished, socket cold openWebSocket messages220 (0.0%)2✅
      Codexlong turn finished, socket cold openrows1511510 (0.0%)151✅
      Codexlong turn finished, work pageHTTP decoded186.2 KiB186.2 KiB0 B (0.0%)200.2 KiB✅
      Codexlong turn finished, work pagerows1501500 (0.0%)150✅
      Codexidle thread resumeWebSocket decoded65 B65 B0 B (0.0%)200 B✅
      Codexidle thread resumeWebSocket messages110 (0.0%)1✅
      Codexfleet shell snapshotHTTP decoded42.4 KiB42.4 KiB0 B (0.0%)53.7 KiB✅
      Codexfleet shell snapshotrows27270 (0.0%)27✅
      Codexfleet shell subscribeWebSocket decoded65 B65 B0 B (0.0%)200 B✅
      Codexfleet shell subscribeWebSocket messages110 (0.0%)1✅
      Codexfleet shell resume checkHTTP decoded18 B18 B0 B (0.0%)100 B✅
      Codexfleet shell resume checkrows0000✅
      Codexstale shell reloadHTTP decoded42.4 KiB42.4 KiB0 B (0.0%)53.7 KiB✅
      Codexstale shell reloadrows27270 (0.0%)27✅
      Codexstale shell resumeWebSocket decoded140 B140 B0 B (0.0%)400 B✅
      Codexstale shell resumeWebSocket messages220 (0.0%)2✅
      Codexstale todo list cold openHTTP decoded72.4 KiB72.4 KiB0 B (0.0%)83.0 KiB✅
      Codexstale todo list cold openrows75750 (0.0%)75✅
      Codexstale todo list cold opennodes220 (0.0%)4✅
      Codexstale todo list cold opensubagents220 (0.0%)4✅
      Codexstale todo list cold openplans110 (0.0%)2✅
      Codexstale todo list cold openproviderTurns110 (0.0%)2✅
      Codexstale todo list cold opencheckpoints110 (0.0%)2✅
      Claudeshort turn resumeWebSocket decoded10.6 KiB10.6 KiB0 B (0.0%)12.7 KiB✅
      Claudeshort turn resumeWebSocket messages220 (0.0%)2✅
      Claudelong turn running, cold openHTTP decoded283.3 KiB283.3 KiB0 B (0.0%)302.7 KiB✅
      Claudelong turn running, cold openrows75750 (0.0%)75✅
      Claudelong turn running, history pageHTTP decoded93.4 KiB93.4 KiB0 B (0.0%)107.4 KiB✅
      Claudelong turn running, history pagerows75750 (0.0%)75✅
      Claudelong turn finished, cold openHTTP decoded384.2 KiB384.2 KiB0 B (0.0%)410.2 KiB✅
      Claudelong turn finished, cold openrows1511510 (0.0%)151✅
      Claudelong turn finished, socket cold openWebSocket decoded384.3 KiB384.3 KiB0 B (0.0%)410.2 KiB✅
      Claudelong turn finished, socket cold openWebSocket messages220 (0.0%)2✅
      Claudelong turn finished, socket cold openrows1511510 (0.0%)151✅
      Claudelong turn finished, work pageHTTP decoded188.1 KiB188.1 KiB0 B (0.0%)200.2 KiB✅
      Claudelong turn finished, work pagerows1501500 (0.0%)150✅
      Claudeidle thread resumeWebSocket decoded66 B66 B0 B (0.0%)200 B✅
      Claudeidle thread resumeWebSocket messages110 (0.0%)1✅
      Claudefleet shell snapshotHTTP decoded43.0 KiB43.0 KiB0 B (0.0%)53.7 KiB✅
      Claudefleet shell snapshotrows27270 (0.0%)27✅
      Claudefleet shell subscribeWebSocket decoded66 B66 B0 B (0.0%)200 B✅
      Claudefleet shell subscribeWebSocket messages110 (0.0%)1✅
      Claudefleet shell resume checkHTTP decoded18 B18 B0 B (0.0%)100 B✅
      Claudefleet shell resume checkrows0000✅
      Claudestale shell reloadHTTP decoded43.0 KiB43.0 KiB0 B (0.0%)53.7 KiB✅
      Claudestale shell reloadrows27270 (0.0%)27✅
      Claudestale shell resumeWebSocket decoded140 B140 B0 B (0.0%)400 B✅
      Claudestale shell resumeWebSocket messages220 (0.0%)2✅
      Claudestale todo list cold openHTTP decoded73.4 KiB73.4 KiB0 B (0.0%)83.0 KiB✅
      Claudestale todo list cold openrows75750 (0.0%)75✅
      Claudestale todo list cold opennodes220 (0.0%)4✅
      Claudestale todo list cold opensubagents220 (0.0%)4✅
      Claudestale todo list cold openplans110 (0.0%)2✅
      Claudestale todo list cold openproviderTurns110 (0.0%)2✅
      Claudestale todo list cold opencheckpoints110 (0.0%)2✅

      Baseline: 391f93f · PR result: cd17ca7 · Source CI: success

      Scenario and decoded snapshot size

      10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

      • Codex decoded thread snapshot: 108.5 KiB
      • Claude decoded thread snapshot: 108.8 KiB

      Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Explain Change →

      No actionable comments were generated in the recent review.

      Walkthrough

      The preview tunnel now returns 403 instead of 502 when a connection error indicates a refused port. The unreachable-port test expects 403 and continues to check the port-refused marker.

      Changes: Preview tunnel refusal response

      Layer / File(s)Summary
      Update refusal response and test
      apps/server/src/preview/PreviewTunnel.ts, apps/server/src/preview/PreviewTunnel.test.ts
      The PreviewTunnelConnectError response status changes from 502 to 403. The response message and port-refused header remain unchanged. The unreachable-port test expects 403 and retains its port-refused marker assertion.

      Estimated code review effort: 1 (Trivial) | ~5 minutes

      Change: Bug fix · Severity of issue fixed: Medium

      Possibly related PRs

      Suggested reviewers: macroscopeapp

      Merge Risk: Minimal · up to cd17c

      Refused loopback ports are now reported to clients with HTTP 403 plus the existing marker header. Client classifiers in this repository key on the marker alone, so no in-repo regression is evident. Whether the marker survives the T3 Connect relay and Cloudflare was not run end to end, so a manual check of the forward-to-this-machine flow is advisable. It does not block merge.

      Pre-merge checks: Passed 4

      Check nameStatusExplanation
      Title checkPassedThe title "fix(server): port refusals survive T3 Connect's relay" is a conventional commit that names the main change: the server's port-refusal response now survives the relay. The changeset switches…
      Linked Issues checkPassedDirectly linked issue #39750 requires that a desktop app over T3 Connect recognise a refused loopback-port probe. The root cause is that Cloudflare replaces an origin 502 and drops the x-t3-tunnel-por…
      Out of Scope Changes checkPassedThe supplied changes are limited to apps/server/src/preview/PreviewTunnel.ts (status for PreviewTunnelConnectError and PreviewTunnelPortRefusedError changed from 502 to 403, marker retained) and Previ…
      Description checkPassedThe description is thorough and covers the problem, the change, scope, and verification. It explains that a refused loopback port returned HTTP 502 with a marker header, that Cloudflare replaced the 5…
      Full details: Title check

      Explanation

      The title "fix(server): port refusals survive T3 Connect's relay" is a conventional commit that names the main change: the server's port-refusal response now survives the relay. The changeset switches the refused-port response from 502 to 403 and makes the relay pass 403 through, so the title matches the main change. It is specific enough for a teammate scanning history, though it does not say the status code changed.

      Full details: Linked Issues check

      Explanation

      Directly linked issue #39750 requires that a desktop app over T3 Connect recognise a refused loopback-port probe. The root cause is that Cloudflare replaces an origin 502 and drops the x-t3-tunnel-port-refused header. At the reviewed head, apps/server/src/preview/PreviewTunnel.ts maps PreviewTunnelConnectError to status 403 with PORT_REFUSED_HEADERS, and the route handler carries a comment explaining the change. The desktop probe (per the issue and PR description) treats the probe as remote when it sees the marker header, and does not depend on the 502 status, so the 403 response keeps the marker through the relay. The test PreviewTunnel.test.ts is updated to expect 403 with the port-related marker. Residual uncertainty: the relay hop and the desktop GUI were not exercised end to end in this repository. The relay pass-through of 403 rests on the reporter's observation, and no relay code is present in the reviewed evidence. The whole-PR file diff could not be read (changed blobs unavailable), so the change is confirmed from the head file contents and the supplied summary.

      Full details: Out of Scope Changes check

      Explanation

      The supplied changes are limited to apps/server/src/preview/PreviewTunnel.ts (status for PreviewTunnelConnectError and PreviewTunnelPortRefusedError changed from 502 to 403, marker retained) and PreviewTunnel.test.ts (expectation updated to 403). Both directly implement the objective of #39750. The head file shows no unrelated edits in the route handler. The PR description states a relay change, but no relay code appears in the reviewed evidence, so no unrelated relay change is shown. The whole-PR diff could not be read, so completeness of the change list rests on the supplied summary.

      Full details: Description check

      Explanation

      The description is thorough and covers the problem, the change, scope, and verification. It explains that a refused loopback port returned HTTP 502 with a marker header, that Cloudflare replaced the 502 and dropped the header, and that the environment now answers 403 with the same header. It links issue #39750 and includes reproduction steps, before/after evidence, test commands with results, and a list of remaining behavior and untested areas. The template's Problem, Change, Scope, and Verification sections are all present. The author also lists the model and harness used, which the template requests. The title is absent from the template comments, but the template asks for a conventional commit title and the PR objectives do not state one, so that is not a required section in the description body. The description is long, but it is complete.

      Finishing Touches

      Generate unit tests (beta)

      • Commit to this branch
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
      Recent review info

      Run configuration

      Commits

      Reviewing files that changed from the base of the PR and between 391f93fc8fd851a4c2a655ffb8ea64698470d2e2 and cd17ca740e7501b7810aba691a3b8c08589c8d10.

      Files selected for processing: 2

      • apps/server/src/preview/PreviewTunnel.test.ts
      • apps/server/src/preview/PreviewTunnel.ts

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

    4. juliusmarminge merged this563ba1a

    5. juliusmarminge closed this

Sign in to join this conversation.