Skip to content

Route agent bb calls through the machine proxy after a server move - #4277

Closed
SawyerHood wants to merge 1 commit into
mainfrom
bb/4243-fix-migrated-host-cli-authentication-and-ve-thr_m76b3bj9mn
Closed

SawyerHood wants to merge 1 commit into
mainfrom
bb/4243-fix-migrated-host-cli-authentication-and-ve-thr_m76b3bj9mn

Conversation

@SawyerHood

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

A host daemon on a machine enrolled with a bb connect server gives each thread BB_SERVER_URL pointing at its loopback proxy, which adds the machine's serverHeaders. Such machines include the old computer after a server move. When an agent ran the packaged bb launcher, resolveServerUrl put config.json serverUrl ahead of that env var, and the launcher rewrote BB_SERVER_URL to the public gateway address before re-running $BB_CLI. The bundled CLI never read BB_SERVER_HEADERS, so these direct calls carried no credential and got 401. The same gap also broke bb in the user's own terminal on those machines.

The config-first order is still needed for bb-host-daemon/bb-app. A daemon relaunched from a thread shell must not adopt its predecessor's proxy address, so this PR keeps that order for daemon and server launches and changes only the CLI path.

What changed

  • packages/bb-app/src/launcher.ts: runBbCli resolves in a new cli mode. In that mode a non-blank BB_SERVER_URL from the caller wins over the saved serverUrl. The launcher passes config serverHeaders/machineCredential to the CLI as BB_SERVER_HEADERS only when the target is the saved serverUrl, so the credential never goes to an address it wasn't issued for. It also stops being copied into thread CLI processes. Daemon and server launches (managed/local) are unchanged.
  • apps/cli/src/client.ts, packages/config/src/cli.ts: cliFetch, which carries every CLI server request including SDK calls, attaches BB_SERVER_HEADERS only when the request origin matches the CLI's BB_SERVER_URL.
  • Docs: docs/configuration.md (BB_SERVER_URL row, machine header paragraphs), packages/bb-app/README.md (CLI and SDK resolution), and the bb guide machines template.
  • No server/daemon wire change and no protocol bump.

This goes past the issue's suggested fix, which was to prefer env everywhere. Env-first applies only to the CLI, and the header binding plus CLI header support are new.

How you verified

  • New tests fail on origin/main and pass with the change:

    • packages/bb-app/test/index.test.ts: in CLI mode a caller URL wins and gets no BB_SERVER_HEADERS. This test also runs the real runBundledCliCommand child. Unset, blank, and same-URL overrides keep the saved URL and its headers. Without config, the CLI uses the default local server. The existing config-over-env test still passes; it is renamed to say it covers non-CLI launches.
    • apps/cli/src/__tests__/client.test.ts: real local HTTP servers. Headers go only to the configured origin, both as a URL string and as a Request, and SDK calls carry them. Nothing is sent when BB_SERVER_HEADERS is unset.
  • pnpm exec turbo run typecheck lint --filter=bb-app --filter=@bb/cli --filter=@bb/config --filter=@bb/sdk --filter=@bb/templates, pnpm exec turbo run test --filter=bb-app --filter=@bb/cli --filter=@bb/config, and pnpm exec turbo run smoke:tarball --filter=bb-app with pi off PATH: all pass.

  • Real Connect migration e2e using only the repo's local pnpm cloud:dev gateway, fresh disposable data dirs and test credentials. Setup: installed packaged tarballs → paired server A with the gateway → enrolled B → ran bb server move --to B in connect mode. Afterwards A's config.json held the gateway serverUrl plus serverHeaders. A terminal opened on A through the moved server had BB_SERVER_URL=http://127.0.0.1:<proxy> and BB_CLI set, and no BB_SERVER_HEADERS. Packaged bb.js results, cross-checked against the gateway request log:

    case (project list --json, machine list --json) main this PR
    agent thread env (proxy BB_SERVER_URL + BB_CLI) 401, --help shows gateway URL lists; --help shows proxy URL; CLI child has no BB_SERVER_HEADERS
    own terminal (no BB_SERVER_URL) 401 lists, sent straight to the gateway with the saved headers
    direct "$BB_CLI" in the thread env (control) lists lists
  • An isolated harness with a real packaged server, a header-checking gate and the daemon's real startMachineAuthProxy reproduced the same table. It also covered a blank BB_SERVER_URL, which falls back to the saved URL and authenticates. An explicit BB_SERVER_URL pointed at an unrelated server received no credential header, and the credential never appeared in any log or output.

Fixes #4243

🤖 Generated with Claude Code

AGENT GENERATED

On a machine whose config.json names a bb connect server, the packaged
launcher replaced the BB_SERVER_URL the host daemon gives threads (its
authenticated loopback proxy) with the saved serverUrl, so agent bb calls
reached the public gateway without credentials and failed with 401. The
bundled CLI also ignored BB_SERVER_HEADERS, so the same calls from the
user's own terminal failed too.

The launcher's CLI path now uses a caller-supplied BB_SERVER_URL and passes
config serverHeaders only when the target is the saved serverUrl. Daemon
and server launches keep config first. The CLI attaches BB_SERVER_HEADERS
only to requests for its server origin.

Fixes #4243

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@SawyerHood

Copy link
Copy Markdown
Collaborator Author

This is an automated message. We have closed this pull request because it has been open for more than two weeks.

@SawyerHood SawyerHood closed this Oct 9, 2026
@timurkhakhalev

Copy link
Copy Markdown
Contributor

Technical review of head d0654af1b3be18e438beb6eb9a3b6acfc04eff7e: the initial-origin check does not prevent the machine credential from being forwarded after a cross-origin redirect.

In apps/cli/src/client.ts, lines 15-33, withServerHeaders checks the initial request URL, then cliFetch delegates to fetch with its default redirect behavior. The custom x-bb-connect-machine header survives a cross-origin HTTP 302.

I reproduced this with Node 24.12.0, two disposable loopback HTTP servers on different ports, and a synthetic credential. I executed the functions extracted from this PR with TypeScript types stripped and the config loaders supplied by the isolated harness; this was not a full packaged Connect e2e.

Reproduction:

  1. Set BB_SERVER_URL to local server A and BB_SERVER_HEADERS to {"x-bb-connect-machine":"FAKE_TEST_CREDENTIAL"}.
  2. Have A return 302 Location: <local server B URL>.
  3. Call cliFetch against A.
  4. B receives x-bb-connect-machine: FAKE_TEST_CREDENTIAL, although B has a different origin.

Control: calling cliFetch directly against B sends no credential. The existing different-origin test therefore passes while the redirect case leaks it.

Please prevent credential-bearing requests from following redirects across origins, or handle redirects with the origin binding enforced at every hop. Add a regression test using two local servers. This is a conditional security defect in the proposed change, not evidence of a real credential compromise and not an explanation for the automated closure of this PR.

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.

Agents on a host whose server moved to bb Connect get HTTP 401 from bb: launcher prefers config.json serverUrl over BB_SERVER_URL

2 participants