fix(server): port refusals survive T3 Connect's relay #39763
main fromrefs/pull/39763/head·merged by juliusmarminge View on GitHubOver 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.
| Before | After |
|---|---|
| 391f93fc8f | cd17ca740e |
![]() | ![]() |
![]() | ![]() |
| Before interaction | Same interaction on head, dedicated Forward button |

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 passedvp 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
listenRefusingEndpointstill 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
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XS
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.tsThe PreviewTunnelConnectErrorresponse 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
- pingdotgg/t3code#37947: Adds the preview tunnel route and response behavior that this change updates.
Suggested reviewers:
macroscopeapp
Merge Risk:
Minimal · up tocd17c-
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:
4-
Check name Status Explanation Title check 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… Linked Issues check Directly linked issue #39750requires 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 check 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 Previ… Description check 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 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
#39750requires 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
#39750and 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-
- Source mode: Auto
- Repository: pingdotgg/t3code
- Branch: factory/issue-39750
- File: .coderabbit.config.ts
- Commit: cd17ca7
- Layers: Repository TS config
- Review profile: CHILL
- Plan: Team
- Run ID:
aa286707-5283-40f2-9968-94b63b1e7095
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.tsapps/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.
-
-
juliusmarminge merged this563ba1a
juliusmarminge closed this
Sign in to join this conversation.




Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Other scenarios#
All 64 scenario measurements match
main.All 64 scenario measurements
short turn resumeWebSocket decodedshort turn resumeWebSocket messageslong turn running, cold openHTTP decodedlong turn running, cold openrowslong turn running, history pageHTTP decodedlong turn running, history pagerowslong turn finished, cold openHTTP decodedlong turn finished, cold openrowslong turn finished, socket cold openWebSocket decodedlong turn finished, socket cold openWebSocket messageslong turn finished, socket cold openrowslong turn finished, work pageHTTP decodedlong turn finished, work pagerowsidle thread resumeWebSocket decodedidle thread resumeWebSocket messagesfleet shell snapshotHTTP decodedfleet shell snapshotrowsfleet shell subscribeWebSocket decodedfleet shell subscribeWebSocket messagesfleet shell resume checkHTTP decodedfleet shell resume checkrowsstale shell reloadHTTP decodedstale shell reloadrowsstale shell resumeWebSocket decodedstale shell resumeWebSocket messagesstale todo list cold openHTTP decodedstale todo list cold openrowsstale todo list cold opennodesstale todo list cold opensubagentsstale todo list cold openplansstale todo list cold openproviderTurnsstale todo list cold opencheckpointsshort turn resumeWebSocket decodedshort turn resumeWebSocket messageslong turn running, cold openHTTP decodedlong turn running, cold openrowslong turn running, history pageHTTP decodedlong turn running, history pagerowslong turn finished, cold openHTTP decodedlong turn finished, cold openrowslong turn finished, socket cold openWebSocket decodedlong turn finished, socket cold openWebSocket messageslong turn finished, socket cold openrowslong turn finished, work pageHTTP decodedlong turn finished, work pagerowsidle thread resumeWebSocket decodedidle thread resumeWebSocket messagesfleet shell snapshotHTTP decodedfleet shell snapshotrowsfleet shell subscribeWebSocket decodedfleet shell subscribeWebSocket messagesfleet shell resume checkHTTP decodedfleet shell resume checkrowsstale shell reloadHTTP decodedstale shell reloadrowsstale shell resumeWebSocket decodedstale shell resumeWebSocket messagesstale todo list cold openHTTP decodedstale todo list cold openrowsstale todo list cold opennodesstale todo list cold opensubagentsstale todo list cold openplansstale todo list cold openproviderTurnsstale todo list cold opencheckpointsBaseline:
391f93f· PR result:cd17ca7· Source CI: successScenario 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.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.