fix(openapi): declare the admin session cookie on every guarded operation #71

Merged
sirius merged 1 commit from fix/openapi-admin-security into master 2026-11-27 05:30:00 +00:00
Owner

Fixes audit finding F17 (2026-11-27-cluster-netsec-audit.md).

/openapi.json is publicly served and declared no securitySchemes at all — all 22 mutating operations read as security: None while AdminAuthMiddleware answers 401 {"detail":"admin authentication required"} on them. Generated clients, scanners and reviewers reading the spec concluded the API was unauthenticated.

Root cause

The admin gate is middleware, not a route dependency, so nothing in the decorators gave FastAPI a scheme to infer. The fix does not move the check into per-route dependencies (the middleware stays the single enforcement point).

Change

app/auth.py

  • components.securitySchemes.adminSession: apiKey / in: cookie / name: osint_session — the exact cookie the middleware reads (HttpOnly, SameSite=Lax, HMAC-SHA256 signed, 7-day). The 401/403 behaviour is in the description. No bearer scheme was invented.
  • apply_openapi_security() stamps every documented operation from auth.is_public() — the same allowlist the middleware enforces, so the document cannot drift from the behaviour.

app/main.py

  • app.openapi is overridden with a generator that runs default get_openapi() then apply_openapi_security() (cached in app.openapi_schema as usual).

Resulting spec (69 paths, 77 operations):

  • 22 mutating operations total → 19 declare security: [{adminSession: []}], plus GET /api/keys, GET /api/settings, GET /api/news/models and the rest of the admin reads.
  • The 3 deliberately public mutating operations stay public: POST /api/search (read-only query), POST /api/auth/login, POST /api/auth/logout.
  • Public reads (/api/aircraft, /api/stats, /api/health, /api/news, …) carry an explicit security: [] rather than an omitted field.
  • Structural delta vs the old spec is only security + securitySchemes — verified by diffing the two documents with those fields stripped.

/docs, /redoc, /openapi.json and the HUD are unchanged (still public); no decision needed from Lance.

Tests (tests/test_auth.py, +10)

  • test_openapi_publishes_the_admin_session_cookie_scheme — scheme is truthful (apiKey/cookie/osint_session).
  • test_openapi_json_endpoint_serves_the_scheme — the real endpoint serves it.
  • test_every_documented_operation_pairs_with_the_middleware — the pair, per documented operation: declares security ⇒ the real app answers 401; declares [] ⇒ the real AdminAuthMiddleware instance admits the request (200 from a sentinel downstream). Also asserts no operation is left with an absent security key.
  • test_admin_reads_are_documented_as_protected (/api/keys, /api/settings), test_public_reads_are_documented_as_unauthenticated (/api/aircraft, /api/stats, /api/health).
  • test_every_route_is_in_the_documented_surface — every APIRoute in app.routes must appear in the spec, so a route cannot dodge the pair by being undocumented.
  • test_mutating_surface_is_admin_only_except_the_public_three — the public set is pinned; a new mutating route that forgets to be gated (or is gated but undocumented) fails the pair test.

Verification

  • Red on the untouched base: reverting only the apply_openapi_security() call fails 9 of the new tests (the route-coverage test stays green, as it only asserts documentation coverage).
  • pytest: 442 passed, 3 failed. All 3 failures are pre-existing on master @ 7603553 and unrelated to auth — confirmed by stashing the patch and re-running them:
    • test_hud_chrome.py::test_initmap_prefers_viirs_true_color and ::test_news_pins_are_circle_markers (stale basemap/pin assertions vs the 915f6cb Blue Marble HUD change),
    • test_geofence.py::test_camera_publish_and_insert_do_not_notify (monkeypatches run_camera_service.nats, which no longer exists).
  • tests/test_auth.py: 64 passed.

Live /openapi.json will change after the image is built and the operator pins the new sha in sirius/homek8s — the deployed contract stays as-is until then.

Fixes audit finding **F17** (`2026-11-27-cluster-netsec-audit.md`). `/openapi.json` is publicly served and declared **no `securitySchemes` at all** — all 22 mutating operations read as `security: None` while `AdminAuthMiddleware` answers `401 {"detail":"admin authentication required"}` on them. Generated clients, scanners and reviewers reading the spec concluded the API was unauthenticated. ## Root cause The admin gate is middleware, not a route dependency, so nothing in the decorators gave FastAPI a scheme to infer. The fix does **not** move the check into per-route dependencies (the middleware stays the single enforcement point). ## Change `app/auth.py` - `components.securitySchemes.adminSession`: `apiKey` / `in: cookie` / `name: osint_session` — the exact cookie the middleware reads (HttpOnly, SameSite=Lax, HMAC-SHA256 signed, 7-day). The 401/403 behaviour is in the description. No bearer scheme was invented. - `apply_openapi_security()` stamps every documented operation from `auth.is_public()` — the **same allowlist the middleware enforces**, so the document cannot drift from the behaviour. `app/main.py` - `app.openapi` is overridden with a generator that runs default `get_openapi()` then `apply_openapi_security()` (cached in `app.openapi_schema` as usual). Resulting spec (69 paths, 77 operations): - **22 mutating operations total → 19 declare `security: [{adminSession: []}]`**, plus `GET /api/keys`, `GET /api/settings`, `GET /api/news/models` and the rest of the admin reads. - The 3 deliberately public mutating operations stay public: `POST /api/search` (read-only query), `POST /api/auth/login`, `POST /api/auth/logout`. - Public reads (`/api/aircraft`, `/api/stats`, `/api/health`, `/api/news`, …) carry an **explicit `security: []`** rather than an omitted field. - Structural delta vs the old spec is *only* `security` + `securitySchemes` — verified by diffing the two documents with those fields stripped. `/docs`, `/redoc`, `/openapi.json` and the HUD are unchanged (still public); no decision needed from Lance. ## Tests (`tests/test_auth.py`, +10) - `test_openapi_publishes_the_admin_session_cookie_scheme` — scheme is truthful (`apiKey`/`cookie`/`osint_session`). - `test_openapi_json_endpoint_serves_the_scheme` — the real endpoint serves it. - `test_every_documented_operation_pairs_with_the_middleware` — the pair, per documented operation: declares security ⇒ the real app answers 401; declares `[]` ⇒ the real `AdminAuthMiddleware` instance admits the request (200 from a sentinel downstream). Also asserts no operation is left with an absent `security` key. - `test_admin_reads_are_documented_as_protected` (`/api/keys`, `/api/settings`), `test_public_reads_are_documented_as_unauthenticated` (`/api/aircraft`, `/api/stats`, `/api/health`). - `test_every_route_is_in_the_documented_surface` — every `APIRoute` in `app.routes` must appear in the spec, so a route cannot dodge the pair by being undocumented. - `test_mutating_surface_is_admin_only_except_the_public_three` — the public set is pinned; a new mutating route that forgets to be gated (or is gated but undocumented) fails the pair test. ## Verification - Red on the untouched base: reverting only the `apply_openapi_security()` call fails 9 of the new tests (the route-coverage test stays green, as it only asserts documentation coverage). - `pytest`: **442 passed, 3 failed**. All 3 failures are pre-existing on `master` @ `7603553` and unrelated to auth — confirmed by stashing the patch and re-running them: - `test_hud_chrome.py::test_initmap_prefers_viirs_true_color` and `::test_news_pins_are_circle_markers` (stale basemap/pin assertions vs the `915f6cb` Blue Marble HUD change), - `test_geofence.py::test_camera_publish_and_insert_do_not_notify` (monkeypatches `run_camera_service.nats`, which no longer exists). - `tests/test_auth.py`: 64 passed. Live `/openapi.json` will change after the image is built and the operator pins the new sha in `sirius/homek8s` — the deployed contract stays as-is until then.
/openapi.json is served to the internet and declared no securitySchemes at
all, so every one of the 22 mutating operations read as unauthenticated while
the middleware answers 401 on them. The enforcement lives in
AdminAuthMiddleware, not in a route dependency, so FastAPI had nothing to
infer a scheme from.

The spec is now stamped from auth.is_public() — the same allowlist the
middleware enforces — instead of a hand-maintained per-route list:
- components.securitySchemes.adminSession: apiKey in cookie, name
  osint_session (the cookie the middleware actually reads), with the 401/403
  behaviour documented.
- protected operations: security: [{"adminSession": []}] — 19 of the 22
  mutating operations plus GET /api/keys, GET /api/settings, /api/news/models.
- public operations: security: [] (explicit "no auth"; not omitted).
  POST /api/search, POST /api/auth/login and POST /api/auth/logout stay
  public, and the read-only HUD surface is untouched.

Tests pin the pair per documented operation: the real app must answer 401 on
every operation that declares security, and the middleware must admit every
operation that declares []. Verified red on the untouched base (9 failures)
and green after the change.
sirius force-pushed fix/openapi-admin-security from 967c504057 to 5a39404384 2026-11-27 05:27:53 +00:00 Compare
sirius merged commit 6066ac8e0d into master 2026-11-27 05:30:00 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
sirius/osint-dashboard!71
No description provided.