Skip to content

Loopback services with no browser-origin guard: /api/v1, second bb server, mock CLI traffic proxy #1531

Closed

Description

@SawyerHood

Important

This blocks #1522. That PR must not merge until the /api/v1/* guard below lands, because it makes a second bb server's REST API reachable from a browsed page and nothing rejects it.

Problem

#1522 relaxed the in-app browser firewall so a loopback page can reach other loopback ports, and reserved the ports bb itself serves. A reserved list can only name ports the desktop process can observe. Two loopback services fall outside it and have no control that stops a browsed page from reaching them.

Both are reachable only through a second bb instance — a dev instance running alongside the packaged app, which is the everyday bb-developer setup. The attached instance's ports are reserved and unaffected.

1. A second bb server's REST API

apps/server/src/server.ts applies browserRequestProblem to /ws (line 447), /ws/terminals/:id (line 468), and to routes in plugins.ts and files.ts. /api/v1/* has only cors(), which decides whether to echo Access-Control-Allow-Origin; it never rejects. A no-cors POST with content-type: text/plain skips the preflight, and the handler runs while the page never reads the reply.

Suggested fix: apply browserRequestProblem as middleware on /api/v1/*. Worth checking against the connect-tunnel and public-share paths first: isTrustedOrigin already accepts an origin matching the request Host, so tunnelled and same-origin share pages should pass, but that needs real verification rather than reasoning.

2. The Claude Code mock CLI traffic proxy

packages/agent-runtime/src/claude-code/bridge/mock-cli-traffic-proxy.ts binds a random loopback port and forwards to a fixed endpoint. It is gated behind the off-by-default claudeCodeMockCliTraffic experiment and, unlike the machine-auth proxy, injects no credential — rewriteHeaders forwards only what the caller sent. So reaching it grants no ambient authority, which is why it is lower severity.

The residual concern is a pivot: isClaudeCodeMockCliTrafficEndpoint permits an http:// loopback endpoint. If a user points it at a reserved port, the proxy becomes a forwarder to a port the firewall otherwise withholds. The page cannot choose the host — buildUpstreamUrl takes only the path from the request — so this needs that specific configuration.

Suggested fix: reject browser-originated requests as the machine-auth proxy now does. Doing it properly means lifting the isBrowserRequest / isProxyLoopbackAuthority helpers out of apps/host-daemon into a shared package, since packages/agent-runtime cannot import from an app.

Why these were not fixed in #1522

Both fixes are wider than a bug-fix PR should carry. The server one touches every /api/v1 route and has paths (connect tunnel, public shares) that cannot be verified from that PR's test surface. The proxy one requires moving helpers into a new shared package. #1522 fixed the services that carry ambient authority and are reachable through the attached instance; these two need their own change with their own verification.

Related

Coverage table (copied from #1522, so it survives edits there)

Every loopback listener a bb machine can run, found by searching for listeners (.listen(, serve(, createServer() across apps/, packages/, and plugins/.

Service Where it binds What stops a browsed page reaching it
bb server (attached instance) apps/server/src/start-server.ts Reserved port (builtinServerUrl, attached runtime URL). Plus an Origin check on /ws and /ws/terminals/:id.
Host daemon local API (attached instance) apps/host-daemon/src/local-api.ts Reserved port (packaged constant, BB_HOST_DAEMON_PORT, fetched value), and an Origin allowlist enforced as a request guard (this PR).
Machine-auth proxy apps/host-daemon/src/machine-auth-proxy.ts Cannot be reserved — random port. Origin + Sec-Fetch-Site check and a Host authority check (this PR). Injects a machine credential, so this is the highest-authority service on the list.
ACP dynamic tool bridge packages/agent-runtime/src/acp/bridge/bridge.ts Cannot be reserved — random port. Protected by an unguessable 256-bit token required per request (bridge.ts:282), on a newline-JSON protocol an HTTP request cannot satisfy. This is the capability-token control SlopCop recommended, already in place here.
bb app Vite dev server dev only, apps/app/vite.dev.config.ts Reserved port via appWindowUrl (this PR), when it is the window the desktop loaded.
Second bb server (dev instance alongside the packaged app) same as above Nothing. /api/v1/* has cors() but no request-rejecting origin guard. Filed as #1531.
Mock CLI traffic proxy packages/agent-runtime/src/claude-code/bridge/mock-cli-traffic-proxy.ts Nothing, but it injects no credential (rewriteHeaders forwards only what the caller sent), so it carries no ambient authority. Off by default behind the claudeCodeMockCliTraffic experiment. Filed as #1531.
Second host daemon local API same as above The Origin guard added in this PR, which is per-service and so covers every instance.
Second machine-auth proxy / ACP bridge same as above Their guards are per-service and cover every instance.

What could not be enumerated. Any second bb instance: it is started outside the desktop process and reports its ports to nobody, so no reserved list can name it. That is precisely why the services themselves are guarded rather than their ports listed — a guard covers every instance, a port list covers one. The two rows above with no control are the gap that remains, both filed as #1531 with the reason each was too wide for this PR.

Not included: apps/connect/scripts/spike-origin.mts, apps/desktop/scripts/smoke-packaged-app.mjs, packages/bb-app/scripts/smoke-tarball.mjs, scripts/bb-cloud-dev.mjs, and packages/scripts/src/commands/run-dev.ts (which binds only to probe a port, then closes). None run as part of a user's bb.

Not included: apps/connect/scripts/spike-origin.mts, apps/desktop/scripts/smoke-packaged-app.mjs, packages/bb-app/scripts/smoke-tarball.mjs, scripts/bb-cloud-dev.mjs, and packages/scripts/src/commands/run-dev.ts (which binds only to probe a port, then closes). None run as part of a user's bb.

AGENT GENERATED: by Claude Opus 5

Activity

  1. changed the title [-]Loopback services with no browser-origin guard: second bb server API and mock CLI traffic proxy[/-] [+]Loopback services with no browser-origin guard (blocks #1522): second bb server API and mock CLI traffic proxy[/+] on Aug 13, 2026
  2. SawyerHood commented on Aug 13, 2026

    @SawyerHood
    CollaboratorAuthor

    Adding one item to the shared-module scope, from SlopCop's fourth pass on #1522.

    The browser-boundary checks now exist in three places with near-identical logic:

    • apps/host-daemon/src/machine-auth-proxy.ts — isBrowserRequest, isProxyLoopbackAuthority
    • apps/host-daemon/src/local-api.ts — the enforced isAllowedAppOrigin guard
    • apps/server/src/browser-request-guard.ts — browserRequestProblem

    A shared browser-boundary module should absorb all three. That is not only cleanup: it is a prerequisite for item 2 above, because packages/agent-runtime cannot import from apps/host-daemon, so the mock CLI traffic proxy cannot reuse the existing helpers until they move.

    Doing item 1 (/api/v1/*) and item 2 on one shared implementation avoids adding a fourth copy.

    AGENT GENERATED: by Claude Opus 5

  3. changed the title [-]Loopback services with no browser-origin guard (blocks #1522): second bb server API and mock CLI traffic proxy[/-] [+]Loopback services with no browser-origin guard: /api/v1, second bb server, mock CLI traffic proxy[/+] on Aug 13, 2026
  4. SawyerHood commented on Aug 13, 2026

    @SawyerHood
    CollaboratorAuthor

    Status update: this is no longer a blocker on a PR — it is what #1536 implements.

    #1522 was closed. The approach inverted: rather than keeping the in-app browser firewall and guarding around it, the firewall is deleted (#1537) and each service gets its own origin guard (#1536). That makes this issue the foundation of the stack rather than a follow-up to it.

    Item 1 — /api/v1/* — implemented in #1536. browserRequestProblem now guards the whole subtree. Two things I learned building it that are worth recording here:

    • /api/v1/plugins/<id>/http/* had to be excluded. A plugin declares its own auth mode per route (local | token | none), and none is deliberately reachable from any origin. A blanket guard pre-empts it. Three plugin-wire tests caught this.
    • requireJsonForMutation was not enabled. It answers 415 to any mutation without application/json and would break every curl -d caller. The origin check alone stops browser CSRF, since a browser always sends Origin on a cross-origin mutation. There is now a test asserting the form-encoded POST is not 415, so this stays deliberate.

    The connect-tunnel risk resolved safely: headersForLoopbackRequest already rewrites a remote Origin to the loopback origin, so https://bee.getbb.app keeps working. #1536 has a canary test asserting an unrewritten public origin gets 403, so a regression in that rewrite names itself.

    Item 2 — the mock CLI traffic proxy — still open. Not addressed. It still needs the shared browser-boundary module, because packages/agent-runtime cannot import from apps/host-daemon. With #1536 there are now four near-identical implementations to absorb: the server guard, the daemon local API, the machine-auth proxy, and whatever the mock proxy gets.

    Leaving this issue open for item 2 after #1536 lands.

    AGENT GENERATED: by Claude Opus 5

  5. added a commit that references this issue on Aug 13, 2026
    c34eabb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions