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.

19.2 KB
Raw Blame
"""Regression guards for two review-round fixes in ``fastapi_app.py``.

FIX 1 — ``RememberMeMiddleware`` session-cookie persistence
-------------------------------------------------------------
The middleware only stripped ``Max-Age``/``Expires`` from the session
cookie when ``session["_remember_me"] is False`` — an exact identity
check. Starlette's ``SessionMiddleware`` applies a global 30-day
``max_age`` to EVERY cookie it sets, including for anonymous visitors
(no ``_remember_me`` key at all) and immediately after logout (where
``request.session.clear()`` wipes the key entirely, so it reads back as
``None``, not ``False``). Both cases read `is False` -> ``False``, so the
attribute-stripping branch never ran and the browser was handed a
persistent 30-day cookie — main (Flask) defaulted to a browser-session
cookie and only made it persistent when "remember me" was checked.

FIX 2 — ``BodySizeLimitMiddleware`` JSON body cap
--------------------------------------------------
``await request.json()`` -> ``json.loads`` is synchronous and runs on the
single uvicorn event loop (workers=1), so one large JSON body stalls
every concurrent request. The only existing bound was the global
``max_body_size`` cap (~600 GB, sized for multipart file uploads), no
practical limit for a single JSON body. A separate, much smaller cap
(100 MB, matching ``web/routers/notes.py::_MAX_JSON_BODY_BYTES``) now
applies to non-multipart bodies while multipart (the two real upload
routes) keeps the large cap.

Both halves below use small custom caps (bytes, not real megabytes) via
the middleware's constructor params so the assertions don't allocate
real megabyte-scale buffers on a memory-constrained CI box — this
mirrors the existing style in ``tests/web/test_body_size_limit.py``.
"""

import os
import asyncio
import json
import uuid

import pytest
from fastapi.testclient import TestClient

from local_deep_research.web.fastapi_app import BodySizeLimitMiddleware

TEST_PASSWORD = "TestPassword123!"  # noqa: S105


# ---------------------------------------------------------------------------
# FIX 1 helpers — session cookie persistence (full app, TestClient)
# ---------------------------------------------------------------------------


def _fresh_client(app, base_url="http://testserver"):
    """TestClient with a unique X-Forwarded-For so each client gets its
    own slowapi rate-limit bucket (registration is rate limited per IP;
    the testclient peer is private, so X-Forwarded-For is honored)."""
    client = TestClient(app, base_url=base_url, raise_server_exceptions=False)
    fwd_ip = f"10.{uuid.uuid4().int % 254 + 1}.{uuid.uuid4().int % 254 + 1}.2"
    client.headers.update({"X-Forwarded-For": fwd_ip})
    return client


def _csrf(client):
    """Stamp the session with a CSRF token and return it."""
    client.get("/auth/login")
    resp = client.get("/auth/csrf-token")
    return resp.json().get("csrf_token", "") if resp.status_code == 200 else ""


def _register(client, username):
    return client.post(
        "/auth/register",
        data={
            "username": username,
            "password": TEST_PASSWORD,
            "confirm_password": TEST_PASSWORD,
            "acknowledge": "true",
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )


def _login(client, username, remember):
    return client.post(
        "/auth/login",
        data={
            "username": username,
            "password": TEST_PASSWORD,
            "remember": remember,
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )


def _session_set_cookies(resp):
    """All Set-Cookie header values for the `session` cookie."""
    return [
        v
        for k, v in resp.headers.multi_items()
        if k.lower() == "set-cookie" and v.lower().startswith("session=")
    ]


def _cookie_attr(cookie, name):
    """Return the value of a cookie attribute (case-insensitive), or the
    empty string for a value-less attribute, or None when absent."""
    for part in cookie.split(";")[1:]:
        attr, sep, value = part.strip().partition("=")
        if attr.lower() == name.lower():
            return value if sep else ""
    return None


@pytest.fixture
def registered_user(app):
    """Register a fresh user (unique per test) and hand back a client
    that is NOT yet logged in via /auth/login plus the username."""
    client = _fresh_client(app)
    username = f"remembme2_{uuid.uuid4().hex[:8]}"
    resp = _register(client, username)
    assert resp.status_code == 302, (
        f"registration failed: {resp.status_code} {resp.text[:300]}"
    )
    return client, username


# ---------------------------------------------------------------------------
# FIX 1 — anonymous visitor: no Max-Age
# ---------------------------------------------------------------------------


def test_anonymous_first_request_has_no_max_age(app):
    """A visitor who never logs in must get a browser-session cookie, not
    a persistent 30-day one.

    Before the fix: `remember_me is False` is False for the anonymous
    `None` value, so the stripping branch never runs and the cookie
    keeps SessionMiddleware's 30-day Max-Age.
    """
    client = _fresh_client(app)
    resp = client.get("/auth/login")
    assert resp.status_code == 200

    cookies = _session_set_cookies(resp)
    assert cookies, "GET /auth/login set no session cookie"
    cookie = cookies[0]
    assert _cookie_attr(cookie, "max-age") is None, (
        f"anonymous session cookie must not be persistent: {cookie}"
    )
    assert _cookie_attr(cookie, "expires") is None, cookie


# ---------------------------------------------------------------------------
# FIX 1 — post-logout: no Max-Age
# ---------------------------------------------------------------------------


def test_post_logout_response_has_no_max_age(registered_user):
    """Logging out must hand back a non-persistent cookie, even though the
    user had a persistent remember-me=True session moments earlier.

    Before the fix: `request.session.clear()` (routers/auth.py:946) wipes
    `_remember_me` entirely, so it reads back as None post-logout.
    `None is False` is False, so the stripping branch never runs and the
    logout response still carries the 30-day Max-Age.
    """
    client, username = registered_user
    login_resp = _login(client, username, remember="true")
    assert login_resp.status_code == 302, (
        f"login failed: {login_resp.status_code} {login_resp.text[:300]}"
    )
    # Sanity: this login really was persistent, so logout is a real
    # transition, not a no-op.
    login_cookie = _session_set_cookies(login_resp)[0]
    assert _cookie_attr(login_cookie, "max-age") is not None, (
        f"setup login was not persistent: {login_cookie}"
    )

    logout_resp = client.post(
        "/auth/logout",
        headers={"X-CSRFToken": _csrf(client)},
        follow_redirects=False,
    )
    assert logout_resp.status_code == 302, (
        f"logout failed: {logout_resp.status_code} {logout_resp.text[:300]}"
    )

    cookies = _session_set_cookies(logout_resp)
    assert cookies, "logout set no session cookie"
    cookie = cookies[0]
    assert _cookie_attr(cookie, "max-age") is None, (
        f"post-logout session cookie must not be persistent: {cookie}"
    )
    assert _cookie_attr(cookie, "expires") is None, cookie


# ---------------------------------------------------------------------------
# FIX 1 — regression guard: remember-me=True still gets a persistent cookie
# ---------------------------------------------------------------------------


def test_remember_me_true_still_has_max_age(registered_user):
    """The one case that MUST keep Max-Age: an explicit remember-me=True
    login. A broad "always strip" fix (e.g. unconditionally stripping,
    or checking `remember_me is not True` incorrectly) must not regress
    this — this is the fence against overcorrecting FIX 1."""
    client, username = registered_user
    resp = _login(client, username, remember="true")
    assert resp.status_code == 302, (
        f"login failed: {resp.status_code} {resp.text[:300]}"
    )

    cookies = _session_set_cookies(resp)
    assert cookies, "login set no session cookie"
    cookie = cookies[0]
    max_age = _cookie_attr(cookie, "max-age")
    assert max_age is not None, f"remember-me cookie not persistent: {cookie}"
    assert int(max_age) == 30 * 24 * 3600, cookie


# ---------------------------------------------------------------------------
# FIX 2 helpers — JSON body cap (direct ASGI middleware unit tests)
# ---------------------------------------------------------------------------


def _scope(path="/api/v1/research", headers=None):
    return {
        "type": "http",
        "method": "POST",
        "path": path,
        "headers": headers or [],
    }


class _App:
    """Records whether the wrapped app ran; drains the body like a route."""

    def __init__(self):
        self.ran = False

    async def __call__(self, scope, receive, send):
        self.ran = True
        more = True
        while more:
            message = await receive()
            more = message.get("more_body", False)
        await send(
            {
                "type": "http.response.start",
                "status": 200,
                "headers": [(b"content-type", b"application/json")],
            }
        )
        await send({"type": "http.response.body", "body": b"{}"})


def _run(middleware, scope, body_chunks):
    """Drive the ASGI app; returns (status, body)."""
    sent = []
    chunks = list(body_chunks)

    async def receive():
        more = len(chunks) > 1
        return {
            "type": "http.request",
            "body": chunks.pop(0) if chunks else b"",
            "more_body": more,
        }

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

    asyncio.run(middleware(scope, receive, send))

    status = next(
        m["status"] for m in sent if m["type"] == "http.response.start"
    )
    body = b"".join(
        m.get("body", b"") for m in sent if m["type"] == "http.response.body"
    )
    return status, body


# ---------------------------------------------------------------------------
# FIX 2 — over-cap JSON body -> 413, under-cap -> passes
# ---------------------------------------------------------------------------


def test_over_cap_json_body_rejected_with_413():
    """A JSON body declaring a Content-Length above the (small, custom)
    JSON cap is rejected before the app runs — same 413 shape the
    middleware already produces for the general cap."""
    app = _App()
    mw = BodySizeLimitMiddleware(
        app, max_body_size=10_000, max_json_body_size=100
    )
    scope = _scope(
        headers=[
            (b"content-type", b"application/json"),
            (b"content-length", b"101"),
        ]
    )

    status, body = _run(mw, scope, [b""])

    assert status == 413
    assert json.loads(body) == {"error": "Request too large"}
    assert app.ran is False


def test_under_cap_json_body_passes():
    app = _App()
    mw = BodySizeLimitMiddleware(
        app, max_body_size=10_000, max_json_body_size=100
    )
    scope = _scope(
        headers=[
            (b"content-type", b"application/json; charset=utf-8"),
            (b"content-length", b"100"),
        ]
    )

    status, _ = _run(mw, scope, [b"x" * 100])

    assert status == 200
    assert app.ran is True


def test_chunked_json_body_over_cap_is_rejected_via_streaming_count():
    """No Content-Length (chunked transfer) must not bypass the JSON cap
    — the middleware already counts bytes mid-stream for this case; the
    JSON cap must reuse that same counting path."""
    app = _App()
    mw = BodySizeLimitMiddleware(
        app, max_body_size=10_000, max_json_body_size=100
    )
    scope = _scope(headers=[(b"content-type", b"application/json")])

    status, body = _run(mw, scope, [b"x" * 60, b"x" * 60])

    assert status == 413
    assert json.loads(body) == {"error": "Request too large"}


def test_missing_content_type_gets_the_small_cap_too():
    """A route can call `await request.json()` regardless of what
    Content-Type the client sent (Starlette's `.json()` doesn't check
    it) — gating the small cap strictly on `application/json` would
    leave a missing/spoofed Content-Type as a bypass. A body with no
    Content-Type at all must still get the small JSON-tier cap, not the
    large multipart-sized one."""
    app = _App()
    mw = BodySizeLimitMiddleware(
        app, max_body_size=10_000, max_json_body_size=100
    )
    scope = _scope(headers=[(b"content-length", b"101")])

    status, _ = _run(mw, scope, [b""])

    assert status == 413


# ---------------------------------------------------------------------------
# FIX 2 — multipart upload well over the JSON cap still works
# ---------------------------------------------------------------------------


def test_multipart_upload_over_json_cap_still_passes():
    """The two real upload routes are multipart, not JSON — they must
    keep enforcing only the large `max_body_size` cap, not the small
    JSON-tier one."""
    app = _App()
    mw = BodySizeLimitMiddleware(
        app, max_body_size=10_000, max_json_body_size=100
    )
    scope = _scope(
        path="/library/api/collections/abc/upload",
        headers=[
            (
                b"content-type",
                b"multipart/form-data; boundary=----WebKitFormBoundary",
            ),
            (b"content-length", b"5000"),
        ],
    )

    status, _ = _run(mw, scope, [b"x" * 5000])

    assert status == 200
    assert app.ran is True


def test_multipart_still_capped_by_the_large_limit():
    """Multipart is exempt from the small JSON cap, not uncapped — the
    large `max_body_size` cap still applies to it."""
    app = _App()
    mw = BodySizeLimitMiddleware(
        app, max_body_size=1_000, max_json_body_size=100
    )
    scope = _scope(
        headers=[
            (b"content-type", b"multipart/form-data; boundary=x"),
            (b"content-length", b"1001"),
        ]
    )

    status, body = _run(mw, scope, [b""])

    assert status == 413
    assert json.loads(body) == {"error": "Request too large"}


# ---------------------------------------------------------------------------
# FIX 2 — an explicit small max_body_size still wins (backward compat)
# ---------------------------------------------------------------------------


def test_explicit_small_max_body_size_is_not_widened_by_the_json_default():
    """A caller passing a small `max_body_size` (as tests/web/
    test_body_size_limit.py does) and relying on the *default*
    max_json_body_size must not have its cap silently widened to 100 MB
    for non-multipart traffic — `min(max_body_size, max_json_body_size)`
    must pick the smaller of the two."""
    app = _App()
    mw = BodySizeLimitMiddleware(app, max_body_size=100)  # default JSON cap
    scope = _scope(headers=[(b"content-length", b"101")])

    status, _ = _run(mw, scope, [b""])

    assert status == 413


def test_default_json_caps_are_16mb_ordinary_and_100mb_for_notes():
    """Pins both caps.

    100 MB (what notes needs: 2x the 50 MB NOTE_CONTENT_MAX_BYTES) is a weak
    mitigation as a GLOBAL cap, because the whole point is that `json.loads`
    stalls the single event loop in proportion to body size: measured on this
    branch, 104 MB -> ~637 ms, 34 MB -> ~128 ms, 8 MB -> ~38 ms. So ordinary
    routes get 16 MB (~60 ms worst case, ~4x headroom over the largest
    realistic legitimate body, ~4 MB) and only the notes prefixes keep 100 MB.
    """
    mw = BodySizeLimitMiddleware(_App())
    assert mw.max_json_body_size == 16 * 1024 * 1024
    assert mw.max_large_json_body_size == 100 * 1024 * 1024


def test_ordinary_json_route_gets_the_small_cap():
    app = _App()
    mw = BodySizeLimitMiddleware(
        app, max_body_size=10_000, max_json_body_size=100
    )
    scope = _scope(
        path="/library/api/documents/bulk",
        headers=[
            (b"content-type", b"application/json"),
            (b"content-length", b"101"),
        ],
    )

    status, _ = _run(mw, scope, [b""])

    assert status == 413


def test_notes_route_keeps_the_large_cap():
    """The exemption must actually apply, or every notes route over the small
    cap breaks. All 40 registered notes routes sit under `/notes/`."""
    app = _App()
    mw = BodySizeLimitMiddleware(
        app,
        max_body_size=10_000,
        max_json_body_size=100,
        max_large_json_body_size=5_000,
    )
    scope = _scope(
        path="/notes/api/notes",
        headers=[
            (b"content-type", b"application/json"),
            (b"content-length", b"101"),
        ],
    )

    status, _ = _run(mw, scope, [b"x" * 101])

    assert status == 200, "notes was wrongly capped by the ordinary JSON limit"


def test_notes_exemption_is_still_bounded():
    """Exempt does not mean unbounded — notes is still held to the large cap."""
    app = _App()
    mw = BodySizeLimitMiddleware(
        app,
        max_body_size=10_000,
        max_json_body_size=100,
        max_large_json_body_size=5_000,
    )
    scope = _scope(
        path="/notes/api/notes",
        headers=[
            (b"content-type", b"application/json"),
            (b"content-length", b"5001"),
        ],
    )

    status, _ = _run(mw, scope, [b""])

    assert status == 413


def test_large_cap_can_never_be_smaller_than_the_ordinary_cap():
    """Guards a misconfiguration that would otherwise make an "exempt" route
    STRICTER than a normal one."""
    mw = BodySizeLimitMiddleware(
        _App(), max_json_body_size=1000, max_large_json_body_size=10
    )
    assert mw.max_large_json_body_size == 1000


# ---------------------------------------------------------------------------
# Threadpool sizing vs per-user DB pool capacity (BC-4 hardening)
# ---------------------------------------------------------------------------


def test_threadpool_warning_fires_above_pool_capacity():
    """Above capacity a single user's concurrency can exhaust their own pool.

    Extracted from the lifespan hook specifically so it is testable: the
    limiter it reads (`anyio.to_thread.current_default_thread_limiter()`)
    raises outside a running async context, so the check could not otherwise
    be exercised without standing up a full app startup.
    """
    from local_deep_research.database.pool_config import (
        MAX_OVERFLOW,
        POOL_SIZE,
    )
    from local_deep_research.web.fastapi_app import (
        warn_if_threadpool_exceeds_db_pool,
    )

    capacity = POOL_SIZE + MAX_OVERFLOW
    assert warn_if_threadpool_exceeds_db_pool(capacity + 1) is True
    assert warn_if_threadpool_exceeds_db_pool(capacity) is False
    assert warn_if_threadpool_exceeds_db_pool(40) is False, (
        "AnyIO's default must not warn — this changes nothing by default"
    )


def test_threadpool_warning_uses_the_real_pool_constants():
    """Pins the single source of truth: the warning must track the values the
    engine is actually built with, not a copy that can drift."""
    from local_deep_research.database.encrypted_db import db_manager
    from local_deep_research.database.pool_config import (
        MAX_OVERFLOW,
        POOL_SIZE,
    )

    db_manager._use_static_pool = False
    try:
        kwargs = db_manager._get_pool_kwargs()
    finally:
        db_manager._use_static_pool = bool(os.environ.get("TESTING"))

    assert kwargs["pool_size"] == POOL_SIZE
    assert kwargs["max_overflow"] == MAX_OVERFLOW