fix(openapi): declare the admin session cookie on every guarded operation
/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.
This commit is contained in:
parent
76035535bc
commit
967c504057
3 changed files with 198 additions and 1 deletions
26
app/main.py
26
app/main.py
|
|
@ -28,13 +28,14 @@ import httpx
|
|||
import structlog
|
||||
from fastapi import FastAPI, HTTPException, Query, WebSocket, WebSocketDisconnect
|
||||
from fastapi.middleware.gzip import GZipMiddleware
|
||||
from fastapi.openapi.utils import get_openapi
|
||||
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from sqlalchemy import and_, func, or_, select, text
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database import async_session, init_extensions
|
||||
from auth import AdminAuthMiddleware, router as auth_router
|
||||
from auth import AdminAuthMiddleware, apply_openapi_security, router as auth_router
|
||||
from models import (
|
||||
alerts, documents, entities, entity_events, events, feed_sources, fires,
|
||||
articles, article_scores, article_summaries, news_items,
|
||||
|
|
@ -123,6 +124,29 @@ app.add_middleware(GZipMiddleware, minimum_size=1024)
|
|||
app.add_middleware(AdminAuthMiddleware)
|
||||
app.include_router(auth_router)
|
||||
|
||||
|
||||
def _custom_openapi() -> dict:
|
||||
"""OpenAPI document that declares the admin-cookie requirement.
|
||||
|
||||
The gate lives in ``AdminAuthMiddleware``, so nothing in the route
|
||||
decorators tells FastAPI which operations need a session. Security is
|
||||
stamped from ``auth.is_public`` — the same allowlist the middleware
|
||||
enforces — rather than hand-maintained per route.
|
||||
"""
|
||||
if app.openapi_schema:
|
||||
return app.openapi_schema
|
||||
schema = get_openapi(
|
||||
title=app.title,
|
||||
version=app.version,
|
||||
description=app.description,
|
||||
routes=app.routes,
|
||||
)
|
||||
app.openapi_schema = apply_openapi_security(schema)
|
||||
return app.openapi_schema
|
||||
|
||||
|
||||
app.openapi = _custom_openapi
|
||||
|
||||
STATIC_DIR = Path(__file__).parent / "static"
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue