Skip to content

Send the machine's server headers with bb CLI requests - #8458

Merged
ymichael merged 1 commit into
mainfrom
bb/bb-cli-never-sends-the-machine-credential-401-on-thr_xv9nbyag6k
Dec 1, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/bb-cli-never-sends-the-machine-credential-401-on-thr_xv9nbyag6k

Conversation

@ymichael

@ymichael ymichael commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

On a machine enrolled through bb connect, running plain bb in an ordinary shell (outside a bb terminal or agent shell) returned HTTP 401. The bb launcher reads the machine's config.json, sets BB_SERVER_URL to the stored server, and passes the machine credential to the CLI in BB_SERVER_HEADERS (packages/bb-app/src/launcher.ts, applyManagedConfigEnv). Nothing in apps/cli read BB_SERVER_HEADERS, so requests reached the bb connect gate without x-bb-connect-machine. docs/configuration.md already said the bundled CLI sends these headers. bb terminals and agent shells were not affected because their requests go through the daemon's loopback helper, which adds the credential itself. Found while reviewing #8266, which fixed only that managed path.

What changed

  • packages/config/src/cli.ts: new loadCliServerHeaders() parses BB_SERVER_HEADERS with the existing BB_SERVER_HEADERS_ENV definition. The default is {}, and a malformed value throws instead of silently dropping the credential.
  • apps/cli/src/client.ts: cliFetch is the single function every CLI server request goes through, both SDK calls and the raw status and plugin-proxy fetches. It now starts from those headers, and request headers set by the caller take precedence.

No wire changes, so no HOST_DAEMON_PROTOCOL_VERSION bump. No CLI, guide or doc changes: the existing docs/configuration.md text is now accurate.

How you verified

  • Live repro: I built bb-app with turbo run build --filter=bb-app and put a local gate in front of a real server. The gate returns 401 unless x-bb-connect-machine matches, the way bb connect does. I ran node packages/bb-app/dist/bb.js thread list under env -i with BB_DATA_DIR pointing at a config.json holding serverUrl (the gate) and machineCredential.
    • Before the fix: HTTP 401: sign in required, and the gate logged 401 GET /api/v1/threads….
    • After the fix: exit 0 with threads listed. bb status (/system/config, /plugins, /plugins/contributions) and a plugin command (POST /plugins/bb--git/cli) all passed the gate.
  • Tests: in apps/cli/src/__tests__/client.test.ts, SDK requests and direct cliFetch requests must carry BB_SERVER_HEADERS, and a malformed value must throw. All three fail without the fix and pass with it.
  • pnpm exec turbo run test typecheck --filter=@bb/cli --filter=@bb/config passed (CLI 1460 tests, config 101).

AGENT GENERATED

🤖 Generated with Claude Code

On a machine enrolled through bb connect, the bb launcher puts the
machine credential in BB_SERVER_HEADERS and points BB_SERVER_URL at the
stored server, but the CLI never read BB_SERVER_HEADERS. Plain bb in an
ordinary shell therefore reached the connect gate without the credential
and got HTTP 401. cliFetch, which every CLI server request goes through,
now attaches those headers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@blacksmith-sh

This comment has been minimized.

@ymichael
ymichael merged commit 2745f23 into main Dec 1, 2026
94 of 95 checks passed
@ymichael
ymichael deleted the bb/bb-cli-never-sends-the-machine-credential-401-on-thr_xv9nbyag6k branch December 1, 2026 05:15
scowalt pushed a commit to scowalt/bb that referenced this pull request Dec 1, 2026
## Human comments

## What was wrong

On a machine enrolled through bb connect, running plain `bb` in an
ordinary shell (outside a bb terminal or agent shell) returned HTTP 401.
The `bb` launcher reads the machine's `config.json`, sets
`BB_SERVER_URL` to the stored server, and passes the machine credential
to the CLI in `BB_SERVER_HEADERS` (`packages/bb-app/src/launcher.ts`,
`applyManagedConfigEnv`). Nothing in `apps/cli` read
`BB_SERVER_HEADERS`, so requests reached the bb connect gate without
`x-bb-connect-machine`. `docs/configuration.md` already said the bundled
CLI sends these headers. bb terminals and agent shells were not affected
because their requests go through the daemon's loopback helper, which
adds the credential itself. Found while reviewing get-bb#8266, which fixed
only that managed path.

## What changed

- `packages/config/src/cli.ts`: new `loadCliServerHeaders()` parses
`BB_SERVER_HEADERS` with the existing `BB_SERVER_HEADERS_ENV`
definition. The default is `{}`, and a malformed value throws instead of
silently dropping the credential.
- `apps/cli/src/client.ts`: `cliFetch` is the single function every CLI
server request goes through, both SDK calls and the raw `status` and
plugin-proxy fetches. It now starts from those headers, and request
headers set by the caller take precedence.

No wire changes, so no `HOST_DAEMON_PROTOCOL_VERSION` bump. No CLI,
guide or doc changes: the existing `docs/configuration.md` text is now
accurate.

## How you verified

- **Live repro:** I built `bb-app` with `turbo run build
--filter=bb-app` and put a local gate in front of a real server. The
gate returns 401 unless `x-bb-connect-machine` matches, the way bb
connect does. I ran `node packages/bb-app/dist/bb.js thread list` under
`env -i` with `BB_DATA_DIR` pointing at a `config.json` holding
`serverUrl` (the gate) and `machineCredential`.
- Before the fix: `HTTP 401: sign in required`, and the gate logged `401
GET /api/v1/threads…`.
- After the fix: exit 0 with threads listed. `bb status`
(`/system/config`, `/plugins`, `/plugins/contributions`) and a plugin
command (`POST /plugins/bb--git/cli`) all passed the gate.
- **Tests:** in `apps/cli/src/__tests__/client.test.ts`, SDK requests
and direct `cliFetch` requests must carry `BB_SERVER_HEADERS`, and a
malformed value must throw. All three fail without the fix and pass with
it.
- `pnpm exec turbo run test typecheck --filter=@bb/cli
--filter=@bb/config` passed (CLI 1460 tests, config 101).

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
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