Repository navigation
fix(daemon): wire auto-minted API token through every CLI/MCP client - #3549
Conversation
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]>
|
Warning Review limit reached
Next review available in: 57 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the 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 configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (11)
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. Comment |
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_tokenonly 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'sfind/facetsdaemon 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_tokenset, which is the whole point of auto-minting): the daemon requires a real token, every client sends none, every request gets 401.DaemonClient.probe()returnsNoneon 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'sload_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
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 viaresolve_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.polylogue/daemon/api_auth.py,polylogue/browser_capture/receiver.py): new_is_trusted_token_filehelper 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.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
find,facets,maintenance rebuild-index) and the MCP call-log outbox: fixed in this PR.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 explicitauth_tokenparameter, 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
Ref polylogue-n6pz