Merge auth/ of boilerplate 09e144d (lockout, recovery, cookies, tokens, 421 on a foreign Host) and the hardening headers on every answer (wp-up-auth)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Fable 5.1 committed 2026-10-27 11:52:30 +01:00
commit 6513e2ab95
23 files changed
+2432 -220

No files matched your search

+93
View File
@@ -0,0 +1,93 @@
"""The three hardening headers `app.main.Hardening` puts on every HTTP answer:
a page, an API answer, the error handler's answers and the door's own
refusals, since it sits outside all of them. The socket's handshake is not an
HTTP answer and passes through untouched."""
from __future__ import annotations
from collections.abc import Iterator
from pathlib import Path
import pytest
from fastapi import FastAPI, Response
from fastapi.testclient import TestClient
from httpx import Headers
from app.config import Config, ServerConfig
from app.main import HARDENING, create_app
@pytest.fixture
def app(tmp_path: Path) -> FastAPI:
return create_app(Config(server=ServerConfig(host="127.0.0.1", port=0),
database=tmp_path / "roledesk.db"))
@pytest.fixture
def client(app: FastAPI) -> Iterator[TestClient]:
"""Reached as 127.0.0.1: an app open on loopback answers 421 to any
other `Host`."""
with TestClient(app, base_url="http://127.0.0.1") as c:
yield c
def hardened(headers: Headers) -> None:
for name, value in HARDENING.items():
assert headers.get(name) == value, name
def test_a_page_carries_the_three_headers(client: TestClient) -> None:
answer = client.get("/settings/account")
assert answer.status_code == 200
hardened(answer.headers)
def test_an_api_answer_carries_them(client: TestClient) -> None:
answer = client.get("/api/account")
assert answer.status_code == 200
assert answer.headers["content-type"].startswith("application/json")
hardened(answer.headers)
def test_an_error_answer_carries_them(client: TestClient) -> None:
"""The JSON error and the error page both come from the exception handler,
inside the middleware."""
missing = client.get("/api/no-such-thing")
assert missing.status_code == 404
assert "detail" in missing.json()
hardened(missing.headers)
page = client.get("/no-such-page")
assert page.status_code == 404
assert page.headers["content-type"].startswith("text/html")
hardened(page.headers)
def test_the_doors_refusal_carries_them(app: FastAPI) -> None:
"""auth's 421 for a host that is not loopback is answered by its
middleware, which `Hardening` wraps."""
with TestClient(app, base_url="http://rebound.example") as stranger:
refused = stranger.get("/")
assert refused.status_code == 421
hardened(refused.headers)
def test_a_header_the_route_sets_is_kept(app: FastAPI, client: TestClient) -> None:
@app.get("/api/framed")
def framed() -> Response:
return Response("{}", media_type="application/json",
headers={"X-Frame-Options": "SAMEORIGIN"})
answer = client.get("/api/framed")
assert answer.headers["x-frame-options"] == "SAMEORIGIN"
assert answer.headers["x-content-type-options"] == "nosniff"
def test_the_socket_handshake_is_left_alone(client: TestClient) -> None:
"""TestClient names `testserver` on a socket whatever its `base_url`, so
the handshake names the loopback host itself."""
with client.websocket_connect("/ws/jobs", headers={"host": "127.0.0.1"}) as socket:
accepted = {bytes(name).decode().lower()
for name, _ in (socket.extra_headers or [])}
assert socket.receive_json()["type"] == "jobs"
assert not accepted & {name.lower() for name in HARDENING}
+3 -1
View File
@@ -41,7 +41,9 @@ def store(app: FastAPI) -> Store:
@pytest.fixture
def client(app: FastAPI) -> Iterator[TestClient]:
with TestClient(app) as c:
"""Reached as 127.0.0.1, the name a browser uses: an app open on loopback
answers 421 to any other `Host`, TestClient's `testserver` included."""
with TestClient(app, base_url="http://127.0.0.1") as c:
yield c