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.

7.6 KB
Raw Blame
"""CSRF body replay must not starve the event loop on streaming routes.

When a state-changing POST carries its CSRF token as a *form field* rather
than an ``X-CSRFToken`` header, ``CSRFMiddleware`` has to buffer the request
body to read it, then replay that body to the inner app through a
substitute ``receive`` callable.

The hazard is what that substitute does once the body is exhausted. 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``. A substitute that answers every
subsequent call with an empty ``http.request`` never ends that loop, and if
it contains no ``await`` point it never yields to the event loop either.

The result was not a stuck request but a pinned event loop: with
``workers=1`` (required for Socket.IO without Redis) a single authenticated
form-token POST to a streaming route took down HTTP, Socket.IO, SSE and the
health endpoint for the whole process until it was killed.
``POST /library/api/download-all-text`` is such a route today: a plain
``def`` handler with no body parameters, so FastAPI never drains the body,
returning a ``StreamingResponse``.

These tests drive the real middleware around a real ``StreamingResponse``
with a uvicorn-shaped scope. They run the app on its own event loop in a
worker thread and join with a hard timeout, so a regression FAILS rather
than hanging the suite — ``asyncio.wait_for`` cannot help here, because a
starved loop never gets to fire its own timer.
"""

import asyncio
import threading

import pytest
from starlette.responses import StreamingResponse

from local_deep_research.web.dependencies.csrf import CSRFMiddleware

CSRF_TOKEN = "t" * 43
STREAM_PATH = "/library/api/download-all-text"

# Generous enough never to trip on a loaded CI box, short enough that a
# regression (which spins a daemon thread at 100% CPU) is cut off quickly.
RUN_TIMEOUT_SECONDS = 15.0


async def _streaming_app(scope, receive, send):
    """Stands in for download_all_text: never reads the body, streams back."""

    async def generate():
        yield b"first-chunk"
        yield b"second-chunk"

    response = StreamingResponse(generate(), media_type="text/event-stream")
    await response(scope, receive, send)


def _uvicorn_shaped_scope(*, use_header_token: bool):
    headers = [(b"cookie", b"session=abc")]
    if use_header_token:
        headers.append((b"x-csrftoken", CSRF_TOKEN.encode()))
    else:
        headers.append((b"content-type", b"application/x-www-form-urlencoded"))
    return {
        "type": "http",
        # spec_version is the whole point: 2.3 selects Starlette's
        # listen_for_disconnect path. Hardcoded rather than read from
        # uvicorn so this keeps pinning the behaviour even if uvicorn
        # later advertises 2.4 and the starvation path stops being
        # reachable in production.
        "asgi": {"version": "3.0", "spec_version": "2.3"},
        "method": "POST",
        "path": STREAM_PATH,
        "headers": headers,
        "session": {"_csrf_token": CSRF_TOKEN},
        "client": ("127.0.0.1", 5000),
    }


def _drive(scope, body):
    """Run the middleware on a private loop in a worker thread.

    Returns (completed, sent_message_types, receive_call_count).
    """
    sent = []
    state = {"body_delivered": False, "receive_calls": 0}

    async def receive():
        state["receive_calls"] += 1
        if state["body_delivered"]:
            # A real transport with the client gone. Any correct
            # implementation reaches this and the response finishes; the
            # broken one never asks.
            return {"type": "http.disconnect"}
        state["body_delivered"] = True
        return {"type": "http.request", "body": body, "more_body": False}

    async def send(message):
        sent.append(message["type"])

    finished = threading.Event()

    def runner():
        try:
            asyncio.run(CSRFMiddleware(_streaming_app)(scope, receive, send))
        finally:
            finished.set()

    # Daemon so a regression cannot keep the interpreter alive at exit.
    threading.Thread(target=runner, daemon=True).start()
    completed = finished.wait(RUN_TIMEOUT_SECONDS)
    return completed, sent, state["receive_calls"]


class TestFormTokenReplayOnStreamingRoute:
    def test_form_token_post_to_streaming_route_completes(self):
        """The regression pin. Reverting the fix hangs the worker thread."""
        completed, sent, _ = _drive(
            _uvicorn_shaped_scope(use_header_token=False),
            f"csrf_token={CSRF_TOKEN}".encode(),
        )

        assert completed, (
            f"form-token POST to a streaming route did not finish within "
            f"{RUN_TIMEOUT_SECONDS}s — the CSRF body-replay receive is "
            f"looping on empty http.request messages and starving the "
            f"event loop (see this module's docstring)"
        )
        assert sent[0] == "http.response.start"
        assert "http.response.body" in sent

    def test_header_token_path_is_unaffected(self):
        """Control: the no-buffering path never had the problem."""
        completed, sent, _ = _drive(
            _uvicorn_shaped_scope(use_header_token=True), b""
        )

        assert completed
        assert sent[0] == "http.response.start"

    def test_buffered_body_is_replayed_once_then_defers_to_transport(self):
        """The mechanism, not just the symptom.

        The buffered body must be handed over exactly once; every later
        call has to reach the real transport so the disconnect is
        observable. If the substitute answered from its own buffer forever,
        the disconnect signal could never arrive.
        """
        received = []

        async def recording_app(scope, receive, send):
            for _ in range(3):
                received.append(await receive())
            await StreamingResponse(iter(()))(scope, receive, send)

        scope = _uvicorn_shaped_scope(use_header_token=False)
        body = f"csrf_token={CSRF_TOKEN}".encode()

        state = {"delivered": False}

        async def receive():
            if state["delivered"]:
                return {"type": "http.disconnect"}
            state["delivered"] = True
            return {"type": "http.request", "body": body, "more_body": False}

        async def send(message):
            pass

        finished = threading.Event()

        def runner():
            try:
                asyncio.run(CSRFMiddleware(recording_app)(scope, receive, send))
            finally:
                finished.set()

        threading.Thread(target=runner, daemon=True).start()
        assert finished.wait(RUN_TIMEOUT_SECONDS), "app did not finish"

        assert received[0]["type"] == "http.request"
        assert received[0]["body"] == body
        # Everything after the single replay comes from the transport.
        assert [m["type"] for m in received[1:]] == [
            "http.disconnect",
            "http.disconnect",
        ], (
            f"expected the replay receive to defer to the real transport "
            f"after delivering the body once, got {received[1:]}"
        )


@pytest.mark.parametrize("use_header_token", [True, False])
def test_streaming_route_never_leaves_response_unstarted(use_header_token):
    """Both token-delivery paths must produce a started response."""
    body = b"" if use_header_token else f"csrf_token={CSRF_TOKEN}".encode()
    completed, sent, _ = _drive(
        _uvicorn_shaped_scope(use_header_token=use_header_token), body
    )
    assert completed
    assert "http.response.start" in sent