Skip to content

fix(daemon): wire auto-minted API token through every CLI/MCP client - #3549

Merged
Sinity merged 1 commit into
masterfrom
feature/security/daemon-api-cross-uid-auth
Aug 2, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/security/daemon-api-cross-uid-auth

Conversation

@Sinity

@Sinity Sinity commented Aug 2, 2026

Copy link
Copy Markdown
Owner

Summary

Follows up on PR #3488 (polylogue-rzve), which made the daemon auto-mint and enforce a bearer token by default instead of serving fully open on loopback. This PR closes the gap that PR left: no client actually read that auto-minted token, so the daemon's own default posture silently broke its own fast paths, and hardens the token file's trust boundary against the same class of mistake the underlying audit (polylogue-n6pz) flagged.

Problem

polylogue.config.Config.api_auth_token only returns an explicitly configured value (daemon.api.auth_token / POLYLOGUE_API_AUTH_TOKEN). Every daemon client in this repo read that raw config value directly: the CLI's find/facets daemon fast paths, maintenance rebuild-index, and the MCP call-log outbox delivery path. None of them fell back to the daemon's own auto-minted, persisted token (polylogue.daemon.api_auth.load_or_mint_api_auth_token).

Net effect in the common unconfigured case (no explicit daemon.api.auth_token set, which is the whole point of auto-minting): the daemon requires a real token, every client sends none, every request gets 401. DaemonClient.probe() returns None on non-200, and the CLI's fast path treats that identically to "daemon unreachable" — it silently falls back to direct SQLite reads. So the auto-mint change from #3488, while closing the cross-uid audit finding server-side, quietly defeated the daemon fast path for every normal single-user setup.

Separately, load_or_mint_api_auth_token (and its documented mirror, the browser-capture receiver's load_or_mint_receiver_token) trusted an existing token file's bytes without checking who wrote them — no uid or permission-bit check before reading. The n6pz audit's whole point was "don't assume a filesystem boundary holds without verifying it"; the token file itself hadn't received that scrutiny.

Solution

  • Client wiring (polylogue/cli/click_app.py, polylogue/cli/commands/facets.py, polylogue/cli/archive_query.py, polylogue/cli/commands/maintenance/_rebuild_index.py, polylogue/mcp/call_log.py): every daemon-HTTP/UDS client call site now resolves its token via resolve_api_auth_token(config.api_auth_token, allow_no_auth=config.api_allow_no_auth) instead of reading the raw config field. This is the same resolver the daemon itself uses to decide its enforced token, so a client with no explicit config transparently picks up the identical auto-minted/persisted value — zero-friction for the common same-uid case, as required.
  • Token-file trust hardening (polylogue/daemon/api_auth.py, polylogue/browser_capture/receiver.py): new _is_trusted_token_file helper refuses to trust an existing token file that is a symlink, or not owned by the current uid, or has any group/other permission bit set. An untrusted file is treated as absent (logged, then re-minted) rather than read. Applied to both the daemon API token loader and its mirrored browser-capture-receiver counterpart for consistency.
  • Threat-model doc (docs/daemon-threat-model.md): the "when unset, the API is open on loopback" paragraph was stale since fix(daemon): auto-mint and persist the daemon API bearer token #3488 shipped auto-minting. Rewritten to describe the current default (token required unless explicitly opted out), the cross-uid protection it restores, and the residual same-uid risk (unchanged and out of scope, same as any other localhost dev tool).

Callers audited

  • CLI daemon fast paths (find, facets, maintenance rebuild-index) and the MCP call-log outbox: fixed in this PR.
  • Browser extension: only talks to the separate browser-capture receiver on port 8765 (its own independent, already-required auth_token), never the daemon API on 8766 — no change needed.
  • archive_query._fetch_daemon_sessions_payload_with_deadline / _fetch_daemon_sessions_payload_once / _daemon_matches_archive_root: a second fast-path cluster that already accepts an explicit auth_token parameter, but is currently only exercised directly by tests (tests/unit/cli/test_query_exec_laws.py), not reachable from any production call site today. No fix needed now; noting it explicitly so whoever wires it into a real caller inherits the same resolver pattern rather than reading raw config.

Verification

devtools test tests/unit/daemon/test_api_auth.py tests/unit/browser_capture/test_receiver_token.py
# 22 passed -- includes 4 new tests for the ownership/permission/symlink token-file check

devtools test tests/unit/cli/test_daemon_golden_parity.py
# 4 passed -- new test_find_daemon_proxied_path_authenticates_with_auto_minted_token starts a
# real UDS daemon server requiring the archive's actual auto-minted token (not an empty one) and
# proves the CLI's daemon-proxied path is still reached (source == "daemon"), which fails without
# the client-side resolver fix (probe would 401 and silently fall back to direct reads)

devtools test tests/unit/cli/test_click_app.py tests/unit/mcp/test_mcp_call_log.py tests/unit/cli/test_daemon_client.py
# 107 passed

devtools test tests/unit/cli/test_archive_maintenance_cli.py
# 2 failures reproduce identically on a clean stash of this branch (git stash + rerun) -- pre-existing, unrelated

devtools verify --quick
# exit 0: ruff format/check, mypy --strict, render all --check, layering, closure-matrix, schema/doc policy checks all green

Ref polylogue-n6pz

Problem: PR #3488 (polylogue-rzve) made the daemon auto-mint and require a
bearer token by default, closing the cross-uid loopback-read gap the
polylogue-n6pz audit flagged. But no client actually read that auto-minted
token -- polylogue.config.Config.api_auth_token only returns an explicitly
configured value, so every daemon client (CLI query fast path, `facets`,
`maintenance rebuild-index`, the MCP call-log outbox) sent no Authorization
header in the common unconfigured case. The daemon then rejected every
request with 401, and the CLI's own probe-failure path silently falls back
to direct SQLite reads -- so the fast path was effectively dead by default,
and any client that hard-fails on 401 instead of falling back would have
been broken outright.

Also hardens the token file itself: `load_or_mint_api_auth_token` and its
browser-capture-receiver mirror used to trust an existing file's bytes
without checking who put them there. That repeats the exact assumption the
n6pz audit found reachable elsewhere (a filesystem boundary that looks solid
but isn't verified). Both loaders now refuse a symlink and any file not
owned by the current uid with owner-only (0600) bits, logging and
re-minting instead of trusting it.

What changed:
- polylogue/daemon/api_auth.py: `_is_trusted_token_file` gates
  `load_or_mint_api_auth_token`'s read path (uid + symlink + permission
  check).
- polylogue/browser_capture/receiver.py: same check mirrored onto
  `load_or_mint_receiver_token` for consistency (identical mirrored function
  per its own docstring).
- polylogue/cli/click_app.py, cli/commands/facets.py, cli/archive_query.py,
  cli/commands/maintenance/_rebuild_index.py, mcp/call_log.py: every daemon
  HTTP/UDS client now resolves the token via
  `daemon.api_auth.resolve_api_auth_token(config.api_auth_token,
  allow_no_auth=config.api_allow_no_auth)` instead of reading the raw config
  value, so an unconfigured client picks up the same persisted auto-minted
  token the daemon itself is enforcing.
- docs/daemon-threat-model.md: the "open on loopback when unset" residual
  risk paragraph was stale since #3488 shipped; rewritten to describe the
  auto-mint default, the cross-uid protection it now provides, and the
  unchanged same-uid residual risk.

Callers audited: CLI (`find`/`facets`/`maintenance rebuild-index` daemon
fast paths) and MCP (`call_log` outbox delivery) are the daemon-HTTP-API
clients that exist in this repo and are now wired. The browser extension
only talks to the separate browser-capture receiver (port 8765, its own
independent auth_token, already required), never the daemon API on 8766, so
no change was needed there. `archive_query._fetch_daemon_sessions_payload_with_deadline`
/ `_daemon_matches_archive_root` is a second, currently-unreached-from-production
fast-path cluster (only exercised directly by tests); it already accepts an
explicit `auth_token` parameter, so no fix was needed, but note it for
whoever wires it up next.

Verification:
- devtools test tests/unit/daemon/test_api_auth.py tests/unit/browser_capture/test_receiver_token.py -> 22 passed
- devtools test tests/unit/cli/test_daemon_golden_parity.py -> 4 passed (new regression test proves the daemon-proxied path is reached with only the auto-minted token, no explicit config)
- devtools test tests/unit/cli/test_click_app.py tests/unit/mcp/test_mcp_call_log.py tests/unit/cli/test_daemon_client.py -> 107 passed
- devtools test tests/unit/cli/test_archive_maintenance_cli.py -> 2 pre-existing failures confirmed unrelated (same failures reproduce on a clean stash of this branch)
- devtools verify --quick -> exit 0 (ruff format/check, mypy --strict, render all --check, layering, closure-matrix, schema/doc policy checks all green)

Ref polylogue-n6pz

Co-Authored-By: Claude <[email protected]>
@coderabbitai

coderabbitai Bot commented Aug 2, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Sinity, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 57 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: febaf3f2-412e-455b-90fe-7d9e7f1a43cd

📥 Commits

Reviewing files that changed from the base of the PR and between 622edcb and c3de462.

📒 Files selected for processing (11)
  • docs/daemon-threat-model.md
  • polylogue/browser_capture/receiver.py
  • polylogue/cli/archive_query.py
  • polylogue/cli/click_app.py
  • polylogue/cli/commands/facets.py
  • polylogue/cli/commands/maintenance/_rebuild_index.py
  • polylogue/daemon/api_auth.py
  • polylogue/mcp/call_log.py
  • tests/unit/browser_capture/test_receiver_token.py
  • tests/unit/cli/test_daemon_golden_parity.py
  • tests/unit/daemon/test_api_auth.py

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Sinity
Sinity merged commit bf1e809 into master Aug 2, 2026
3 checks passed
@Sinity
Sinity deleted the feature/security/daemon-api-cross-uid-auth branch August 2, 2026 12:06
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