Skip to content

connect-next 2/5: server tunnel ingress that verifies gate assertions - #6048

Merged
SawyerHood merged 1 commit into
connect-next/1-account-apifrom
connect-next/2-server-ingress
Oct 21, 2026
Merged

SawyerHood merged 1 commit into
connect-next/1-account-apifrom
connect-next/2-server-ingress

Conversation

@SawyerHood

@SawyerHood SawyerHood commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Through bb connect next, a request reaches the bb server from a local frpc that the Fly gate drives, so it arrives on loopback. The main loopback port trusts local callers, so frpc cannot point at it: anyone who can reach the frp tunnel would be treated as local. The server needs a separate ingress that only accepts requests the gate signed for this server, and turns the gate's claims into the same request context the Cloudflare path produces (end-to-end auth: the gate cannot be bypassed by talking to frp directly, and the peer secret or a stolen tunnel alone reaches nothing).

PR 2 of 5 (stack: account API → server ingress → gate → plugin → machine tunnels). Based on #6047.

What changed

  • apps/server/src/services/connect-next/tunnel-ingress.ts: per-audience loopback listener (ephemeral port, shared per audience, freed with the last handle). Every HTTP request and WebSocket upgrade must carry an x-bb-gate-assertion for server:<serverId>, the request method and exact path and query; anything else is 401 and runs nothing. A verified request reaches the same app with loopback host/origin, authenticated only by the claims (getGateAuthKind); public install assertions reach only the install paths, without cookies or credentials. Verified tunnel traffic is never treated as coming from this machine (request-context.ts).
  • gate-keys.ts: fetches and caches the gate key set from BB_CONNECT_GATE_KEYS_URL (default https://getbb.app/.well-known/bb-connect-gate-keys.json), refetches once for an unknown kid, keeps the last good set when the URL fails, fails closed without keys.
  • Plugin SDK: bb.server.internal_DO_NOT_USE_openTunnelIngress({ audience }) (+ fake host support and InternalDoNotUsePluginTunnelIngress* types). Handles close when the plugin is disabled or unloaded.
  • PLUGIN_SDK_VERSION 0.6.65 → 0.6.66 (packages/domain, packages/plugin-sdk/package.json) for the new SDK member.
  • packages/config: BB_CONNECT_GATE_KEYS_URL; docs/configuration.md.
  • AGENTS.md: the internal_DO_NOT_USE_ prefix rule for first-party-only plumbing; docs/api_to_audit.md entry naming its only caller (connect-next).
  • plugin-authoring-docs.test.ts: the "every public export is documented in the authoring skill" check skips InternalDoNotUse* types, which are deliberately not documented as plugin API.

How you verified

  • pnpm exec turbo run typecheck --filter='...[connect-next/1-account-api]': 76/76 tasks pass.
  • pnpm exec turbo run test --filter='[connect-next/1-account-api]': @bb/config 101 passed / 1 skipped, @get-bb/plugin-sdk 390/390, @bb/integration-tests 77/77, @bb/server 3,637 passed / 1 skipped (new test/connect-next/tunnel-ingress.test.ts: verified requests by auth kind, refusals without/with wrong assertions, loopback host/origin for plugin routes, public install assertions, WebSocket upgrades for /ws and plugin sockets, key rotation and fail-closed, listener sharing, closing on plugin disable).
  • The test that carries gate-signed requests through real frps and the connect-next plugin's frpc lives in PR 4, where the plugin exists.
  • oxfmt --check, pnpm lint: clean.

AGENT GENERATED

bb.server.internal_DO_NOT_USE_openTunnelIngress opens a per-audience loopback
listener that only runs requests carrying a valid gate-signed assertion for
this server, method and path. Gate keys come from BB_CONNECT_GATE_KEYS_URL.
Adds the internal_DO_NOT_USE_ prefix rule to AGENTS.md and its audit entry.
@SawyerHood
SawyerHood force-pushed the connect-next/2-server-ingress branch from 77be18b to de40291 Compare October 21, 2026 16:54
@SawyerHood
SawyerHood merged commit 9927ea9 into main Oct 21, 2026
95 of 106 checks passed
@SawyerHood
SawyerHood deleted the connect-next/2-server-ingress branch October 21, 2026 17:21
SawyerHood added a commit that referenced this pull request Oct 21, 2026
## Human comments

## What was wrong

bb connect next needs its data plane: a gate on Fly that admits visitors
to `https://<label>.next.getbb.app` with every rule the Cloudflare gate
applies, terminates the frp tunnels bb machines dial, signs a
per-request assertion for the server's ingress (PR 2), and routes a
visitor to whichever Fly machine holds the label's tunnel.

PR 3 of 5 (stack: account API → server ingress → gate → plugin → machine
tunnels). Based on #6048.

## What changed

- `apps/connect-next` (Fly app `bb-connect-next`): Node HTTP gate plus
`frps` 0.71.0 as a loopback child (bind, vhost and server-plugin hooks
`Login`/`NewProxy`/`CloseProxy`/`Ping`). It pipes
`wss://tunnel.<domain>/~!frp` to frps, checks frp logins against the
account API (a client may only claim its own label and shares), admits
visitors with the Cloudflare gate's rules (owner session, desktop
session, machine credential, next-resource MCP OAuth, Origin refusal,
header/cookie stripping, Set-Cookie filtering, offline page with a 15 s
redial grace), signs an Ed25519 `x-bb-gate-assertion` per request and
WebSocket, and refreshes sessions like the Cloudflare gate.
- Load fixes from the staging load tests: batched account API lookups
and presence (coalesced, jittered refreshes, retry with backoff),
stale-while-revalidate answer cache with last-good answers through
account API outages, 16 resolve batches in flight, 3 s drain on shutdown
with `fly-replay: elsewhere=true`, WebSocket close frames for frp
sockets on shutdown, hard exit inside Fly's `kill_timeout`, open-file
limit raised to 65,536.
- Regions (`src/regions.ts`, `src/peer-server.ts`): peers over Fly's
private network (`PEER_SECRET`), announcements and lookups, `fly-replay`
to the holder with HMAC'd state, private-network forwarding for bodies
Fly cannot replay, duplicate-tunnel resolution.
- `fly.toml` `[[vm]]` is `performance-2x` / 4 GB: `flyctl deploy`
applies it to every machine, and the load test showed shared CPUs cannot
carry the gate. `docs/connect-next.md` (Machine size, Machine plan) says
all regions use that size and that staging overrides it with `--vm-size
shared-cpu-2x --vm-memory 1024` on its deploy command.
- `Dockerfile`, workflow `.github/workflows/deploy-connect-next.yml`
(manual `Deploy Connect Next`, pinned flyctl SHA-256).
- `scripts/e2e-local.sh` (+ `e2e-local.ts`, `e2e-peer-gate.ts`): the
whole path on one machine; it needs the plugin (PR 4) and machine
tunnels (PR 5) to pass fully.

No production deploy happens on merge: the workflow is manual.

## How you verified

- `pnpm exec turbo run typecheck test --filter=@bb/connect-next`: 87/87
tests (units, plus real `frps`/`frpc` against a stand-in account API and
bb server in `test/gate.e2e.test.ts`, the Fly replay stand-in in
`test/fly.ts`).
- `oxfmt --check`, `pnpm lint`: clean.
- Staging (`bb-connect-next-staging`, `next.vibecodethis.site`): 2,500
real frpc tunnels. Before the load fixes a shared-cpu-2x 1GB machine
sustained only ~30–40 req/s (CPU throttled), performance-2x 4GB held
2,500 tunnels at ~280 req/s with ~1.3× CPU headroom, and the account API
was flooded by presence writes. After the fixes: visitor errors from the
gate went from ~0.4% to 0, Worker load fell ~15×, no frp Login waits ran
out on a mass reconnect, and a stop with 2,294 tunnels took 0.7 s.
Details in `docs/connect-next.md` (Presence, shutdown and limits;
Regions).
- Local end to end: results on PR 5 (top of the stack).

> AGENT GENERATED
alexandre-vl added a commit to alexandre-vl/bb that referenced this pull request Oct 21, 2026
Main moved the SDK to 0.6.67 (get-bb#6048, get-bb#6051), so this branch's own type
changes need the next version.
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