Skip to content

Tighten bb connect share hosts, tunnel ownership, pairing codes, and tunnel backpressure - #5096

Merged
SawyerHood merged 3 commits into
mainfrom
bb/connect-gate-hardening-thr_6h3rmiba57
Oct 7, 2026
Merged

SawyerHood merged 3 commits into
mainfrom
bb/connect-gate-hardening-thr_6h3rmiba57

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Four independent problems in bb connect, found while reviewing the gate and tunnel code:

  • Gate endpoints on share hosts. The gate matched its own /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.
  • No owner check in the tunnel object. Server tunnel objects are keyed by label alone, and the object never checked that the connected tunnel was dialed by the server the gate resolved for the visitor. A tunnel that stayed attached after its label changed hands (for example when the close on disconnect failed) would keep receiving that label's traffic. Machine tunnels already carry a generation in their key; servers do not.
  • Pairing code purpose. /api/connect/redeem refused only server-link codes, so a machine-pair code was accepted there and minted a server credential.
  • No backpressure in the tunnel client. Response bodies were written to the tunnel socket without watching its send buffer. A large response over a slow uplink was buffered in the bb server's memory, and heartbeats queued behind it until the heartbeat deadline dropped the tunnel.

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> or machine:<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: redeemConnectCode accepts server-pair codes 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 of ws now declares bufferedAmount, which the tunnel client reads.

No tunnel protocol or daemon wire change, so HOST_DAEMON_PROTOCOL_VERSION is unchanged. No CLI, guide, or doc changes.

Compatibility notes:

  • Every client in the repo calls /api/connect/* on a server or machine host, and redeems machine codes at /api/connect/redeem-machine, so none needs an update.
  • A tunnel object with no stored owner still forwards as before. The owner id stored at dial and the id sent with each request come from the same row, so they only differ when the label really points at a different server or machine.
  • After a server is removed and re-created under the same label, its owner can see the offline page for up to 15 seconds while the gate's label cache still holds the old id.

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, and pnpm 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):

  • A tunnel dialed under the previous gate kept serving after the deploy, without a reconnect.
  • App load and reload: no failed responses, no console errors; cached bundles decode to the files on disk for br, gzip, zstd, and identity.
  • A thread created and a message sent from the UI got its model reply.
  • A shared port serves through its share host, and the four /api/connect/* paths on that host are answered by the shared app.
  • On the server host, minting a session and listing servers still work through the connect plugin.
  • Stopping the server gives the offline response; after a restart the fresh dial serves both hosts.
  • A 400 MB download through the shared port matches its checksum with the tunnel staying up; server memory peaked at 289 MB with the change and 677 MB without it.

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

AGENT GENERATED

…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
SawyerHood marked this pull request as ready for review October 7, 2026 17:19
SawyerHood and others added 2 commits October 7, 2026 17:21
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]>
@SawyerHood
SawyerHood merged commit e866cfd into main Oct 7, 2026
47 checks passed
@SawyerHood
SawyerHood deleted the bb/connect-gate-hardening-thr_6h3rmiba57 branch October 7, 2026 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant