Repository navigation
fix(daemon): auto-mint and persist the daemon API bearer token - #3488
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
Warning Review limit reached
Next review available in: 32 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 (10)
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 |
Problem: docs/daemon.md documented --api-auth-token as "auto-generated if not provided", but no minting code existed anywhere -- run_daemon_services simply stored auth_token=None when the flag was omitted, so the daemon HTTP API started fully open on loopback by default (polylogue-rzve, discovered while implementing polylogue-gnie). The browser-capture receiver already solved this exact problem for its own pairing token. What changed: - polylogue/daemon/api_auth.py: new module mirroring polylogue.browser_capture.receiver's mint/load/rotate/resolve contract for the daemon API -- load_or_mint_api_auth_token (atomic mkstemp + fchmod(0o600) + os.replace), resolve_api_auth_token (explicit token wins, else auto-mint, else the explicit --api-allow-no-auth opt-out), and a `polylogued api token show [--rotate]` CLI surface. - polylogue/paths/_roots.py: api_auth_token_path(), scoped under archive_root() like the receiver token so distinct POLYLOGUE_ARCHIVE_ROOT archives never share one API token. - polylogue/daemon/cli.py: run_daemon_services now resolves the API token through resolve_api_auth_token before the remote-bind gate and before constructing DaemonAPIHTTPServer/DaemonAPIUnixHTTPServer; added --api-allow-no-auth (mirrors --browser-capture-allow-no-auth) wired through config layering (daemon.api.allow_no_auth / POLYLOGUE_API_ALLOW_NO_AUTH). - polylogue/config.py: api_allow_no_auth property + inventory entry + bool-key/default registration, same pattern as browser_capture_allow_no_auth. - polylogue/daemon/http.py: corrected the _check_auth docstring, which described the pre-fix "open by default" behavior as intentional. - docs/daemon.md: documents --api-allow-no-auth and clarifies the token auto-mint contract. This is a deliberate behavior change: the daemon API is no longer ambiguously open by default on loopback -- a real, persisted token is always in effect unless the operator explicitly opts out. Remote binding (--insecure-allow-remote) still requires a token in effect and cannot be combined with --api-allow-no-auth. Verification: - devtools test tests/unit/daemon/test_api_auth.py tests/unit/daemon/test_daemon_cli_remote_bind.py -> 23 passed - devtools test tests/unit/cli/test_config_command.py tests/unit/core/test_config_inventory.py tests/unit/cli/test_query_exec_laws.py tests/unit/core/test_config.py tests/unit/devtools/test_render_openapi.py tests/unit/devtools/test_deployment_smoke.py -> 306 passed - devtools verify --quick -> exit 0 (ruff format/check, mypy --strict, render all --check, topology/layering/closure-matrix, docs-coverage, hash-boundary-census, schema-versioning policy, schema promotion audit) Anti-vacuity: test_resolve_api_auth_token_default_mints_and_persists exercises the production resolve_api_auth_token -> load_or_mint_api_auth_token path directly against a real temp file; removing the auto-mint call (i.e. reverting to `return None`) makes the assertion `resolved is not None` and the persisted-file assertions fail. test_non_loopback_bind_with_allow_remote_and_no_explicit_token_auto_mints exercises the same path through run_daemon_services's actual gate logic and asserts the real token file exists on disk afterward. Ref polylogue-rzve
91f3a09 to
573bf2a
Compare
…, 37t.1, uegw, wofr (PRs #3488-#3496) Co-Authored-By: Claude <[email protected]>
…3549) ## 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 #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 Co-authored-by: Claude <[email protected]>
Summary
docs/daemon.mddocumented--api-auth-tokenas "auto-generated if not provided," but no minting code existed anywhere:run_daemon_servicessimply storedauth_token=Nonewhen the flag was omitted, so the daemon HTTP API started fully open on loopback by default. This implements the documented behavior by mirroring the browser-capture receiver's already-shipped token-lifecycle contract.Problem
Discovered while implementing polylogue-gnie (2026-07-08), triage-verified 2026-07-31:
docs/daemon.md:83claims auto-generation;polylogue/daemon/cli.py(pre-fix) only checkedif not api_auth_tokenfor the remote-bind gate, with no minting path anywhere in the codebase.DaemonAPIHTTPServer.__init__just stored whateverauth_tokenit was given —Noneby default. This is exactly the "ambiguous unauthenticated state" polylogue-rzve's design note calls out for a remotely-reachable, write-capable local API.Solution
polylogue/daemon/api_auth.py(new):load_or_mint_api_auth_token(atomic mkstemp +fchmod(0o600)+os.replace, matchingpolylogue.browser_capture.receiver.load_or_mint_receiver_tokenbyte-for-byte in idiom),resolve_api_auth_token(explicit token wins → auto-mint/load →allow_no_authopt-out returnsNonewith a loud warning log), and apolylogued api token show [--rotate]CLI surface.polylogue/paths/_roots.py:api_auth_token_path(), scoped underarchive_root()(notstate_home()) so twoPOLYLOGUE_ARCHIVE_ROOTarchives never share one token — same reasoning asbrowser_capture_receiver_token_path().polylogue/daemon/cli.py:run_daemon_servicesnow resolves the token viaresolve_api_auth_tokenbefore the remote-bind gate and before constructingDaemonAPIHTTPServer/DaemonAPIUnixHTTPServer. Added--api-allow-no-auth(mirrors--browser-capture-allow-no-auth), wired through config layering (daemon.api.allow_no_auth/POLYLOGUE_API_ALLOW_NO_AUTH). Remote binding (--insecure-allow-remote) still requires a real token in effect and cannot be paired with--api-allow-no-auth.polylogue/config.py:api_allow_no_authproperty +ConfigInventoryEntry+ bool-key/default registration, identical pattern tobrowser_capture_allow_no_auth.polylogue/daemon/http.py: corrected the_check_authdocstring, which described the pre-fix "open by default" behavior as an intentional "local dev default."docs/daemon.md: documents--api-allow-no-authand clarifies the auto-mint contract for--api-auth-token.Deliberate behavior change: the daemon API is no longer ambiguously open by default on loopback — a real, persisted 0600 token is always in effect unless the operator explicitly opts out with
--api-allow-no-auth.Alternatives rejected: fixing only the docs (removing the "auto-generated" claim) was the AC's other allowed option, but the bead's design section and priority-correction note ("a sensitive local daemon cannot have documentation claim credential minting while runtime may start with no token") plus the receiver precedent point at implementing the documented behavior as the default expectation.
Verification
devtools test tests/unit/daemon/test_api_auth.py tests/unit/daemon/test_daemon_cli_remote_bind.py→23 passeddevtools test tests/unit/cli/test_config_command.py tests/unit/core/test_config_inventory.py tests/unit/cli/test_query_exec_laws.py tests/unit/core/test_config.py tests/unit/devtools/test_render_openapi.py tests/unit/devtools/test_deployment_smoke.py→306 passeddevtools verify --quick→exit_code: 0(ruff format/check,mypy --strict,render all --check, topology/layering/closure-matrix, docs-coverage, hash-boundary-census, schema-versioning policy, schema promotion audit)devtools render topology-projection(new modulepolylogue/daemon/api_auth.pyregeneratesdocs/plans/topology-target.yaml)Anti-vacuity:
test_resolve_api_auth_token_default_mints_and_persistscalls the productionresolve_api_auth_token→load_or_mint_api_auth_tokenpath directly against a real temp file; reverting the auto-mint call toreturn Nonemakes theresolved is not Noneand persisted-file assertions fail.test_non_loopback_bind_with_allow_remote_and_no_explicit_token_auto_mintsexercises the same path throughrun_daemon_services's real gate logic and asserts the token file exists on disk afterward — it fails if the mint call is removed or the gate regresses to always requiring an explicit token.Ref polylogue-rzve