~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""CSRF token *lifecycle* contracts, and where they diverge from ``origin/main``.
Companion to the existing CSRF suite, deliberately non-overlapping with it:
* ``tests/web/test_csrf_middleware_edges.py`` — per-request accept/reject
edges (header names, form field, JSON/multipart, cross-session binding,
exempt paths, non-ASCII *provided* token).
* ``tests/web/dependencies/test_csrf_body_cap.py`` — the 256 KB form cap.
* ``tests/security/test_csrf_hardening.py`` — exemption policy membership.
* ``tests/web/routers/test_auth_flow_gaps.py`` /
``tests/web/test_auth_session_lifecycle.py`` — token rotation across login
(session-fixation fence) and refusal of a pre-login token afterwards.
What is left, and what this file covers:
1. **Fail-closed across the whole unsafe-method set.** The edge suite spot-
checks POST/PATCH/DELETE by hand; ``PUT`` is never exercised and nothing
is driven off ``_UNSAFE_METHODS`` itself, so a method added to that set
would ship untested. The tests below iterate the real frozenset, and pair
every rejection with a same-method acceptance so a 403 can never be a
routing artefact. ``OPTIONS`` (CORS preflight) is checked to stay outside
the set — the edge suite only covers GET/HEAD.
2. **Exemption-set parity with main, by equality.** ``test_csrf_hardening``
asserts membership (``"/auth/csrf-token" in _SKIP_EXACT_PATHS``), which
cannot catch an *added* exemption. These assert the sets exactly, and tie
the "``/api/v1`` is no longer exempt" hardening claim to main's actual
``csrf.exempt`` call site rather than to prose. Also enforced here: the
standing constraint csrf.py records about its bare ``"/ws"`` prefix entry
("do NOT register a route whose path starts with /ws ... or it ships
CSRF-exempt"), which nothing checked.
3. **Malformed *session-side* token.** Both the middleware and the public
``validate_csrf_token`` helper fail closed when the signed session payload
contains a non-ASCII or non-string token. The session is SECRET_KEY-signed,
but treating stale or future-writer state as an ordinary mismatch keeps a
security check from becoming an attacker-amplifiable 500 path.
4. **Token lifetime.** main ran Flask-WTF on its stock configuration, whose
``WTF_CSRF_TIME_LIMIT`` default is 3600s — a token older than an hour was
refused even inside a live session. The port's token carries no timestamp
at all: its only bound is the session cookie's ``max_age``. That is a real
behavioural divergence (hours -> the whole session) and nothing pinned it.
5. **Form-field name.** Flask-WTF's ``_get_csrf_token`` accepted any form key
*ending* in ``csrf_token`` (its WTForms prefix support); the port matches
the exact key only. Pinned, together with a scan proving no shipped
template depends on the dropped behaviour.
Everything here drives the REAL ``CSRFMiddleware`` under Starlette's
``SessionMiddleware`` in the same relative order as ``fastapi_app.py`` (CSRF
inside Session), via TestClient — no re-implementation of the middleware's
logic.
"""
import re
import subprocess
import time
from pathlib import Path
import itsdangerous.timed
import pytest
from fastapi import FastAPI, Request
from fastapi.testclient import TestClient
from starlette.middleware.sessions import SessionMiddleware
import local_deep_research
from local_deep_research.web.dependencies.csrf import (
_SKIP_EXACT_PATHS,
_SKIP_PATH_PREFIXES,
_UNSAFE_METHODS,
CSRFMiddleware,
generate_csrf_token,
validate_csrf_token,
)
REPO_ROOT = Path(__file__).resolve().parents[2]
PKG_ROOT = Path(local_deep_research.__file__).resolve().parent
MAIN_APP_FACTORY = "origin/main:src/local_deep_research/web/app_factory.py"
# Starlette's SessionMiddleware max_age in fastapi_app.py is
# `security.session_remember_me_days` (default 30) converted to seconds.
_SESSION_MAX_AGE = 30 * 24 * 3600
# Flask-WTF's stock defaults, which main ran on unmodified (asserted from
# main's source in the tests that depend on them, rather than trusted).
_FLASK_WTF_DEFAULT_METHODS = frozenset({"POST", "PUT", "PATCH", "DELETE"})
_FLASK_WTF_DEFAULT_TIME_LIMIT = 3600
def _git(*args: str) -> str | None:
"""Run a read-only git command, or None if it cannot be answered.
None always means "this environment cannot tell me" (a shallow checkout
has no ``origin/main``), never "the answer is empty".
"""
try:
completed = subprocess.run(
["git", *args],
cwd=REPO_ROOT,
capture_output=True,
timeout=120,
check=False,
)
except (OSError, subprocess.SubprocessError):
return None
if completed.returncode != 0:
return None
return completed.stdout.decode("utf-8", "replace")
def _main_app_factory() -> str:
src = _git("show", MAIN_APP_FACTORY)
if src is None:
pytest.skip(
"no readable origin/main (shallow checkout?) — cannot compare "
"against the Flask-WTF configuration this test is a parity gate for"
)
return src
# ---------------------------------------------------------------------------
# Harness — the real middleware pair, minimal app
# ---------------------------------------------------------------------------
_MUTATING = ["POST", "PUT", "PATCH", "DELETE"]
def _make_app() -> FastAPI:
app = FastAPI()
app.state.mutation_calls = 0
@app.get("/token")
def token(request: Request):
return {"csrf_token": generate_csrf_token(request)}
@app.get("/plant")
def plant(request: Request, kind: str):
"""Force a hostile/legacy value into the session's _csrf_token slot.
Stands in for a session payload this middleware did not mint — a
forged cookie, or a future writer of the key other than
generate_csrf_token.
"""
request.session["_csrf_token"] = _MALFORMED_SESSION_VALUES[kind]
return {"planted": kind}
@app.get("/helper")
def helper(request: Request, token: str):
"""Expose the public `validate_csrf_token` helper's return value."""
return {"valid": validate_csrf_token(request, token)}
@app.api_route("/mutate", methods=_MUTATING)
def mutate():
app.state.mutation_calls += 1
return {"ok": True}
@app.api_route("/preflight", methods=["OPTIONS"])
def preflight():
return {"ok": True}
# A route under main's one CSRF-exempt blueprint. Reaching this handler
# without a token would mean the port re-adopted main's api_v1 exemption.
@app.api_route("/api/v1/research", methods=_MUTATING)
def api_v1_research():
return {"reached": True}
app.add_middleware(CSRFMiddleware)
app.add_middleware(
SessionMiddleware,
secret_key="test-secret-key",
max_age=_SESSION_MAX_AGE,
)
return app
_MALFORMED_SESSION_VALUES: dict[str, object] = {
# Non-ASCII str: compare_digest rejects non-ASCII str operands.
"non_ascii": "café" * 16,
# Non-str values: compare_digest requires str-or-bytes-like.
"bool": True,
"dict": {"token": "a" * 64},
"float": 1.5,
"int": 12345,
"list": ["a" * 64],
}
def _client(app=None) -> TestClient:
return TestClient(app or _make_app(), raise_server_exceptions=False)
def _mint(client: TestClient) -> str:
resp = client.get("/token")
assert resp.status_code == 200
return resp.json()["csrf_token"]
def _age_the_session(monkeypatch, seconds: int) -> None:
"""Advance the wall clock by `seconds` for everything already issued.
Patches `time.time` itself rather than just itsdangerous' signer, so BOTH
possible expiry mechanisms move together: the session cookie's
itsdangerous age (TimestampSigner.get_timestamp is `int(time.time())`)
and any wall-clock CSRF expiry the middleware might grow. Patching only
the signer would let a `time.time()`-based token time limit slip past
these tests unnoticed.
"""
real_time = time.time
base = real_time()
monkeypatch.setattr(time, "time", lambda: real_time() + seconds)
# Sanity: the patch is visible where the session signature is verified.
assert (
itsdangerous.timed.TimestampSigner.get_timestamp(
itsdangerous.timed.TimestampSigner("k")
)
>= int(base + seconds) - 1
)
# ---------------------------------------------------------------------------
# 1. Fail-closed across the entire unsafe-method set
# ---------------------------------------------------------------------------
def test_unsafe_method_set_matches_the_flask_wtf_default_main_ran_on():
"""The set of guarded methods must not have narrowed in the port.
main never assigned WTF_CSRF_METHODS, so Flask-WTF's default
{POST, PUT, PATCH, DELETE} applied. Dropping (say) PUT from
`_UNSAFE_METHODS` would silently un-guard every PUT route.
"""
main_src = _main_app_factory()
assert "WTF_CSRF_METHODS" not in main_src, (
"main overrode WTF_CSRF_METHODS — this parity assertion's premise "
"(that main ran Flask-WTF's default method set) no longer holds"
)
assert frozenset(_UNSAFE_METHODS) == _FLASK_WTF_DEFAULT_METHODS
@pytest.mark.parametrize("method", sorted(_UNSAFE_METHODS))
def test_every_unsafe_method_fails_closed_with_no_session(method):
"""No session => no session-bound token => 403, for every guarded method.
Driven off the real frozenset so a method added to it is covered on the
day it is added. Covers PUT, which the edge suite never exercises.
"""
client = _client()
resp = client.request(method, "/mutate")
assert resp.status_code == 403, (
f"{method} /mutate without a session must fail closed, "
f"got {resp.status_code}"
)
@pytest.mark.parametrize("method", sorted(_UNSAFE_METHODS))
def test_every_unsafe_method_fails_closed_with_a_wrong_token(method):
"""A session exists but the presented token is not its token => 403."""
client = _client()
minted = _mint(client)
wrong = "0" * 64
assert wrong != minted
resp = client.request(method, "/mutate", headers={"X-CSRFToken": wrong})
assert resp.status_code == 403, (
f"{method} /mutate with a non-matching token must fail closed, "
f"got {resp.status_code}"
)
@pytest.mark.parametrize("method", sorted(_UNSAFE_METHODS))
def test_every_unsafe_method_accepts_its_own_session_token(method):
"""Positive control for the two tests above.
Without this, a 403 from a route that simply does not accept `method`
would read as CSRF enforcement.
"""
client = _client()
token = _mint(client)
resp = client.request(method, "/mutate", headers={"X-CSRFToken": token})
assert resp.status_code == 200, (
f"{method} /mutate with the session's own token must be accepted, "
f"got {resp.status_code}"
)
assert resp.json() == {"ok": True}
def test_options_preflight_is_not_challenged():
"""CORS preflight must not be treated as state-changing.
OPTIONS is outside Flask-WTF's default method set and must stay outside
`_UNSAFE_METHODS`: a 403'd preflight breaks every cross-origin caller
before the real request is ever sent. The edge suite covers GET/HEAD only.
"""
assert "OPTIONS" not in _UNSAFE_METHODS
client = _client() # no session, no token
resp = client.request("OPTIONS", "/preflight")
assert resp.status_code == 200, (
f"OPTIONS preflight must not be CSRF-challenged, got {resp.status_code}"
)
# ---------------------------------------------------------------------------
# 2. Exemption-set parity with main — by equality, not membership
# ---------------------------------------------------------------------------
def test_exempt_sets_are_exactly_the_reviewed_literals():
"""Equality fence over both skip lists.
test_csrf_hardening asserts membership, which cannot notice an *added*
exemption — the direction that actually loses protection. Any new entry
must land here and be justified in review.
"""
assert set(_SKIP_EXACT_PATHS) == {"/auth/csrf-token"}
assert set(_SKIP_PATH_PREFIXES) == {"/ws/", "/ws"}
def test_mains_only_csrf_exemption_was_api_v1_and_the_port_dropped_it():
"""Ties the "stricter than main" claim to main's actual exempt call site.
main exempted exactly one blueprint (api_v1) and nothing else. The port
exempts it nowhere, so a mutating /api/v1 request without a token must be
refused rather than reaching the handler.
"""
main_src = _main_app_factory()
assert 'for bp_name in ("api_v1",):' in main_src, (
"main's CSRF exemption list is no longer the single api_v1 blueprint "
"this comparison assumes"
)
assert main_src.count("csrf.exempt(") == 1, (
"main gained another csrf.exempt() call site; the port's exemption "
"set must be re-compared against it"
)
client = _client()
resp = client.post("/api/v1/research")
assert resp.status_code == 403, (
"/api/v1 was blanket CSRF-exempt on main and must NOT be exempt here "
f"(it authenticates with session cookies); got {resp.status_code}"
)
# Control: the same route is reachable once the token is presented, so the
# 403 above is the CSRF check and not a missing route.
token = _mint(client)
ok = client.post("/api/v1/research", headers={"X-CSRFToken": token})
assert ok.status_code == 200 and ok.json() == {"reached": True}
def _router_paths() -> list[tuple[str, str]]:
"""(file, full path) for every route literal declared in web/routers/."""
prefix_re = re.compile(r"APIRouter\((?P<args>[^)]*)\)", re.DOTALL)
kw_re = re.compile(r"prefix\s*=\s*[\"'](?P<p>[^\"']*)[\"']")
route_re = re.compile(
r"@\w+\.(?:get|post|put|patch|delete|head|options|api_route)\(\s*"
r"[\"'](?P<path>[^\"']*)[\"']"
)
found: list[tuple[str, str]] = []
router_dir = PKG_ROOT / "web" / "routers"
for py in sorted(router_dir.glob("*.py")):
text = py.read_text(encoding="utf-8")
prefix = ""
for m in prefix_re.finditer(text):
kw = kw_re.search(m.group("args"))
if kw:
prefix = kw.group("p")
break
for m in route_re.finditer(text):
path = m.group("path")
joined = f"{prefix.rstrip('/')}/{path.lstrip('/')}"
found.append((py.name, joined))
return found
def test_no_router_route_shadows_the_bare_ws_exempt_prefix():
"""Enforces the standing constraint csrf.py records about its "/ws" entry.
`_SKIP_PATH_PREFIXES` contains a bare `"/ws"`, matched with `startswith`,
so ANY route path beginning with those two characters (`/wsearch`,
`/ws-export`, ...) ships CSRF-exempt. csrf.py calls this out as inert
today and a standing constraint on future routes; nothing enforced it.
"""
paths = _router_paths()
assert len(paths) > 50, (
f"route scan found only {len(paths)} paths — the scan broke, and a "
"silently-empty scan would make this gate vacuous"
)
offenders = [(f, p) for f, p in paths if p.startswith("/ws") and p != "/ws"]
assert not offenders, (
"these routes start with the bare '/ws' CSRF-exempt prefix and would "
f"ship without CSRF protection: {offenders}"
)
# ---------------------------------------------------------------------------
# 3. Malformed SESSION-side token
# ---------------------------------------------------------------------------
@pytest.mark.parametrize("kind", sorted(_MALFORMED_SESSION_VALUES))
def test_malformed_session_token_fails_closed_with_403_not_500(kind):
"""An unusable session token must reject the request, not crash it.
A 500 here is worse than a 403 on three counts: it is the non-fail-closed
outcome for a security check, it is indistinguishable from a server fault
to callers, and it takes the traceback path on every such request.
"""
app = _make_app()
client = _client(app)
assert client.get("/plant", params={"kind": kind}).status_code == 200
resp = client.post("/mutate", headers={"X-CSRFToken": "a" * 64})
assert resp.status_code == 403, (
f"session _csrf_token of kind {kind!r} produced "
f"{resp.status_code}, not the fail-closed 403"
)
assert resp.json() == {"error": "CSRF token missing or invalid"}
assert app.state.mutation_calls == 0, (
f"the protected handler ran for malformed session token {kind!r}"
)
@pytest.mark.parametrize("kind", sorted(_MALFORMED_SESSION_VALUES))
def test_validate_csrf_token_helper_returns_false_on_malformed_session(kind):
"""The public helper must answer False, not raise, on a hostile session.
`validate_csrf_token` is exported and documented for out-of-band checks,
and its docstring promises a bool ("True if the token matches, False
otherwise"). The middleware and helper must share the same guarded,
constant-time comparison path.
"""
client = _client()
assert client.get("/plant", params={"kind": kind}).status_code == 200
resp = client.get("/helper", params={"token": "a" * 64})
assert resp.status_code == 200, (
f"validate_csrf_token raised on a {kind!r} session token "
f"(HTTP {resp.status_code}) instead of returning a bool"
)
assert resp.json() == {"valid": False}
@pytest.mark.parametrize("kind", sorted(_MALFORMED_SESSION_VALUES))
def test_token_mint_rotates_malformed_session_state_to_a_usable_token(kind):
"""The mint endpoint must recover a session that validation rejects.
Returning a truthy non-string/non-ASCII value unchanged leaves the client
unable to make any protected request: the endpoint appears to mint a token,
but the comparison path must reject it forever. Rotation makes that state
self-healing while preserving normal stable-token behaviour below.
"""
app = _make_app()
client = _client(app)
assert client.get("/plant", params={"kind": kind}).status_code == 200
token = _mint(client)
assert re.fullmatch(r"[0-9a-f]{64}", token), (
f"malformed {kind!r} session state was returned instead of rotated"
)
assert _mint(client) == token, "the replacement token must remain stable"
helper = client.get("/helper", params={"token": token})
assert helper.status_code == 200
assert helper.json() == {"valid": True}
response = client.post("/mutate", headers={"X-CSRFToken": token})
assert response.status_code == 200
assert response.json() == {"ok": True}
assert app.state.mutation_calls == 1
def test_well_formed_session_token_is_unaffected_by_the_above():
"""Control: the normal path rejects mismatches and accepts an exact match.
Confirms rejection is caused by malformed state rather than a harness
that can never reach either the helper's true branch or the mutator.
"""
client = _client()
minted = _mint(client)
assert _mint(client) == minted
resp = client.post("/mutate", headers={"X-CSRFToken": "a" * 64})
assert resp.status_code == 403
helper = client.get("/helper", params={"token": "a" * 64})
assert helper.status_code == 200
assert helper.json() == {"valid": False}
good = client.get("/helper", params={"token": minted})
assert good.status_code == 200
assert good.json() == {"valid": True}
mutation = client.post("/mutate", headers={"X-CSRFToken": minted})
assert mutation.status_code == 200
assert mutation.json() == {"ok": True}
assert client.app.state.mutation_calls == 1
# ---------------------------------------------------------------------------
# 4. Token lifetime — divergence from Flask-WTF's 1-hour default
# ---------------------------------------------------------------------------
def test_token_outlives_the_flask_wtf_time_limit_main_ran_on(monkeypatch):
"""DIVERGENCE FROM MAIN, pinned deliberately.
main never set WTF_CSRF_TIME_LIMIT, so Flask-WTF's 3600s default applied:
a token older than an hour was refused even inside a live session, because
the transmitted token was a timestamped itsdangerous payload. The port's
token is bare `secrets.token_hex(32)` with no timestamp, so an hours-old
token is still accepted. Documented here so the reduction in
defence-in-depth is a reviewed decision rather than an accident.
"""
main_src = _main_app_factory()
assert "WTF_CSRF_TIME_LIMIT" not in main_src, (
"main set WTF_CSRF_TIME_LIMIT — the 3600s default this divergence is "
"measured against no longer describes main"
)
client = _client()
token = _mint(client)
assert re.fullmatch(r"[0-9a-f]{64}", token), (
"the port's token must be bare hex; a timestamp-bearing token would "
f"change what this test measures (got {token!r})"
)
_age_the_session(monkeypatch, _FLASK_WTF_DEFAULT_TIME_LIMIT * 2)
resp = client.post("/mutate", headers={"X-CSRFToken": token})
assert resp.status_code == 200, (
"a token twice Flask-WTF's 3600s default age was refused — the port "
"gained a CSRF time limit; update this divergence note if intended"
)
def test_session_cookie_max_age_is_the_only_bound_on_token_lifetime(
monkeypatch,
):
"""The token dies with its session cookie and not before.
Complements the test above: past the session cookie's own max_age the
signature no longer verifies, the session reads empty, and the middleware
takes its "no session token" fail-closed branch. That cookie window is
the entire lifetime of a CSRF token in the port.
"""
client = _client()
token = _mint(client)
_age_the_session(monkeypatch, _SESSION_MAX_AGE + 3600)
resp = client.post("/mutate", headers={"X-CSRFToken": token})
assert resp.status_code == 403, (
"a token whose session cookie has outlived max_age must be refused, "
f"got {resp.status_code}"
)
assert "missing" in resp.json()["error"].lower()
# ---------------------------------------------------------------------------
# 5. Form-field name parity
# ---------------------------------------------------------------------------
def test_prefixed_form_field_name_is_not_honored_unlike_flask_wtf():
"""The port matches `csrf_token` exactly; main matched any key ending in it.
Flask-WTF's `_get_csrf_token` looped `for key in request.form: if
key.endswith(field_name)` to support WTForms' form prefixes, so
`myform-csrf_token` authenticated a request on main. The port reads
`parse_qs(...)["csrf_token"]` only. Stricter, and safe *given* the scan in
the next test; pinned so the narrowing is not rediscovered as a bug.
"""
client = _client()
token = _mint(client)
headers = {"Content-Type": "application/x-www-form-urlencoded"}
resp = client.post(
"/mutate", content=f"myform-csrf_token={token}", headers=headers
)
assert resp.status_code == 403, (
"a WTForms-prefixed field name was accepted — the port grew main's "
f"endswith() matching; got {resp.status_code}"
)
# Control: the exact field name, same request shape, is accepted.
ok = client.post("/mutate", content=f"csrf_token={token}", headers=headers)
assert ok.status_code == 200, (
"the exact csrf_token field must still work, so the 403 above is the "
f"field NAME and not the form path; got {ok.status_code}"
)
_CSRF_FIELD_RE = re.compile(r"""name=["']([^"']*csrf_token)["']""")
_FORM_TAG_RE = re.compile(r"<form\b[^>]*>", re.IGNORECASE | re.DOTALL)
def _template_csrf_findings() -> dict:
"""Scan shipped templates for forms relying on dropped Flask-WTF behaviour.
Module-level (not inlined in the test) so the scanner can be exercised
directly against a mutated package copy without booting pytest.
"""
templates = PKG_ROOT / "web" / "templates"
prefixed: list[str] = []
multipart_with_field: list[str] = []
total_fields = 0
multipart_forms = 0
for html in sorted(templates.rglob("*.html")):
text = html.read_text(encoding="utf-8")
rel = str(html.relative_to(templates))
for name in _CSRF_FIELD_RE.findall(text):
total_fields += 1
if name != "csrf_token":
prefixed.append(f"{rel}: {name}")
for form_tag in _FORM_TAG_RE.findall(text):
if "multipart/form-data" not in form_tag.lower():
continue
multipart_forms += 1
# Body of this form: from the open tag to the next </form>.
start = text.index(form_tag) + len(form_tag)
end = text.lower().find("</form>", start)
body = text[start:] if end == -1 else text[start:end]
if _CSRF_FIELD_RE.search(body):
multipart_with_field.append(rel)
return {
"prefixed": prefixed,
"multipart_with_field": multipart_with_field,
"total_fields": total_fields,
"multipart_forms": multipart_forms,
}
def test_no_shipped_template_depends_on_a_dropped_field_behaviour():
"""No rendered form relies on behaviour the port removed.
Two dropped Flask-WTF behaviours could break a form silently (403 at
submit, no server-side clue): WTForms-prefixed field names, and reading
the token out of a *multipart* body (Flask's `request.form` covered
multipart; this middleware parses urlencoded only and documents that
multipart callers must send the header).
"""
found = _template_csrf_findings()
assert found["total_fields"] >= 5 and found["multipart_forms"] >= 1, (
f"template scan found {found['total_fields']} csrf fields / "
f"{found['multipart_forms']} multipart forms — the scan broke, and an "
"empty scan would make this gate vacuous"
)
assert not found["prefixed"], (
"these templates use a WTForms-prefixed csrf field name, which this "
"middleware does not honour (submits would 403): "
f"{found['prefixed']}"
)
assert not found["multipart_with_field"], (
"these templates put csrf_token in a multipart form; the middleware "
"never parses multipart bodies for it, so the submit 403s unless JS "
f"sends the X-CSRFToken header: {found['multipart_with_field']}"
)