Skip to content

~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.

18.6 KB
Raw Blame
"""Edge-case tests for the custom CSRF middleware.

Drives the REAL ``CSRFMiddleware`` + Starlette ``SessionMiddleware`` stack
(same relative order as ``fastapi_app.py``: CSRF runs inside Session) through
a minimal FastAPI app via TestClient, so token minting, cookie signing and
session binding are all exercised end-to-end without importing the heavy
application module.

Covers:
- valid token accepted via X-CSRFToken / X-CSRF-Token header and via the
  ``csrf_token`` form field (including a case-variant Content-Type)
- missing / wrong token -> 403
- token is bound to the session that minted it (session A's token is
  rejected on session B)
- exempt paths (/auth/csrf-token, /ws*) skip
  validation; exact exemptions do NOT extend to suffixed paths
- header precedence: a wrong X-CSRFToken is not rescued by a valid form field
- JSON / multipart bodies are never parsed for the token (fail closed)
- the buffered form body is replayed intact to the handler (Form parsing and
  chunked-body accumulation both still work)
- safe methods (GET / HEAD) are never blocked; non-http scopes pass through
- non-ASCII token bytes (header or form field) -> 403, not an unhandled 500
  (regression test for a TypeError from ``secrets.compare_digest``)

Body-cap behavior (256 KB urlencoded buffering limit) is covered separately in
tests/web/dependencies/test_csrf_body_cap.py.
"""

import asyncio

import pytest
from fastapi import FastAPI, Form, Request
from fastapi.testclient import TestClient
from starlette.middleware.sessions import SessionMiddleware

from local_deep_research.web.dependencies.csrf import (
    CSRFMiddleware,
    generate_csrf_token,
)


def _make_app() -> FastAPI:
    app = FastAPI()

    @app.get("/token")
    def token(request: Request):
        # Mints (or returns) the per-session CSRF token, mirroring the real
        # /auth/csrf-token endpoint.
        return {"csrf_token": generate_csrf_token(request)}

    @app.post("/mutate")
    def mutate():
        return {"ok": True}

    @app.put("/mutate")
    def mutate_put():
        return {"ok": True}

    @app.delete("/mutate")
    def mutate_delete():
        return {"ok": True}

    @app.patch("/mutate")
    def mutate_patch():
        return {"ok": True}

    @app.post("/echo-form")
    def echo_form(value: str = Form(...)):
        # Parses the (replayed) urlencoded body via FastAPI's Form machinery,
        # proving the middleware handed the buffered body back intact.
        return {"value": value}

    # HEAD registered explicitly: FastAPI's @app.get does not auto-register
    # HEAD, and a routing 405 would be indistinguishable from a CSRF block
    # in the "safe methods" test below.
    @app.api_route("/page", methods=["GET", "HEAD"])
    def page():
        return {"page": True}

    # Routes at the middleware's exempt paths so a skipped validation is
    # observable as a 200 from the handler (a regression would 403 first).
    @app.post("/auth/validate-password")
    def validate_password():
        return {"exempt": "exact"}

    @app.post("/auth/csrf-token")
    def csrf_token_mint():
        return {"exempt": "mint"}

    # NOT exempt: exact-path matching must not degrade into prefix matching
    # (see the _SKIP_EXACT_PATHS comment in csrf.py about /auth/login-attacker
    # style routes). If this handler is ever reached without a token, exact
    # matching has regressed to startswith.
    @app.post("/auth/csrf-token-extra")
    async def _csrf_token_extra():  # pragma: no cover - routing only
        return {"exempt": "should-not-happen"}

    @app.post("/auth/validate-password-extra")
    def validate_password_extra():
        return {"exempt": "should-never-skip"}

    @app.post("/ws")
    def ws_mount():
        return {"exempt": "prefix-bare"}

    @app.post("/ws/socket.io/")
    def ws_sub():
        return {"exempt": "prefix-sub"}

    # Same LIFO order as fastapi_app.py: CSRF added first => runs INSIDE
    # SessionMiddleware, so scope["session"] is populated when it validates.
    app.add_middleware(CSRFMiddleware)
    app.add_middleware(SessionMiddleware, secret_key="test-secret-key")
    return app


def _client(app=None) -> TestClient:
    return TestClient(app or _make_app(), raise_server_exceptions=False)


def _mint(client: TestClient) -> str:
    """Fetch the session-bound CSRF token (sets the session cookie)."""
    resp = client.get("/token")
    assert resp.status_code == 200
    return resp.json()["csrf_token"]


# ---------------------------------------------------------------------------
# Valid-token acceptance
# ---------------------------------------------------------------------------


def test_valid_token_via_x_csrftoken_header_accepted():
    client = _client()
    tok = _mint(client)
    resp = client.post("/mutate", headers={"X-CSRFToken": tok})
    assert resp.status_code == 200
    assert resp.json() == {"ok": True}


def test_valid_token_via_x_csrf_token_alias_header_accepted():
    # The middleware also honors the dashed spelling X-CSRF-Token.
    client = _client()
    tok = _mint(client)
    resp = client.put("/mutate", headers={"X-CSRF-Token": tok})
    assert resp.status_code == 200


def test_valid_token_via_form_field_accepted():
    # No-JS fallback: token travels as a csrf_token urlencoded form field.
    client = _client()
    tok = _mint(client)
    resp = client.post("/mutate", data={"value": "x", "csrf_token": tok})
    assert resp.status_code == 200
    assert resp.json() == {"ok": True}


def test_form_field_accepted_with_case_variant_content_type():
    # Media types are case-insensitive (RFC 9110); a spec-valid
    # "Application/X-WWW-Form-Urlencoded" must still trigger form parsing.
    client = _client()
    tok = _mint(client)
    resp = client.post(
        "/mutate",
        content=f"csrf_token={tok}".encode(),
        headers={"Content-Type": "Application/X-WWW-Form-Urlencoded"},
    )
    assert resp.status_code == 200


def test_json_body_token_field_is_not_honored():
    # Only urlencoded forms are parsed for the token; a JSON body carrying
    # a "csrf_token" key must NOT satisfy the check (fail closed).
    client = _client()
    tok = _mint(client)
    resp = client.post("/mutate", json={"csrf_token": tok})
    assert resp.status_code == 403


def test_multipart_csrf_field_is_not_honored():
    # Deliberate design: only urlencoded bodies are buffered/parsed (so file
    # uploads stream instead of being read into memory). A csrf_token field
    # inside multipart/form-data must NOT satisfy the check — uploads send
    # the token via the X-CSRFToken header.
    client = _client()
    tok = _mint(client)
    resp = client.post(
        "/mutate",
        data={"csrf_token": tok},
        files={"file": ("a.txt", b"hello")},
    )
    assert resp.status_code == 403


def test_form_body_replayed_intact_to_handler_after_validation():
    # The middleware consumes the urlencoded body to find csrf_token, then
    # must replay it so the endpoint's own Form parsing still works. A replay
    # regression (lost/truncated body) would 4xx here despite a valid token.
    client = _client()
    tok = _mint(client)
    resp = client.post(
        "/echo-form", data={"value": "hello world", "csrf_token": tok}
    )
    assert resp.status_code == 200
    assert resp.json() == {"value": "hello world"}


def test_valid_token_via_header_accepted_for_patch():
    client = _client()
    tok = _mint(client)
    resp = client.patch("/mutate", headers={"X-CSRFToken": tok})
    assert resp.status_code == 200


# ---------------------------------------------------------------------------
# Missing / wrong token
# ---------------------------------------------------------------------------


def test_missing_token_with_session_rejected_403():
    client = _client()
    _mint(client)  # session exists, but no token sent on the POST
    resp = client.post("/mutate")
    assert resp.status_code == 403
    assert "missing or invalid" in resp.json()["error"]


def test_wrong_token_rejected_403():
    client = _client()
    _mint(client)
    resp = client.post("/mutate", headers={"X-CSRFToken": "f" * 64})
    assert resp.status_code == 403
    assert "missing or invalid" in resp.json()["error"]


def test_wrong_token_in_form_field_rejected_403():
    client = _client()
    _mint(client)
    resp = client.post("/mutate", data={"csrf_token": "not-the-token"})
    assert resp.status_code == 403


def test_delete_without_token_rejected_403():
    # DELETE is in the unsafe set alongside POST/PUT/PATCH.
    client = _client()
    _mint(client)
    resp = client.delete("/mutate")
    assert resp.status_code == 403


def test_request_without_session_rejected_403_even_with_header():
    # Fresh client: no session cookie at all -> fail closed regardless of
    # any header the attacker invents.
    client = _client()
    resp = client.post("/mutate", headers={"X-CSRFToken": "a" * 64})
    assert resp.status_code == 403
    assert "csrf-token" in resp.json()["error"].lower()


def test_patch_without_token_rejected_403():
    # PATCH is in the unsafe set alongside POST/PUT/DELETE.
    client = _client()
    _mint(client)
    resp = client.patch("/mutate")
    assert resp.status_code == 403


def test_wrong_header_not_rescued_by_valid_form_field():
    # The header takes precedence: when X-CSRFToken is present (but wrong),
    # the middleware does not fall back to parsing the form body, so a valid
    # csrf_token field cannot rescue the request. Fail closed.
    client = _client()
    tok = _mint(client)
    resp = client.post(
        "/mutate",
        data={"csrf_token": tok},
        headers={"X-CSRFToken": "f" * 64},
    )
    assert resp.status_code == 403


# ---------------------------------------------------------------------------
# Session binding
# ---------------------------------------------------------------------------


def test_token_from_other_session_rejected():
    app = _make_app()
    client_a = _client(app)
    client_b = _client(app)

    token_a = _mint(client_a)
    token_b = _mint(client_b)
    assert token_a != token_b  # distinct sessions mint distinct tokens

    # Session B presenting session A's (perfectly well-formed) token -> 403.
    resp = client_b.post("/mutate", headers={"X-CSRFToken": token_a})
    assert resp.status_code == 403

    # Sanity: each session's own token still works, so the rejection above
    # is the binding check and not some unrelated failure.
    assert (
        client_b.post("/mutate", headers={"X-CSRFToken": token_b}).status_code
        == 200
    )
    assert (
        client_a.post("/mutate", headers={"X-CSRFToken": token_a}).status_code
        == 200
    )


# ---------------------------------------------------------------------------
# Exempt paths
# ---------------------------------------------------------------------------


def test_validate_password_is_no_longer_exempt():
    """Regression fence: /auth/validate-password WAS exempt and is not any
    more. It is CSRF-protected on main, and the shipped caller
    (static/js/security/auth-validation.js) already sends both the
    X-CSRFToken header and a csrf_token form field, so the exemption was
    an unnecessary hole — a cross-site page could POST passwords to it.
    """
    client = _client()
    resp = client.post("/auth/validate-password")
    assert resp.status_code == 403


def test_exempt_token_mint_path_skips_validation():
    # The token-mint endpoint itself can't require a token to fetch one.
    client = _client()
    resp = client.post("/auth/csrf-token")
    assert resp.status_code == 200
    assert resp.json() == {"exempt": "mint"}


def test_exact_exemption_does_not_extend_to_suffixed_paths():
    # /auth/csrf-token is exempt EXACTLY; /auth/csrf-token-extra must still
    # be enforced. Catches a regression from exact matching to startswith
    # (the login-CSRF vector called out in csrf.py's comments).
    client = _client()
    resp = client.post("/auth/csrf-token-extra")
    assert resp.status_code == 403


def test_exempt_ws_prefix_skips_validation():
    client = _client()
    resp = client.post("/ws")  # bare mount path
    assert resp.status_code == 200
    assert resp.json() == {"exempt": "prefix-bare"}

    resp = client.post("/ws/socket.io/")  # sub-path under the mount
    assert resp.status_code == 200
    assert resp.json() == {"exempt": "prefix-sub"}


# ---------------------------------------------------------------------------
# Safe methods
# ---------------------------------------------------------------------------


def test_safe_methods_never_blocked():
    client = _client()  # no session, no token
    assert client.get("/page").status_code == 200
    assert client.head("/page").status_code == 200


def test_get_not_blocked_even_with_bogus_token_header():
    # Safe methods skip validation entirely — a garbage token on a GET must
    # not produce a 403.
    client = _client()
    resp = client.get("/page", headers={"X-CSRFToken": "garbage"})
    assert resp.status_code == 200


# ---------------------------------------------------------------------------
# Non-ASCII token bytes (audit bug: TypeError -> 500 before the guard)
# ---------------------------------------------------------------------------


@pytest.mark.parametrize("raw", [b"\xff\xfe", b"caf\xe9", b"\x80" * 64])
def test_non_ascii_header_token_rejected_403_not_500(raw):
    # Header values are latin-1-decoded by the middleware; before the guard,
    # secrets.compare_digest raised TypeError on the resulting non-ASCII str
    # and the request died with an unhandled 500. It must be a plain 403.
    client = _client()
    _mint(client)
    # A plain bytes-keyed dict, NOT `httpx.Headers(...)`: the client's own
    # header class is whichever httpx `starlette.testclient` imported, and
    # the Docker test image has both httpx and httpx2 installed
    # (`openai>=3.3` requires httpx2). Handing an httpx-v1 `Headers` to an
    # httpx2 client makes it re-normalise the already-latin-1-decoded str
    # and die with UnicodeEncodeError before the request is built, so the
    # 403 this test exists to assert is never reached. Both versions accept
    # a bytes mapping and pass the bytes through verbatim, which is the
    # non-ASCII header this test needs.
    resp = client.post("/mutate", headers={b"X-CSRFToken": raw})
    assert resp.status_code == 403, (
        f"non-ASCII header token must 403, got {resp.status_code}"
    )
    assert "missing or invalid" in resp.json()["error"]


def test_non_ascii_form_field_token_rejected_403_not_500():
    # %FF%FE decodes (errors="replace") to U+FFFD chars — non-ASCII — which
    # also used to raise TypeError inside compare_digest.
    client = _client()
    _mint(client)
    resp = client.post(
        "/mutate",
        content=b"csrf_token=%FF%FE",
        headers={"Content-Type": "application/x-www-form-urlencoded"},
    )
    assert resp.status_code == 403, (
        f"non-ASCII form token must 403, got {resp.status_code}"
    )


# ---------------------------------------------------------------------------
# Direct-ASGI edges: chunked body buffering/replay, non-http scopes
# (cap behavior itself is covered in tests/web/dependencies/test_csrf_body_cap)
# ---------------------------------------------------------------------------


def _drive_asgi(scope, body_chunks):
    """Run the middleware over a raw ASGI exchange; capture the response and
    the exact body the inner app read (to verify replay fidelity)."""

    seen = {"hit": False, "body": b""}

    async def inner_app(scope, receive, send):
        seen["hit"] = True
        chunks = []
        while True:
            msg = await receive()
            chunks.append(msg.get("body", b""))
            if not msg.get("more_body", False):
                break
        seen["body"] = b"".join(chunks)
        await send(
            {"type": "http.response.start", "status": 200, "headers": []}
        )
        await send({"type": "http.response.body", "body": b"ok"})

    mw = CSRFMiddleware(inner_app)
    sent = []
    pending = list(body_chunks)

    async def receive():
        if pending:
            chunk = pending.pop(0)
            return {
                "type": "http.request",
                "body": chunk,
                "more_body": bool(pending),
            }
        return {"type": "http.request", "body": b"", "more_body": False}

    async def send(message):
        sent.append(message)

    asyncio.run(mw(scope, receive, send))
    status = next(
        (m["status"] for m in sent if m["type"] == "http.response.start"),
        None,
    )
    return status, seen


def test_chunked_form_body_split_mid_token_still_validates_and_replays():
    # The token field arrives split across two http.request messages; the
    # middleware must accumulate ALL chunks before parsing (a regression that
    # parses only the first message would 403) and then replay the complete
    # body to the inner app (truncation would corrupt the handler's form).
    full = b"value=x&csrf_token=sessiontok123"
    scope = {
        "type": "http",
        "method": "POST",
        "path": "/mutate",
        "headers": [(b"content-type", b"application/x-www-form-urlencoded")],
        "session": {"_csrf_token": "sessiontok123"},
    }
    status, seen = _drive_asgi(scope, [full[:20], full[20:]])
    assert status == 200
    assert seen["hit"] is True
    assert seen["body"] == full


@pytest.mark.parametrize("scope_type", ["websocket", "lifespan"])
def test_non_http_scope_passes_through_untouched(scope_type):
    # Websocket (and lifespan) scopes must be forwarded verbatim on the
    # scope-type check ALONE. The scope is deliberately poisoned with an
    # unsafe method, a non-exempt path, and no session: if the
    # `scope["type"] != "http"` early-return ever regresses, the middleware
    # proceeds to enforcement, finds no session token, and tries to send a
    # 403 — tripping the raising `send` stub below. (A benign scope — no
    # method, or an exempt /ws path — would pass even without the guard,
    # because the GET default and the exemption branches also forward.)

    seen = {"hit": False}

    async def inner_app(scope, receive, send):
        seen["hit"] = True

    mw = CSRFMiddleware(inner_app)
    scope = {
        "type": scope_type,
        "method": "POST",  # unsafe if (wrongly) treated as http
        "path": "/other/socket.io/",  # NOT in _SKIP_* exemptions
        "headers": [],
    }

    async def receive():  # pragma: no cover - must never be called
        raise AssertionError(
            "middleware must not read the body of a non-http scope"
        )

    async def send(message):  # pragma: no cover - must never be called
        raise AssertionError(
            "middleware must not respond on its own to a non-http scope"
        )

    asyncio.run(mw(scope, receive, send))
    assert seen["hit"] is True