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.

3.7 KB
Raw Blame
# allow: no-sut-import — drives the real CSRFMiddleware ASGI class directly
"""CSRFMiddleware must cap the urlencoded body it buffers for token extraction.

The middleware runs on the single uvicorn event loop. When a form-urlencoded
POST arrives without an ``X-CSRFToken`` header, it buffers the body and runs
``parse_qs`` to look for the ``csrf_token`` field — both synchronous. Without a
cap, an authenticated caller could POST a multi-hundred-MB urlencoded body and
stall every request/WebSocket on that parse. The middleware caps the buffer and
fails closed (403) past the limit; a legitimate CSRF-bearing form is a few KB,
and large submissions are expected to carry the token in the header.
"""

import asyncio

from local_deep_research.web.dependencies.csrf import CSRFMiddleware


async def _drive(scope, body_chunks):
    """Run CSRFMiddleware over a request and capture the response + whether
    the inner app was reached."""
    inner_called = {"hit": False}

    async def inner_app(scope, receive, send):
        inner_called["hit"] = True
        # Drain and 200 so a *passed* CSRF check is observable.
        while True:
            msg = await receive()
            if not msg.get("more_body", False):
                break
        await send(
            {"type": "http.response.start", "status": 200, "headers": []}
        )
        await send({"type": "http.response.body", "body": b"ok"})

    mw = CSRFMiddleware(inner_app)

    sent = []
    chunks = list(body_chunks)

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

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

    await mw(scope, receive, send)
    status = next(
        (m["status"] for m in sent if m["type"] == "http.response.start"),
        None,
    )
    body = b"".join(
        m.get("body", b"") for m in sent if m["type"] == "http.response.body"
    )
    return status, body, inner_called["hit"]


def _scope(csrf_token="tok", extra_headers=None):
    headers = [
        (b"content-type", b"application/x-www-form-urlencoded"),
    ]
    if extra_headers:
        headers.extend(extra_headers)
    return {
        "type": "http",
        "method": "POST",
        "path": "/settings/api/llm.model",
        "headers": headers,
        "session": {"_csrf_token": csrf_token} if csrf_token else {},
    }


def test_oversized_urlencoded_body_rejected_without_buffering_all_of_it():
    # 2 MB body, no X-CSRFToken header, no csrf_token field -> 403, inner app
    # never reached (body not forwarded, parse_qs not run on the full body).
    big = b"x=" + (b"A" * (2 * 1024 * 1024))
    status, body, inner_hit = asyncio.run(_drive(_scope(), [big]))
    assert status == 403, (status, body)
    assert inner_hit is False
    assert b"header" in body.lower()


def test_small_form_with_token_field_passes():
    # The no-JS fallback: a small urlencoded form carrying csrf_token=tok in
    # the body must still validate and reach the app.
    small = b"value=x&csrf_token=tok"
    status, _body, inner_hit = asyncio.run(_drive(_scope(), [small]))
    assert status == 200
    assert inner_hit is True


def test_header_token_bypasses_body_buffering_entirely():
    # A valid X-CSRFToken header means the body is never buffered/parsed by
    # the middleware — even a large body streams straight to the app.
    big = b"x=" + (b"A" * (2 * 1024 * 1024))
    status, _body, inner_hit = asyncio.run(
        _drive(
            _scope(extra_headers=[(b"x-csrftoken", b"tok")]),
            [big],
        )
    )
    assert status == 200
    assert inner_hit is True