Repository navigation
Description
Task
T-080c· Milestone M7 — API · Size S · Priority P0 · Epic #7Implements NFR-SEC-001
Branch
feat/t-080c-api-security-optional-bearer-token— one task = one branch = one PR (doc 32 §6)
Summary
Implement api/auth.py: if api.token set, every request (except /health/live) requires Authorization: Bearer <token> compared with hmac.compare_digest; SSE accepts ?token= (EventSource can't set headers); startup refuses non-loopback binds without a token.
Context / motivation
Threat model TM-01/TM-06: protect the local API from other processes/LAN hosts and CSRF.
Scope
In scope
- JSON-only content types for mutations (reject form posts, TM-06).
- Token never logged (redaction test).
Implementation guidance
roboprobe/api/auth.py:def require_token(request: Request, settings = Depends(get_settings)) -> Nonedependency attached at router include (app.include_router(r, dependencies=[Depends(require_token)])) for all versioned routers./health/livestays open (container healthchecks);/health/readyand/metricsare also protected when a token is set (Prometheus supports bearer auth viaauthorizationin scrape config).- Token sources:
Authorization: Bearer <t>header; forGET /api/v1/eventsonly, also?token=(EventSource limitation, doc 12 §1). Compare withhmac.compare_digest(a.encode(), b.encode()). - Startup guard in
create_app: ifapi.hostis not loopback (ipaddress.ip_address(host).is_loopback, treat0.0.0.0/::as non-loopback) andapi.tokenis unset ⇒ raiseConfigurationError(fail fast, NFR-SEC-001). - Mutating endpoints accept only
application/jsonorapplication/yaml(spaces) — rejectapplication/x-www-form-urlencodedandmultipart/form-datawith 415 (TM-06 CSRF). - Redaction: ensure the structlog redaction processor (T-004a) masks
authorizationheaders andtokenquery params in access logs. - Standards: respect the layering contracts (doc 16 §2) and conventions in doc 34; no hard-coded tunables — thresholds/defaults come from typed settings (doc 27); no magic numbers (ruff
PLR2004); public APIs typed (mypy strict) and documented.
Configuration keys
From the configuration reference (doc 27). Keys marked (new) must be added to the typed settings models and appear in the generated reference (T-003c).
api.tokenapi.host
Acceptance criteria
- Auth on/off tests; wrong token → 401 problem+json
UNAUTHORIZED. - Bind-address test (NFR-SEC-001).
- Query-param token rejected on non-SSE routes.
- Form-encoded POST → 415.
- Access log contains no token value (caplog test).
- Tests listed below added and green in CI; coverage of new code meets the gate in doc 18 §3.
- Docs, config reference and CHANGELOG
Unreleasedupdated where behaviour, keys or interfaces changed. - Definition of Done (doc 32 §3) satisfied.
Tests required
tests/api/test_auth.py.
Dependencies
Blocked by: #142 (T-080a)
Blocks: #156 (T-083b), #161 (T-109)
Parallelization notes
Files / paths owned by this task — avoid editing other paths; shared files (pyproject.toml entry points, cli/main.py, api/routers/__init__.py, router.tsx, docker/compose.yaml, ci.yml) are append-only:
backend/src/roboprobe/api/auth.py
Independent.
Definition of Done
Per doc 32 §3 and AGENTS.md §5: lint, format, mypy strict and import-linter green; tests added and passing; docs updated; PR template completed with Closes #<this issue>; Conventional Commit messages referencing T-080c (doc 16 §6); commits authored only by the repository owner identity (AGENTS.md §4); CI green; reviewer checklist (doc 32 §5.1) satisfied.
References
Design docs: doc 12 §1 · doc 21 §1 · doc 31 §3
Requirements: NFR-SEC-001 (traceability: doc 28)
External:
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsBacklog
Activity
/meta,/metrics, request logging #142/api/v1/eventswith Last-Event-ID resume, pings and ≤ 2 s latency test #156