Repository navigation
connect-next 1/5: account API for the Fly gate, next MCP resources, gate keys - #6047
Merged
Merged
Conversation
…keys Contract (@bb/tunnel-contract/next), D1 migration 0010 for next tunnel presence, gate resolve/presence routes (single and batch) on the bb-connect Worker behind GATE_SECRET, bb-web gate key set and next MCP OAuth resources, local Cloud variables, and the design note docs/connect-next.md.
SawyerHood
force-pushed
the
connect-next/1-account-api
branch
from
October 21, 2026 16:53
31c5eec to
f57a0be
Compare
SawyerHood
added a commit
that referenced
this pull request
Oct 21, 2026
…#6048) ## 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
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
bb connect only reaches a bb server through the Cloudflare Worker and its per-label Durable Object tunnel (
apps/connect). bb connect next is an opt-in second way in,https://<label>.next.getbb.app, through our own gate on Fly (apps/connect-next, PR 3 of this stack). Accounts, servers, machines, credentials and OAuth grants stay in Cloudflare D1, so the Fly gate needs an authenticated account API to admit visitors and frp clients, a place to report tunnel presence, and a published public key set that bb servers use to verify the per-request assertions the gate signs. This PR is that account side; nothing uses it until the gate and the plugin land. Design:docs/connect-next.md.This is PR 1 of 5 (stack: account API → server ingress → gate → plugin → machine tunnels).
What changed
packages/tunnel-contract(@bb/tunnel-contract/next): names, wire types and strict parsers for the gate account API (GateResolveRequest/Response, presence, batch variants), and Ed25519 gate assertion signing/verification (GateAssertionClaims: 60 s, audienceserver:<id>/machine:<id>, bound to method and path-and-query, share target).packages/connect-db: migration0010_next_tunnel_presence(nullablenext_tunnel_gate,next_tunnel_region,next_tunnel_aton servers and machines; snapshot generated with drizzle-kit), reserved handles for the next hosts, presence write rules (tunnel-status.ts), OAuth resource check for next MCP resources.apps/connect(Workerbb-connect):POST /api/gate/resolve,/api/gate/resolve/batch(≤ 50),/api/gate/presence,/api/gate/presence/batch(≤ 500), only onconnect.<BASE_DOMAIN>, behindAuthorization: Bearer <GATE_SECRET>(constant-time); every route answers404while theGATE_SECRETsecret is unset. A resolve batch reads its labels with one server and one machine query. Dashboard online state also counts a fresh next-tunnel presence. VarNEXT_BASE_DOMAIN.apps/web(bb-web):GET /.well-known/bb-connect-gate-keys.jsonfrom theGATE_PUBLIC_KEYSvar (404 while unset; staging holds the staging gate's keystaging-2026-10-20, production deliberately unset),/oauth/authorizeand/oauth/tokenaccepthttps://<label>.<NEXT_BASE_DOMAIN>/mcpas a resource and bind the grant to it exactly.scripts/bb-cloud-dev.mjs:NEXT_BASE_DOMAIN,BB_CLOUD_DEV_GATE_SECRET,BB_CLOUD_DEV_STATE_DIRfor local runs;docs/debugging-and-qa.md.docs/connect-next.md: the design and operating note for the whole stack.Deploy impact on merge: merging deploys the production
bb-connectWorker and applies D1 migration0010tobb-connect-prod(nullable columns only, no backfill); the new/api/gate/*endpoints answer 404 until theGATE_SECRETWorker secret is set. It also deploys bb-web; productionGATE_PUBLIC_KEYSstays unset, sohttps://getbb.app/.well-known/bb-connect-gate-keys.jsonanswers 404 and no server trusts any gate yet.How you verified
pnpm exec turbo run typecheck --filter='...[origin/main]': 16/16 tasks pass.pnpm exec turbo run test --filter='[origin/main]':@bb/tunnel-contract22/22,@bb/connect-db47/47,@bb/connect298/298 (incl.gate-api.test.ts: auth, shapes, batches, presence write rules),@bb/web350/350.pnpm exec oxfmt --checkon the changed files,pnpm lint: clean.apps/web/src/routeTree.gen.ts) was regenerated byvite buildand matches.apps/connect-next/scripts/e2e-local.shruns local Cloud (this Worker and bb-web under Miniflare with a fresh D1 and the migrations), the gate, a bb server and daemons; results are in PR 5.bb-connect-stagingwith the gatebb-connect-next-stagingthrough 2,500-tunnel load tests (see PR 3).