fix(openapi): declare the admin session cookie on every guarded operation #71
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/openapi-admin-security"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Fixes audit finding F17 (
2026-11-27-cluster-netsec-audit.md)./openapi.jsonis publicly served and declared nosecuritySchemesat all — all 22 mutating operations read assecurity: NonewhileAdminAuthMiddlewareanswers401 {"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.pycomponents.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 fromauth.is_public()— the same allowlist the middleware enforces, so the document cannot drift from the behaviour.app/main.pyapp.openapiis overridden with a generator that runs defaultget_openapi()thenapply_openapi_security()(cached inapp.openapi_schemaas usual).Resulting spec (69 paths, 77 operations):
security: [{adminSession: []}], plusGET /api/keys,GET /api/settings,GET /api/news/modelsand the rest of the admin reads.POST /api/search(read-only query),POST /api/auth/login,POST /api/auth/logout./api/aircraft,/api/stats,/api/health,/api/news, …) carry an explicitsecurity: []rather than an omitted field.security+securitySchemes— verified by diffing the two documents with those fields stripped./docs,/redoc,/openapi.jsonand 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 realAdminAuthMiddlewareinstance admits the request (200 from a sentinel downstream). Also asserts no operation is left with an absentsecuritykey.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— everyAPIRouteinapp.routesmust 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
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 onmaster@7603553and unrelated to auth — confirmed by stashing the patch and re-running them:test_hud_chrome.py::test_initmap_prefers_viirs_true_colorand::test_news_pins_are_circle_markers(stale basemap/pin assertions vs the915f6cbBlue Marble HUD change),test_geofence.py::test_camera_publish_and_insert_do_not_notify(monkeypatchesrun_camera_service.nats, which no longer exists).tests/test_auth.py: 64 passed.Live
/openapi.jsonwill change after the image is built and the operator pins the new sha insirius/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.967c504057to5a39404384