Repository navigation
Tighten bb connect share hosts, tunnel ownership, pairing codes, and tunnel backpressure - #5096
Merged
Merged
Conversation
…tunnel backpressure The gate answered its own /api/connect/* endpoints on every host, including <label>--<port> share hosts, so those four paths never reached a shared app and the account endpoints were reachable from share-host pages. They are now handled only on server and machine hosts; on a share host they are forwarded like any other path. A tunnel object is keyed by label for servers, and it never checked that the connected tunnel belonged to the server the gate resolved for the visitor. The gate now sends the resolved server or machine id with each forwarded request, and the object answers offline when the connected tunnel was dialed by a different one. /api/connect/redeem refused only server-link codes, so a machine-pair code was accepted and minted a server credential. It now accepts server-pair codes only. The tunnel client sent response bodies without watching the socket's send buffer, so a large response queued in memory and delayed heartbeats behind it. It now pauses reading the origin while more than 1 MiB is buffered. Co-Authored-By: Claude Code <[email protected]>
SawyerHood
marked this pull request as ready for review
October 7, 2026 17:19
The host daemon type-checks the tunnel client against its own ambient declaration of ws, which did not declare the send-buffer size the tunnel client now reads. Co-Authored-By: Claude Code <[email protected]>
A chunk already read from the origin can still be sent after the stream is canceled, so the test now lets the session settle before checking that no further frames arrive. Co-Authored-By: Claude Code <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
Four independent problems in bb connect, found while reviewing the gate and tunnel code:
/api/connect/*endpoints before it parsed the host, so it answered them on<label>--<port>share hosts too. Those four paths never reached the shared app, and a page served from a share host could call the account endpoints same-origin./api/connect/redeemrefused onlyserver-linkcodes, so amachine-paircode was accepted there and minted a server credential.What changed
apps/connect/src/worker.ts: the/api/connect/*endpoints are handled only when the host is a server or machine host (or unparseable, as before). On a share host they are forwarded to the shared app like any other path.apps/connect/src/worker.ts,protocol-headers.ts,tunnel-do.ts: the gate sends the resolved owner (server:<id>ormachine:<id>) to the tunnel object in a new internal header. The object remembers which server or machine dialed (in memory, restored from the ids it already stores) and answers with the offline response when they differ. The header is stripped from visitor requests and is not forwarded to the origin.apps/web/src/server/api.ts:redeemConnectCodeacceptsserver-paircodes only.packages/tunnel-client/src/session.ts: while streaming an origin response, the client waits when more than 1 MiB is buffered on the tunnel socket, and stops waiting when the stream is canceled or the tunnel closes.apps/host-daemon/src/ws.d.ts: the daemon's own declaration ofwsnow declaresbufferedAmount, which the tunnel client reads.No tunnel protocol or daemon wire change, so
HOST_DAEMON_PROTOCOL_VERSIONis unchanged. No CLI, guide, or doc changes.Compatibility notes:
/api/connect/*on a server or machine host, and redeems machine codes at/api/connect/redeem-machine, so none needs an update.How you verified
Tests added:
apps/connect/src/worker.test.ts: the four endpoints on a share host are forwarded and not answered by the gate; the owner header is set for server and machine visitors and overrides a visitor-supplied value; the tunnel object answers offline for a different owner (plain HTTP and upgrades), forwards for the same owner without leaking the header to the origin, and forgets the owner after a close.apps/web/src/server/api.test.ts: a machine code is refused at the server redeem endpoint, the server credential is unchanged, and the code still redeems for the machine.packages/tunnel-client/test/backpressure.test.ts: reading the origin stops while the send buffer is backed up and resumes after, and waiting ends when the stream is canceled. Both fail with the change removed.Commands:
pnpm exec turbo run test --filter=@bb/connect --filter=@bb/web --filter=@bb/tunnel-client --filter=bb-plugin-connect, the host daemon's connect-tunnel test, andpnpm exec turbo run lint typecheck --filter='...[origin/main]'for the changed packages and their dependents; all pass.Staging (gate deployed to
bb-connect-staging, a real bb server from this branch paired to a fresh label, the real app driven in headless Chromium):/api/connect/*paths on that host are answered by the shared app.Not exercised on staging: the bb-web change (not deployed there), the owner-mismatch refusal (unit tests only), a separately enrolled machine's tunnel, and the desktop and mobile apps.
🤖 Generated with Claude Code