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.

11.1 KB
Raw Blame
"""Anti-buffering header fences for frontend-consumed SSE endpoints.

Server-Sent Events streamed through a reverse proxy (nginx) are buffered
unless the response carries ``X-Accel-Buffering: no``; without
``Cache-Control: no-cache`` an intermediary may serve a stale stream.
Every SSE progress endpoint the frontend consumes must therefore set both
headers, or live progress silently turns into one giant flush at the end.

Audit of ``StreamingResponse`` usage across the FastAPI routers found four
SSE endpoints:

* ``rag.py::index_collection``   — sets the headers (fenced here),
* ``library.py::download_bulk``  — sets the headers (fenced here),
* ``library.py::download_all_text`` — did NOT set them (frontend calls it
  via ``fetch('/library/api/download-all-text')`` in library.html and
  download_manager.html); fixed alongside this test to mirror
  ``download_bulk``,
* ``rag.py::index_all``          — did NOT set them (read by a fetch-based
  SSE client; the ``RAG_INDEX_ALL`` constant in urls.js has no caller in
  the shipped frontend, whose Index All button POSTs
  ``COLLECTION_INDEX_START`` instead); fixed alongside this test to
  mirror ``index_collection``.

The HTTP tests drive the real routers through Starlette's TestClient with
``require_auth`` overridden and only true boundaries mocked (DB sessions,
RAG service, session password store); each asserts on the endpoint's own
generator output as well, so a test cannot pass without the real route
running. A source-level audit test additionally fails if a NEW
``text/event-stream`` StreamingResponse is added to any router without the
anti-buffering headers.

(The remaining StreamingResponse usages — PDF serving in library.py and
the NDJSON/report exports in research.py — are one-shot downloads, not
SSE, where proxy buffering is harmless.)
"""

import ast
from contextlib import contextmanager
from pathlib import Path
from unittest.mock import MagicMock

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

TEST_USER = "sse-header-tester"
STREAMING_RESPONSE_NAMES = frozenset(
    {"StreamingResponse", "WorkerCleanupStreamingResponse"}
)


def _make_client(router):
    """Minimal app: real router + SessionMiddleware, auth dependency
    overridden. No CSRF middleware, so POST SSE endpoints are reachable
    directly."""
    from local_deep_research.web.dependencies.auth import require_auth

    app = FastAPI()
    app.add_middleware(SessionMiddleware, secret_key="test-secret")
    app.include_router(router)
    app.dependency_overrides[require_auth] = lambda: TEST_USER
    return TestClient(app, raise_server_exceptions=False)


def _assert_anti_buffering(resp):
    """The package's core assertion: SSE responses must defeat proxy
    buffering and caching."""
    assert resp.status_code == 200, resp.text[:300]
    assert resp.headers.get("content-type", "").startswith(
        "text/event-stream"
    ), resp.headers.get("content-type")
    cache_control = resp.headers.get("cache-control", "")
    assert "no-cache" in cache_control, (
        f"SSE response is missing Cache-Control: no-cache (got "
        f"{cache_control!r}) — intermediaries may cache/replay the stream"
    )
    assert resp.headers.get("x-accel-buffering") == "no", (
        f"SSE response is missing X-Accel-Buffering: no (got "
        f"{resp.headers.get('x-accel-buffering')!r}) — nginx will buffer "
        f"the stream and progress events arrive in one burst at the end"
    )


# ---------------------------------------------------------------------------
# library.py SSE endpoints
# ---------------------------------------------------------------------------


@pytest.fixture
def library_client(monkeypatch):
    """Client for the library router.

    ``get_authenticated_user_password`` is patched to raise, which makes
    each SSE generator take its real early-exit path (yield one
    'Authentication required' event, close) — the response object and its
    headers are still built by the real route code.
    """
    from local_deep_research.web.routers import library as library_mod

    def _no_password(*_a, **_k):
        raise library_mod.AuthenticationRequiredError()

    monkeypatch.setattr(
        library_mod, "get_authenticated_user_password", _no_password
    )
    return _make_client(library_mod.router)


@pytest.mark.timeout(30)
def test_download_all_text_sse_sets_anti_buffering_headers(library_client):
    """POST /library/api/download-all-text streams SSE progress to the
    library and download-manager pages; it must carry the anti-buffering
    headers like its sibling download_bulk (this was the missing fence
    found by the audit — the route previously sent NO headers)."""
    resp = library_client.post("/library/api/download-all-text")
    _assert_anti_buffering(resp)
    # Anti-tautology: the real generator ran and emitted its SSE event.
    assert "Authentication required" in resp.text
    assert resp.text.startswith("data: ")


@pytest.mark.timeout(30)
def test_download_bulk_sse_sets_anti_buffering_headers(library_client):
    """Existing-behavior fence: download_bulk already sets the headers;
    regressing them would break live progress in the download manager."""
    resp = library_client.post(
        "/library/api/download-bulk",
        json={"research_ids": ["r-1"], "mode": "text_only"},
    )
    _assert_anti_buffering(resp)
    assert "Authentication required" in resp.text


# ---------------------------------------------------------------------------
# rag.py SSE endpoints
# ---------------------------------------------------------------------------


@pytest.fixture
def rag_client(monkeypatch):
    """Client for the RAG router.

    True boundaries mocked: the RAG service (embedding models), the
    settings manager (returns each setting's default), and the user DB
    session (collection lookup returns None, so each generator takes its
    real 'Collection not found' path after the response headers are set).
    """
    import local_deep_research.utilities.db_utils as db_utils_mod
    from local_deep_research.database import session_context
    from local_deep_research.web.routers import rag as rag_mod

    monkeypatch.setattr(
        rag_mod, "get_rag_service", lambda *_a, **_k: MagicMock()
    )

    class _FakeSettings:
        def get_setting(self, _key, default=None, **_k):
            return default

    monkeypatch.setattr(
        rag_mod, "get_settings_manager", lambda *_a, **_k: _FakeSettings()
    )
    # index_all re-imports get_settings_manager from db_utils at request
    # time, so the source module needs the same patch.
    monkeypatch.setattr(
        db_utils_mod, "get_settings_manager", lambda *_a, **_k: _FakeSettings()
    )

    @contextmanager
    def _fake_db_session(*_a, **_k):
        session = MagicMock()
        session.query.return_value.filter_by.return_value.first.return_value = (
            None
        )
        yield session

    # Both routes import get_user_db_session from session_context inside
    # the route body, so patching the source module intercepts them.
    monkeypatch.setattr(
        session_context, "get_user_db_session", _fake_db_session
    )
    return _make_client(rag_mod.router)


@pytest.mark.timeout(30)
def test_index_collection_sse_sets_anti_buffering_headers(rag_client):
    """Existing-behavior fence: POST /library/api/collections/{id}/index
    sets Cache-Control no-cache + X-Accel-Buffering no (plus
    no-transform); dropping them would re-break collection indexing
    progress behind nginx."""
    resp = rag_client.post("/library/api/collections/coll-missing/index")
    _assert_anti_buffering(resp)
    assert "Collection not found" in resp.text


@pytest.mark.timeout(30)
def test_index_all_sse_sets_anti_buffering_headers(rag_client):
    """POST /library/api/rag/index-all streams bulk-index progress; the
    audit found it sent NO anti-buffering headers (fixed alongside this
    test to mirror index_collection).

    POST because the route mutates — it is the verb, not a path
    allowlist, that puts it behind CSRFMiddleware. The handler reads
    ``collection_id``/``force_reindex`` from the query string either
    way, and this fixture's app has no CSRF middleware.
    """
    resp = rag_client.post(
        "/library/api/rag/index-all", params={"collection_id": "coll-missing"}
    )
    _assert_anti_buffering(resp)
    # Real generator ran: start event, then collection-not-found error.
    assert "Starting bulk indexing" in resp.text
    assert "Collection not found" in resp.text


# ---------------------------------------------------------------------------
# Source-level audit: no SSE StreamingResponse without the headers
# ---------------------------------------------------------------------------


def _routers_dir() -> Path:
    import local_deep_research.web.routers as routers_pkg

    return Path(routers_pkg.__file__).parent


def _sse_calls_missing_headers(path: Path):
    """Return [(lineno, has_headers)] for every
    StreamingResponse(..., media_type="text/event-stream") call in the
    file. ``has_headers`` is True when 'X-Accel-Buffering' appears within
    the call itself or the 6 lines after it (covers both the headers=
    kwarg style and index_collection's response.headers[...] style)."""
    source = path.read_text()
    lines = source.splitlines()
    results = []
    for node in ast.walk(ast.parse(source)):
        if not isinstance(node, ast.Call):
            continue
        func = node.func
        name = getattr(func, "id", None) or getattr(func, "attr", None)
        if name not in STREAMING_RESPONSE_NAMES:
            continue
        is_sse = any(
            kw.arg == "media_type"
            and isinstance(kw.value, ast.Constant)
            and kw.value.value == "text/event-stream"
            for kw in node.keywords
        )
        if not is_sse:
            continue
        window = "\n".join(
            lines[node.lineno - 1 : (node.end_lineno or node.lineno) + 6]
        )
        results.append((node.lineno, "X-Accel-Buffering" in window))
    return results


@pytest.mark.timeout(30)
def test_every_sse_streaming_response_sets_anti_buffering_headers():
    """Audit fence: any router adding a text/event-stream
    StreamingResponse without X-Accel-Buffering near it fails here, so
    new SSE endpoints cannot silently reintroduce the proxy-buffering
    bug fixed for download_all_text and index_all."""
    per_file = {}
    for py_file in sorted(_routers_dir().glob("*.py")):
        calls = _sse_calls_missing_headers(py_file)
        if calls:
            per_file[py_file.name] = calls

    # Sanity (anti-tautology): the scanner must actually see the four
    # known SSE endpoints — two in library.py, two in rag.py. If this
    # fails, the audit above proved nothing.
    assert len(per_file.get("library.py", [])) >= 2, per_file
    assert len(per_file.get("rag.py", [])) >= 2, per_file

    violations = [
        f"{fname}:{lineno}"
        for fname, calls in per_file.items()
        for lineno, has_headers in calls
        if not has_headers
    ]
    assert violations == [], (
        "SSE StreamingResponse without anti-buffering headers "
        f"(add Cache-Control: no-cache + X-Accel-Buffering: no): {violations}"
    )