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.

23.2 KB
Raw Blame
"""
CSRF protection for FastAPI.

Implements a session-based CSRF check:
- Generate a random token, store in session
- Include token in forms as hidden field, and expose via /auth/csrf-token
- The frontend sends it back as the X-CSRFToken header (or, for
  urlencoded bodies only, a csrf_token form field)
- CSRFMiddleware validates on every state-changing request

This replaces Flask-WTF's CSRFProtect.

DECISION RECORD — "why not fastapi-csrf-protect?" (evaluated for PR #3299,
in response to a maintainer review comment preferring an established
library over hand-rolled security code, a reasonable default instinct):

  Evaluated github.com/aekasitt/fastapi-csrf-protect v1.0.7 (PyPI, Sep
  2025 release; actively maintained, no known CVEs) against this file's
  actual requirements and found it a poor fit on four independent axes:

  1. Enforcement model. It is a per-route `Depends()` dependency, not
     ASGI middleware — there is no middleware class in the package at
     all. Adopting it means adding `csrf_protect: CsrfProtect = Depends()`
     plus an explicit `await csrf_protect.validate_csrf(request)` call to
     every one of this app's ~135 POST/PUT/PATCH/DELETE route handlers
     (17 router files), each a manual opt-in. `CSRFMiddleware` here is a
     single fail-closed choke point: every unsafe-method request is
     checked unless explicitly listed in `_SKIP_EXACT_PATHS` /
     `_SKIP_PATH_PREFIXES` (itself covered by
     tests/security/test_csrf_hardening.py), and any *future* route is
     protected automatically. A per-route dependency is fail-OPEN by
     omission — one forgotten `Depends()` on any current or future
     mutating route silently ships with no CSRF check, and nothing
     catches it short of a bespoke "every mutator has the dependency"
     meta-test that would have to be built and maintained anyway.
  2. Session coupling. It uses its own signed double-submit cookie
     (itsdangerous `URLSafeTimedSerializer`, a second secret key, a
     second cookie) with no integration with Starlette's
     `SessionMiddleware`. Our token lives in the session payload
     (`request.session["_csrf_token"]`) — i.e. inside the app's existing
     signed, HttpOnly, SameSite=Lax session cookie, not a second
     cookie under a second secret. To be precise about the taxonomy:
     that makes this a *session-bound signed double-submit*, not a
     server-side synchronizer store — the token rides in the same signed
     cookie the session does. The properties that matter follow from the
     binding, not from where the bytes are parked: a cross-origin
     attacker cannot read the cookie through JavaScript (HttpOnly) or
     read the token endpoint/page across origins (same-origin policy),
     nor forge a signed session (SECRET_KEY). Explicit SameSite=Lax also
     keeps the cookie off cross-site unsafe requests while permitting
     incoming top-level GET links. The token dies with its session —
     `request.session.clear()` at login (session-fixation defence) and at
     logout drops it, so the next mint is a fresh token. The library's
     cookie is independent of auth state; its own README has route
     handlers call `unset_csrf_cookie()` by hand "to prevent token
     reuse" — invalidation becomes a per-handler responsibility instead
     of falling out of existing session lifecycle.
  3. Header + form-field support together (what this middleware needs
     for the no-JS form fallback) is only available via the package's
     `fastapi_csrf_protect.flexible` sub-package, which is materially
     less mature: introduced in 1.0.4, that release was pulled from
     PyPI as a "FAILED ROLLOUT ... WIP code" and iterated through
     1.0.5-1.0.7 to reach a working state. Its base (non-flexible) mode
     is single-location only (header XOR body), which this app's mixed
     JS/no-JS forms need to be XOR-able across the whole app, not one
     endpoint.
  4. DoS shape. The library's `get_csrf_from_body()` buffers the full
     request body via `await request.body()` with no size cap of its
     own — the exact synchronous-parse-on-the-single-event-loop hazard
     the 256 KB cap below (`_MAX_CSRF_FORM_BODY`) was added to close;
     this app's general body-size ceiling is upload-sized (tens/hundreds
     of MB), far above what's safe to `parse_qs` synchronously per
     request. That this is a real, not theoretical, hazard for this
     library is corroborated by its own issue tracker/changelog: GH
     issue #23 ("CSRF Token from body can cause 'Stream consumed'
     Exception with Form data") and the 1.0.6 changelog entry fixing a
     `Stream consumed` bug in that same code path.

  Verified against the published 1.0.7 wheel (not just its docs), Aug
  2026: the distribution contains only `core.py`, `csrf_config.py`,
  `load_config.py`, `exceptions.py` and the `flexible/` sub-package —
  no middleware module of any kind (1); `validate_csrf()` reads
  `request.cookies[cookie_key]` and unsigns it with its own
  `URLSafeTimedSerializer`, touching `request.session` nowhere (2);
  base `LoadConfig.token_location` is `Literal["body", "header"]`,
  header-XOR-body, and `flexible/` is absent from the 1.0.3 wheel and
  present in 1.0.5, matching the changelog's "1.0.4 ... FAILED ROLLOUT
  ... Rolled out with WIP code; immediately deleted version from PyPI"
  (1.0.4 is indeed missing from the PyPI release index) (3); and
  `get_csrf_from_body(await request.body())` buffers unbounded (4).
  Two things found in that read that the review comment did not raise
  and that cut *toward* this file: the library compares tokens with a
  plain `token != signature`, not a constant-time compare, where the
  code below uses `secrets.compare_digest`; and its body extraction is
  `data.decode().replace("&", '","').replace("=", '":"')` fed to a
  pydantic model — a hand-rolled urlencoded parser that mangles any
  value containing `&` or `=`. "Use a library" is the right default;
  it is not automatically the more careful option.

  None of this is a case against ever using a library for CSRF — the
  underlying pattern here (session-bound token, compared with
  `secrets.compare_digest`, carried in the app's existing signed session
  cookie) is the standard OWASP-documented approach, not novel
  crypto. It's an assessment that *this particular* library's
  architecture (per-route opt-in, decoupled cookie) doesn't fit *this*
  app's shape (single ASGI choke point, session-bound tokens, one
  event loop to protect). Re-evaluate if a library ships a pure-ASGI,
  session-integrated CSRF middleware with an equivalent exemption
  model — none was found as of this review (Aug 2026).

  Coverage this file must keep preserving: tests/security/
  test_csrf_hardening.py, test_csrf_protection.py, test_csrf_e2e_flow.py;
  tests/web/test_csrf_middleware_edges.py,
  tests/web/dependencies/test_csrf_body_cap.py; and the browser-level
  proof in tests/ui_tests/test_download_and_csrf_flows_ci.js (enforcement
  in both directions, JSON and multipart).
"""

import hashlib
import secrets

from fastapi import Request
from loguru import logger
from starlette.responses import JSONResponse
from starlette.types import ASGIApp, Receive, Scope, Send


def _log_path_preview(path: str) -> str:
    """Bounded preview of the request path for the rejection log lines.

    (#6938: the path is client-chosen and logged before authentication,
    so it goes through the shared value preview. Imported at call time so
    this middleware never imports the routers package at import time.)
    """
    from ..routers.notes import _log_value_preview

    return _log_value_preview(path)


def generate_csrf_token(request: Request) -> str:
    """Generate or retrieve CSRF token for the current session.

    Stores the token in the session so it persists across requests.
    Malformed legacy/session state is rotated instead of being returned to a
    template or API caller as a token that validation can never accept.
    """
    token = request.session.get("_csrf_token")
    if not isinstance(token, str) or not token or not token.isascii():
        token = secrets.token_hex(32)
        request.session["_csrf_token"] = token
    return token


# Response header marking a 409 that means "the page's login was replaced"
# (bound write or recovery fetch), as opposed to an endpoint's own conflict.
AUTH_CHANGED_HEADER = "X-LDR-Auth-Changed"


def get_auth_context(session) -> str | None:
    """Fingerprint a login for retry fencing, without exposing its session ID.

    This value grants no authority. CSRF validation and authentication still
    apply independently; the fingerprint only detects a different login.
    """
    session_id = session.get("session_id")
    if (
        not session.get("username")
        or not isinstance(session_id, str)
        or not session_id
    ):
        return None
    return hashlib.sha256(
        f"ldr-csrf-context:{session_id}".encode("utf-8")
    ).hexdigest()


def _tokens_match(session_token: object, provided_token: object) -> bool:
    """Compare usable string tokens without letting malformed state raise."""
    if not isinstance(session_token, str) or not isinstance(
        provided_token, str
    ):
        return False
    if not session_token.isascii() or not provided_token.isascii():
        return False
    return secrets.compare_digest(session_token, provided_token)


def validate_csrf_token(request: Request, token: str) -> bool:
    """Validate a CSRF token against the session token.

    Args:
        request: The current request.
        token: The token from the form submission.

    Returns:
        True if the token matches, False otherwise.

    Not on any request path: `CSRFMiddleware` below is the single
    enforcement point and does its own check. This exists for handlers
    that need to validate a token out-of-band (and for direct unit
    testing of the comparison); if you reach for it in a route, that is
    a sign the route should be relying on the middleware instead.
    """
    session_token = request.session.get("_csrf_token")
    if not session_token or not token:
        return False
    return _tokens_match(session_token, token)


# Methods that mutate state and require CSRF validation.
#
# Enforcement is keyed on the METHOD and nothing else, so a route that
# mutates state has to be declared on one of these verbs to be covered.
# That is deliberate: the method arrives in its own ASGI scope key and
# has exactly one spelling, whereas a per-path allowlist has to agree
# with the router about how a path is written, and disagreeing fails
# OPEN (the middleware misses, the router still matches, the handler
# runs). Keep state-changing routes on an unsafe verb rather than
# re-introducing a path allowlist to cover a mutating GET.
_UNSAFE_METHODS = frozenset({"POST", "PUT", "PATCH", "DELETE"})

# Routes that bootstrap auth or are token-authenticated and so cannot
# carry a session-bound CSRF token. These bypass CSRF validation.
# Exact paths that bootstrap auth and so cannot carry a session-bound
# CSRF token yet. Prefix matching is too permissive (e.g. /auth/login
# would also match /auth/login-attacker-route) so we match exactly.
_SKIP_EXACT_PATHS = frozenset(
    {
        # `/auth/csrf-token` is the token-mint endpoint itself — can't
        # require a token to fetch one.
        "/auth/csrf-token",
    }
)
# NOTE: `/auth/validate-password` is deliberately NOT listed here. It was
# exempt under Flask as an "idempotent strength check called before any
# session exists", but the register/change-password forms that call it
# already render a CSRF token via the same template injection used by
# `/auth/login`, so the exemption bought nothing and left an unauthenticated
# POST that accepts a password outside the middleware.
# NOTE: `/auth/login` and `/auth/register` are intentionally NOT listed
# here. Both forms render a CSRF token via template injection and POST
# it back in a hidden field; the middleware validates it normally.
# Listing them here re-opens a login-CSRF (OWASP A07) vector where an
# attacker-controlled form silently logs the victim into the attacker's
# account.

# Prefixes that route to non-cookie-authenticated subsystems. /api/v1
# is NOT in this list: those endpoints currently use require_auth
# (session cookies), so CSRF applies.
_SKIP_PATH_PREFIXES = (
    # Socket.IO ASGI handles its own auth handshake. Include both
    # `/ws/` and `/ws` because the app mounts at `/ws` (no trailing
    # slash) and a POST to the bare mount path would otherwise
    # miss the prefix match.
    #
    # Caveat this bare entry carries, and the reason the exact-match
    # rule above exists: `startswith("/ws")` also matches a
    # hypothetical `/wsearch` or `/wsomething`. Nothing routes there
    # today — `app.mount("/ws", socket_app)` serves only `/ws` and
    # `/ws/...`, and no router prefix begins with `/ws` — so this is
    # currently inert, but it is a standing constraint: do NOT register
    # a route whose path starts with `/ws` but is not the Socket.IO
    # mount, or it ships CSRF-exempt. Tighten to an exact `/ws` entry
    # plus the `/ws/` prefix if that ever stops being obvious.
    "/ws/",
    "/ws",
)


class CSRFMiddleware:
    """ASGI middleware enforcing CSRF on state-changing requests.

    Validates the `X-CSRFToken` header (preferred, set by frontend JS)
    or, for `application/x-www-form-urlencoded` bodies only, the
    `csrf_token` form field, against the per-session token stored in
    `request.session["_csrf_token"]`.

    Multipart and JSON bodies are deliberately NOT parsed for a token:
    those callers must send the header, and a multipart/JSON request
    without one fails closed with a 403 (pinned by
    tests/web/test_csrf_middleware_edges.py::
    test_multipart_csrf_field_is_not_honored and
    ::test_json_body_token_field_is_not_honored). The no-JS fallback
    only ever needs the urlencoded path, since a plain HTML form
    without JS posts urlencoded.

    Runs INSIDE SessionMiddleware so request.session is populated.
    """

    def __init__(self, app: ASGIApp) -> None:
        self.app = app

    async def __call__(
        self, scope: Scope, receive: Receive, send: Send
    ) -> None:
        if scope["type"] != "http":
            await self.app(scope, receive, send)
            return

        method = scope.get("method", "GET").upper()
        if method not in _UNSAFE_METHODS:
            await self.app(scope, receive, send)
            return

        path = scope.get("path", "")
        if path in _SKIP_EXACT_PATHS or any(
            path.startswith(p) for p in _SKIP_PATH_PREFIXES
        ):
            await self.app(scope, receive, send)
            return

        session = scope.get("session") or {}
        session_token = session.get("_csrf_token") if session else None

        # Read headers before token validation: a replacement login may not
        # have rendered a page (and minted its CSRF token) yet.
        headers = {
            k.decode("latin-1").lower(): v.decode("latin-1")
            for k, v in scope.get("headers", [])
        }
        # Bind opt-in writes to the login rendered into their page, including
        # a change between CSRF refresh and retry. This header grants no auth.
        expected_context = headers.get("x-ldr-auth-context")
        if expected_context is not None:
            context = get_auth_context(session)
            if not _tokens_match(context, expected_context):
                response = JSONResponse(
                    {
                        "error": "Your sign-in changed. Reload this page before trying again."
                        if context
                        else "Authentication required"
                    },
                    status_code=409 if context else 401,
                    # Stable marker so clients need not match on the prose
                    # (api.js isAuthContextChanged). A handler's own 409, e.g.
                    # an indexing conflict, never carries it.
                    headers={AUTH_CHANGED_HEADER: "1"} if context else None,
                )
                await response(scope, receive, send)
                return

        # Fail closed: an unsafe request MUST carry a session-bound CSRF
        # token. Endpoints legitimately reachable without one (login,
        # register, csrf-token fetch, password-strength check) are in
        # _SKIP_EXACT_PATHS above. Everything else needs a token, whether
        # the caller is authenticated or not — an attacker can forge an
        # unauthenticated POST just as easily, and an empty-session
        # bypass makes the middleware pointless for any future public
        # mutator endpoint.
        if not session_token:
            logger.warning(
                "CSRF rejected: request lacks session _csrf_token ({} {})",
                method,
                _log_path_preview(path),
            )
            response = JSONResponse(
                {"error": "CSRF token missing: fetch /auth/csrf-token first"},
                status_code=403,
                headers={"X-LDR-CSRF-Rejected": "1"},
            )
            await response(scope, receive, send)
            return

        provided = headers.get("x-csrftoken") or headers.get("x-csrf-token")

        # Only buffer the body when we actually need to read the
        # `csrf_token` form field — i.e., when the header is missing AND
        # the request is form-urlencoded. Doing this unconditionally
        # forced every file upload (multipart) and every JSON API POST
        # into memory before the handler could stream it.
        body = b""
        needs_body_replay = False
        # Lowercase the value: media types are case-insensitive (RFC 9110),
        # so a spec-valid "Application/X-WWW-Form-Urlencoded" must still match
        # — otherwise the form-body token extraction is skipped and a request
        # carrying a valid csrf_token FIELD is wrongly rejected with 403.
        content_type = headers.get("content-type", "").lower()
        if not provided and "application/x-www-form-urlencoded" in content_type:
            # Cap the buffered form body. This handler runs on the single
            # event loop, so buffering + `parse_qs` here is synchronous work
            # that stalls EVERY request/WebSocket for its duration. Without a
            # cap, an authenticated caller could POST a multi-hundred-MB
            # urlencoded body (up to BodySizeLimit's upload-sized ceiling)
            # with no X-CSRFToken header and force a multi-second parse_qs on
            # the loop. A legitimate CSRF-bearing form (the no-JS fallback)
            # is a few KB; well-behaved clients send the token in the header.
            # If the form body exceeds the cap we fail closed: leave
            # `provided` unset so the token check below rejects with 403.
            _MAX_CSRF_FORM_BODY = 256 * 1024  # 256 KB
            body_chunks: list[bytes] = []
            more_body = True
            buffered = 0
            overflowed = False
            while more_body:
                message = await receive()
                if message["type"] == "http.request":
                    chunk = message.get("body", b"")
                    buffered += len(chunk)
                    if buffered > _MAX_CSRF_FORM_BODY:
                        overflowed = True
                        # Keep draining so the connection isn't left with an
                        # unread body, but stop accumulating.
                        more_body = message.get("more_body", False)
                        continue
                    body_chunks.append(chunk)
                    more_body = message.get("more_body", False)
                else:
                    more_body = False
            body = b"".join(body_chunks)
            needs_body_replay = True
            if overflowed:
                logger.warning(
                    "CSRF rejected: urlencoded body over the form-parse cap "
                    "with no X-CSRFToken header ({} {})",
                    method,
                    _log_path_preview(path),
                )
                response = JSONResponse(
                    {
                        "error": (
                            "CSRF token missing: send it in the X-CSRFToken "
                            "header for large form submissions"
                        )
                    },
                    status_code=403,
                )
                await response(scope, receive, send)
                return
            try:
                from urllib.parse import parse_qs

                parsed = parse_qs(body.decode("utf-8", errors="replace"))
                csrf_values = parsed.get("csrf_token") or []
                if csrf_values:
                    provided = csrf_values[0]
            except Exception:
                provided = None

        # Both sides are untrusted runtime state. Header bytes are latin-1
        # decoded, form values may contain U+FFFD, and a stale/forged session
        # payload may hold a non-string value. ``compare_digest`` raises for
        # non-ASCII strings and mismatched operand types, so validate both
        # operands and fail closed with the normal 403 instead of a 500.
        if not provided or not _tokens_match(session_token, provided):
            logger.warning(
                "CSRF validation failed: {} {} (token present: {})",
                method,
                _log_path_preview(path),
                bool(provided),
            )
            response = JSONResponse(
                {"error": "CSRF token missing or invalid"},
                status_code=403,
                headers={"X-LDR-CSRF-Rejected": "1"},
            )
            await response(scope, receive, send)
            return

        # Replay the buffered body to the inner app. If we never buffered
        # (header-provided token, or multipart/JSON upload), pass
        # `receive` through unchanged so the handler reads from the wire.
        if not needs_body_replay:
            await self.app(scope, receive, send)
            return

        body_replayed = False

        async def replay_receive() -> dict:
            nonlocal body, body_replayed
            if not body_replayed:
                body_replayed = True
                chunk = body
                body = b""
                return {
                    "type": "http.request",
                    "body": chunk,
                    "more_body": False,
                }

            # The buffered body has been handed over. Defer to the real
            # transport from here on instead of manufacturing an endless
            # stream of empty http.request messages.
            #
            # This is load-bearing, not tidiness. Below ASGI spec_version
            # 2.4 — uvicorn advertises "2.3" — Starlette's StreamingResponse
            # races the body iterator against listen_for_disconnect(), which
            # is `while True: await receive()` until it observes an
            # http.disconnect. Repeating http.request never ends that loop,
            # and because this coroutine had no await point it never yielded
            # to the event loop either. A single form-token POST to a
            # streaming route (POST /library/api/download-all-text is one
            # today) therefore pinned the loop at 100% forever: with
            # workers=1 that is the whole instance — HTTP, Socket.IO and the
            # health endpoint included — until the process is killed.
            return await receive()

        await self.app(scope, replay_receive, send)