Spec: Agent API Authentication¶
- ID: 008-agent-api-auth
- Status: shipped
- Owner: @copilot
- Created: 2026-04-30
- Roadmap Epic: BACKLOG BL-20260429-13 — "Add authn/z to
/api/agents/*routes before the dashboard is exposed beyond localhost." - Constitution Compliance: confirmed against docs/constitution.md §§1, 2, 3, 4, 8, 11.
1. Problem & Motivation¶
Spec 003 shipped the agent ops dashboard read-only and intentionally deferred auth (NG1 in specs/003-agent-ops-dashboard/spec.md). The four /api/agents/* routes leak provenance events (/runs), the BACKLOG, and the registry to anyone who can reach the host.
Today this is fine because the only deployment is localhost. The next deployment target (any compose stack on a developer's LAN, a tailnet, or a cloud staging box) will expose the dashboard publicly. Constitution §1 ("privacy by default") and §8 ("input validation at every API boundary") require we close this gap before that happens, and BL-20260429-13 is the highest-severity Open item in BACKLOG.md.
2. Goals¶
- G1: Gate every
/api/agents/*route behind a single shared API key that the operator configures in.env. - G2: Keep local-first dev DX painless — when no key is configured the routes stay open (with a loud startup warning) so
make devkeeps working out of the box. - G3: Wire the existing
AgentDashboardUI to forward the configured key so the dashboard keeps rendering after the gate is enabled. - G4: Add a reusable
require_api_keyFastAPI dependency inapp.core.securitythat future specs can apply to other routers (analysis, graph, lsat) without rewriting the auth logic.
Closes BL-20260429-13.
3. Non-Goals¶
- NG1: No new user model, no per-user accounts, no JWT issuance — single shared key only. A multi-user identity layer is a future spec.
- NG2: No rate limiting or abuse detection. Auth only.
- NG3: Auth is not applied to
/api/analysis,/api/graph,/api/lsat, or/healthin this spec — gating those needs separate UX consideration. Tracked as a follow-up. - NG4: No client-side key rotation flow or admin UI. Operator restarts the backend with a new
.envvalue.
4. User Stories¶
- As a maintainer running the stack on a tailnet, I want to set
AGENT_API_KEY=...in.envand have all/api/agents/*routes reject requests without that key, so unauthenticated peers cannot read the BACKLOG or audit feed. - As a dashboard user, I want the
/agentspage to keep working after the gate is enabled (because I configured the matching key in the frontend env), so the protection is invisible to me. - As a local developer, I want
make devto keep working with no auth headers when I have not setAGENT_API_KEY, so the privacy gate does not get in the way of inner-loop work. - As a reviewer, I want a 401 on a missing/wrong key and a 200 on a correct key, with a clear error body, so I can verify the gate without reading source.
5. Functional Requirements¶
- FR-1: New module
app.core.securityexposes a FastAPI dependencyrequire_api_key(x_api_key: str | None = Header(None)) -> Nonethat: - Returns
None(allow) whensettings.AGENT_API_KEYisNoneor empty. - Raises
HTTPException(401, {"detail": "missing api key"})when the setting is configured but the header is absent. - Raises
HTTPException(401, {"detail": "invalid api key"})when the header is present but does not equal the setting (compared withsecrets.compare_digestto avoid timing leaks). - FR-2: New setting
AGENT_API_KEY: str | None = Noneis added toapp.core.config.Settings(read from envAGENT_API_KEY). - FR-3:
app.api.agents.routermounts the dependency once viadependencies=[Depends(require_api_key)]onapp.include_router(...)inapp.main. Every existing/api/agents,/api/agents/runs,/api/agents/backlog,/api/agents/specsrequest now flows through the gate. - FR-4: At startup (
lifespan), ifAGENT_API_KEYis unset andENVIRONMENT != "development", log oneWARNINGline:"AGENT_API_KEY not set — /api/agents/* is publicly readable". - FR-5:
frontend/lib/api.tsreadsprocess.env.NEXT_PUBLIC_AGENT_API_KEYand, when non-empty, attachesX-API-Key: <value>to every/api/agents/*request. Other endpoints are unaffected. - FR-6:
.env.exampledocuments bothAGENT_API_KEY(backend) andNEXT_PUBLIC_AGENT_API_KEY(frontend), including the "leave blank for local dev" guidance.
6. Non-Functional Requirements¶
- NFR-SEC-1: Key comparison uses
secrets.compare_digest(constant-time) — never==. - NFR-SEC-2: The configured key is never logged. The startup warning (FR-4) only states whether one is configured. Provenance and audit events do not include the header value.
- NFR-PERF-1: Auth dependency adds < 1 ms p95 to each request (single string compare; no DB / no I/O).
- NFR-AUDIT-1: 401 responses still flow through
provenance_middlewareso denied requests are visible in the audit JSONL via the existinglog_requestpath. - NFR-COVERAGE-1: Backend coverage on
app.core.security≥ 95 %; overall backend coverage stays ≥ 75 %. Frontend coverage stays ≥ 70 %.
7. Data Model Impact¶
None. No Neo4j or Postgres schema changes. No new on-disk files.
8. API Surface¶
No new routes; existing routes gain a dependency:
GET /api/agents — now requires X-API-Key when AGENT_API_KEY is set
GET /api/agents/runs?limit=N — same
GET /api/agents/backlog — same
GET /api/agents/specs — same
Error body shape on rejection (matches FastAPI default for HTTPException):
9. Agent / Crew Impact¶
None. Agents are not invoked by these endpoints; this spec is purely middleware-shaped. No changes to docs/agent-roles.md.
10. Acceptance Criteria¶
- AC-1: With
AGENT_API_KEY=""(default),GET /api/agentsreturns200and the existing payload, with noX-API-Keyheader in the request (verifies FR-1 fall-through and FR-3 wiring). - AC-2: With
AGENT_API_KEY="s3cret"and no header,GET /api/agentsreturns401with{"detail": "missing api key"}(verifies FR-1 missing-key path). - AC-3: With
AGENT_API_KEY="s3cret"andX-API-Key: wrong,GET /api/agents/runsreturns401with{"detail": "invalid api key"}(verifies FR-1 mismatch path + NFR-SEC-1). - AC-4: With
AGENT_API_KEY="s3cret"andX-API-Key: s3cret, all four/api/agents/*routes return200(verifies FR-3 across the whole router). - AC-5: A unit test on
require_api_keyconfirms it usessecrets.compare_digest(asserted viamonkeypatchof the symbol, verifying NFR-SEC-1). - AC-6: AgentDashboard vitest suite passes when
process.env.NEXT_PUBLIC_AGENT_API_KEYis set — MSW handlers assert that theX-API-Keyheader is present and equal to the configured value (verifies FR-5). - AC-7: When
AGENT_API_KEYis unset andENVIRONMENT="staging", the FastAPI app logs the FR-4 warning exactly once on startup (verifies FR-4 — observed viacaplog).
11. LegalBench Impact¶
- Tasks affected: none.
- Expected delta: 0 pp. This spec is infrastructure / security only.
12. Risks & Open Questions¶
- R-1: Operators who upgrade may have a key set in backend
.envbut forget to setNEXT_PUBLIC_AGENT_API_KEYin the frontend, breaking the dashboard. Mitigation: surface a clear panel-level error ("401 — set NEXT_PUBLIC_AGENT_API_KEY") via the existing per-panel error handling inAgentDashboard. Documented in the README/.env.example. - R-2:
NEXT_PUBLIC_*env vars are bundled into the client JS, so the key is not secret from a user who can load the page. This is an explicit trade-off for an MVP single-tenant operator key. Documented in the spec; tracked as a follow-up to introduce a Next.js server-side proxy that holds the key out of the browser bundle. - R-3: A future spec gating
/api/analysis,/api/graph, and/api/lsatwith the same key would re-couple unrelated surfaces. Mitigation:require_api_keyis a generic FastAPI dependency keyed on a single setting; future specs can introduce per-router settings (ANALYSIS_API_KEY, etc.) without rewriting the helper.
13. Out of Scope / Follow-ups¶
- Per-user authentication (OAuth / JWT).
- Server-side proxy in Next.js so the key never enters the browser bundle.
- Extending the gate to
/api/analysis,/api/graph,/api/lsat. - Rate limiting / abuse detection.
- Operator UI for rotating the key without a backend restart.