Skip to content

[T-080c] API security: optional bearer token (constant-time), auto-required off-loopback, SSE ?token= #144

Closed

Description

@CodewithJha

Task T-080c · Milestone M7 — API · Size S · Priority P0 · Epic #7

Implements 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)) -> None dependency attached at router include (app.include_router(r, dependencies=[Depends(require_token)])) for all versioned routers. /health/live stays open (container healthchecks); /health/ready and /metrics are also protected when a token is set (Prometheus supports bearer auth via authorization in scrape config).
  • Token sources: Authorization: Bearer <t> header; for GET /api/v1/events only, also ?token= (EventSource limitation, doc 12 §1). Compare with hmac.compare_digest(a.encode(), b.encode()).
  • Startup guard in create_app: if api.host is not loopback (ipaddress.ip_address(host).is_loopback, treat 0.0.0.0/:: as non-loopback) and api.token is unset ⇒ raise ConfigurationError (fail fast, NFR-SEC-001).
  • Mutating endpoints accept only application/json or application/yaml (spaces) — reject application/x-www-form-urlencoded and multipart/form-data with 415 (TM-06 CSRF).
  • Redaction: ensure the structlog redaction processor (T-004a) masks authorization headers and token query 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.token
  • api.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 Unreleased updated 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

No one assigned

    Labels

    area:apiREST + SSE API (FastAPI)area:securitySecurity hardening, supply chainmilestone:M7M7 — APIpriority:P0Critical path / blocks otherssize:S<= 1 daytaskChild task of a milestone epictype:featureNew functionality

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions