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
51
app/auth.py
51
app/auth.py
|
|
@ -153,6 +153,57 @@ def is_public(path: str, method: str) -> bool:
|
|||
return False
|
||||
|
||||
|
||||
# ── OpenAPI security declaration ─────────────────────────────────────────────
|
||||
# The admin check runs in :class:`AdminAuthMiddleware`, not in a route
|
||||
# dependency, so FastAPI cannot infer a scheme from the decorators. The
|
||||
# published spec is derived from :func:`is_public` instead — the same allowlist
|
||||
# the middleware enforces — so the contract and the behaviour cannot drift.
|
||||
# ``tests/test_auth.py`` asserts both halves of that pair for every documented
|
||||
# operation.
|
||||
|
||||
SECURITY_SCHEME_NAME = "adminSession"
|
||||
|
||||
SECURITY_SCHEME: dict = {
|
||||
"type": "apiKey",
|
||||
"in": "cookie",
|
||||
"name": COOKIE_NAME,
|
||||
"description": (
|
||||
"Admin session cookie set by `POST /api/auth/login` (HttpOnly, "
|
||||
"SameSite=Lax, HMAC-SHA256 signed, 7-day lifetime). Every `/api/*` "
|
||||
"operation outside the public read allowlist answers "
|
||||
'`401 {"detail": "admin authentication required"}` without it, and '
|
||||
"`403` while `ADMIN_PASSWORD_HASH`/`ADMIN_SESSION_SECRET` are unset. "
|
||||
"Browser clients sign in at `/login`; other clients POST credentials "
|
||||
"and replay the cookie."
|
||||
),
|
||||
}
|
||||
|
||||
_OPENAPI_METHODS = frozenset(
|
||||
{"get", "put", "post", "delete", "options", "head", "patch", "trace"}
|
||||
)
|
||||
|
||||
|
||||
def apply_openapi_security(spec: dict) -> dict:
|
||||
"""Stamp the admin-cookie requirement onto every guarded operation.
|
||||
|
||||
Protected operations get ``security: [{"adminSession": []}]``. Public ones
|
||||
get ``security: []`` so the document states "no auth" explicitly — an
|
||||
absent field is indistinguishable from an inherited global requirement.
|
||||
"""
|
||||
components = spec.setdefault("components", {})
|
||||
schemes = components.setdefault("securitySchemes", {})
|
||||
schemes[SECURITY_SCHEME_NAME] = dict(SECURITY_SCHEME)
|
||||
for path, item in (spec.get("paths") or {}).items():
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
for method, operation in item.items():
|
||||
if method.lower() not in _OPENAPI_METHODS or not isinstance(operation, dict):
|
||||
continue
|
||||
protected = not is_public(path, method.upper())
|
||||
operation["security"] = [{SECURITY_SCHEME_NAME: []}] if protected else []
|
||||
return spec
|
||||
|
||||
|
||||
# ── Config (read per call so tests and reloads pick up env changes) ─────────
|
||||
|
||||
def _env(name: str) -> str:
|
||||
|
|
|
|||
Loading…
Reference in a new issue