Skip to content

connect-next 1/5: account API for the Fly gate, next MCP resources, gate keys - #6047

Merged
SawyerHood merged 1 commit into
mainfrom
connect-next/1-account-api
Oct 21, 2026
Merged

SawyerHood merged 1 commit into
mainfrom
connect-next/1-account-api

Conversation

@SawyerHood

Copy link
Copy Markdown
Collaborator

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, audience server:<id>/machine:<id>, bound to method and path-and-query, share target).
  • packages/connect-db: migration 0010_next_tunnel_presence (nullable next_tunnel_gate, next_tunnel_region, next_tunnel_at on 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 (Worker bb-connect): POST /api/gate/resolve, /api/gate/resolve/batch (≤ 50), /api/gate/presence, /api/gate/presence/batch (≤ 500), only on connect.<BASE_DOMAIN>, behind Authorization: Bearer <GATE_SECRET> (constant-time); every route answers 404 while the GATE_SECRET secret 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. Var NEXT_BASE_DOMAIN.
  • apps/web (bb-web): GET /.well-known/bb-connect-gate-keys.json from the GATE_PUBLIC_KEYS var (404 while unset; staging holds the staging gate's key staging-2026-10-20, production deliberately unset), /oauth/authorize and /oauth/token accept https://<label>.<NEXT_BASE_DOMAIN>/mcp as 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_DIR for 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-connect Worker and applies D1 migration 0010 to bb-connect-prod (nullable columns only, no backfill); the new /api/gate/* endpoints answer 404 until the GATE_SECRET Worker secret is set. It also deploys bb-web; production GATE_PUBLIC_KEYS stays unset, so https://getbb.app/.well-known/bb-connect-gate-keys.json answers 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-contract 22/22, @bb/connect-db 47/47, @bb/connect 298/298 (incl. gate-api.test.ts: auth, shapes, batches, presence write rules), @bb/web 350/350.
  • pnpm exec oxfmt --check on the changed files, pnpm lint: clean.
  • The route tree (apps/web/src/routeTree.gen.ts) was regenerated by vite build and matches.
  • End to end (whole stack, top PR): apps/connect-next/scripts/e2e-local.sh runs 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.
  • Staging: this Worker code has been running on bb-connect-staging with the gate bb-connect-next-staging through 2,500-tunnel load tests (see PR 3).

AGENT GENERATED

…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
SawyerHood force-pushed the connect-next/1-account-api branch from 31c5eec to f57a0be Compare October 21, 2026 16:53
@SawyerHood
SawyerHood merged commit b088607 into main Oct 21, 2026
48 checks passed
@SawyerHood
SawyerHood deleted the connect-next/1-account-api branch October 21, 2026 17:21
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
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