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.

21.2 KB
Raw Blame
"""Regression evidence for history bounds and benchmark path typing.

ADR-0010 records the historical migration measurement; this file is the
committed behavior-level evidence for these areas.

HISTORY LOG BOUNDS (``web/routers/history.py``).
    ``GET /history/logs/{research_id}`` clamps ``?limit=`` to
    ``[1, HISTORY_LOGS_HARD_CAP]`` and falls back to
    ``HISTORY_LOGS_DEFAULT_LIMIT`` when no ``limit`` is given at all.
    Grepping ``HISTORY_LOGS_HARD_CAP`` / ``HISTORY_LOGS_DEFAULT_LIMIT``
    across ``tests/`` returned zero hits before this file. The route's own
    docstring: pre-cap, this endpoint "allocated ~150 MB transient on the
    server and Firefox parsed a ~50 MB JSON response" for one long-running
    research.

    VACUITY TRAP: with only a handful of log rows seeded, "the response has
    at most N items" passes just as well whether the clamp exists or not --
    there simply aren't enough rows for an unbounded read to look any
    different from a clamped one. So every assertion here binds an
    ``Engine``-level ``before_cursor_execute`` listener and asserts the
    LIMIT parameter that actually reached SQLite, not the response length.
    (Response length is used only as a secondary corroborating check on the
    positive-control case, where it is non-vacuous because the seed count
    is deliberately smaller than the requested limit.)

    The per-user encrypted DB session is thread-local
    (``database/thread_local_session.py``), and Starlette dispatches sync
    path functions to a worker thread the test does not control, so the
    listener cannot be bound to "the" engine the way a same-thread fixture
    could. It is bound to the ``Engine`` *class* instead
    (``sqlalchemy.event.listen(Engine, ...)``), which fires for every
    engine instance -- including the one the request handler's own thread
    creates -- and is removed at the end of each test.

BENCHMARK PATH-PARAMETER TYPING (``web/routers/benchmark.py``).
    All five ``{benchmark_run_id}`` routes declare ``benchmark_run_id: int``
    and the review snapshot had no direct assertion for FastAPI's own
    path-conversion validation. The tests below now pin it. Honest severity,
    stated in the audit and repeated
    here so nobody mistakes this for more than it is: a regression here
    (e.g. the annotation silently dropped or widened to ``str``) turns a
    non-numeric segment into a 500 inside the handler instead of a 422
    from FastAPI's request-parsing layer, before the handler ever runs.
    It is NOT a SQL-injection vector -- every one of these routes reaches
    the database exclusively through SQLAlchemy ORM equality filters
    (``BenchmarkRun.id == benchmark_run_id`` and friends), which are
    parameterised regardless of what Python type flows in. The tests below
    assert the 422/type-mismatch behaviour, and deliberately do NOT probe
    for injection -- there being none to find on the parameterised path.

PAGINATION BOUNDS -- already covered, not duplicated here.
    The review also considered ``tests/web/routers/test_history_pagination_
    params.py::test_out_of_range_pagination_is_clamped_not_rejected``, whose
    own docstring names the ``LIMIT -1`` unbounded-load hazard but only
    asserts ``status_code < 500`` -- never the clamped value. That assertion
    is incomplete in *that* file, but a separate, stronger successor already
    exists and is live on this branch: ``tests/security/
    test_pagination_bounds.py`` (``TestHistoryRoutesPagination``) drives
    ``GET /history/api`` with a real seeded DB and a ``before_cursor_
    execute`` listener, and asserts the bound LIMIT/OFFSET parameters
    directly for all four clamp arms (oversized limit, in-range limit,
    zero/negative limit, negative offset) plus positive controls for both
    limit and offset. It uses exactly the bind-parameter technique this
    file uses for history-log bounds. Verified as non-vacuous below (see
    ``test_row_13_is_covered_by_an_existing_bind_parameter_test`` and its
    docstring) rather than duplicated here, per the assignment's own
    instruction not to re-test an already-covered guard.
"""

import itertools
import os
import uuid

import pytest
from fastapi.testclient import TestClient
from sqlalchemy import event
from sqlalchemy.engine import Engine

os.environ.setdefault("TESTING", "1")

from local_deep_research.constants import (
    HISTORY_LOGS_DEFAULT_LIMIT,
    HISTORY_LOGS_HARD_CAP,
)
from local_deep_research.web.routers.history import router as history_router
from local_deep_research.web.routers.benchmark import (
    router as benchmark_router,
)

_HISTORY_PATHS = {r.path for r in history_router.routes if hasattr(r, "path")}
_BENCHMARK_PATHS = {
    r.path for r in benchmark_router.routes if hasattr(r, "path")
}

PASSWORD = "HistBenchLimits!Pass123"  # noqa: S105 -- test-only credential

# ---------------------------------------------------------------------------
# Harness (mirrors tests/security/test_metrics_hostile_input_fastapi.py)
# ---------------------------------------------------------------------------

# MONOTONIC, not random -- rate limiting is keyed per client IP
# (dependencies/rate_limit._get_client_ip trusts X-Forwarded-For from the
# TestClient sentinel peer). Random octets collide inside a session this
# size and produce unrelated 429s.
_IP_COUNTER = itertools.count(1)


def _next_forwarded_for() -> str:
    n = next(_IP_COUNTER)
    return f"10.214.{(n // 250) % 250}.{(n % 250) + 1}"


def _fresh_client(app):
    client = TestClient(app, raise_server_exceptions=False)
    client.headers.update({"X-Forwarded-For": _next_forwarded_for()})
    return client


def _csrf(client) -> str:
    """CSRF is enforced by ASGI middleware -- fetch a real token."""
    client.get("/auth/login")
    return client.get("/auth/csrf-token").json()["csrf_token"]


def _register_and_login(app, username: str):
    client = _fresh_client(app)

    resp = client.post(
        "/auth/register",
        data={
            "username": username,
            "password": PASSWORD,
            "confirm_password": PASSWORD,
            "acknowledge": "true",
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )
    assert resp.status_code in (200, 302), (
        f"registration of {username!r} failed: "
        f"{resp.status_code} / {resp.text[:400]}"
    )

    resp = client.post(
        "/auth/login",
        data={
            "username": username,
            "password": PASSWORD,
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )
    assert resp.status_code in (200, 302), (
        f"login of {username!r} failed: {resp.status_code} / {resp.text[:400]}"
    )

    client.headers.update(
        {"X-CSRFToken": client.get("/auth/csrf-token").json()["csrf_token"]}
    )

    whoami = client.get("/auth/check")
    assert whoami.status_code == 200 and whoami.json().get("username") == (
        username
    ), f"session did not bind to {username!r}: {whoami.text[:300]}"
    return client


def _user(prefix: str) -> str:
    return f"{prefix}_{uuid.uuid4().hex[:10]}"


def test_routes_under_test_still_exist():
    """If any of these move, every case below would pass against a 404."""
    assert "/history/logs/{research_id}" in _HISTORY_PATHS
    assert "/history/api" in _HISTORY_PATHS
    assert "/benchmark/api/status/{benchmark_run_id}" in _BENCHMARK_PATHS
    assert "/benchmark/api/cancel/{benchmark_run_id}" in _BENCHMARK_PATHS
    assert "/benchmark/api/results/{benchmark_run_id}" in _BENCHMARK_PATHS
    assert (
        "/benchmark/api/results/{benchmark_run_id}/export" in _BENCHMARK_PATHS
    )
    assert "/benchmark/api/delete/{benchmark_run_id}" in _BENCHMARK_PATHS


def test_the_two_constants_have_not_silently_drifted():
    """Premise guard for every literal below.

    If either constant changes, the tests here should be updated
    deliberately rather than silently passing against a stale literal.
    """
    assert HISTORY_LOGS_DEFAULT_LIMIT == 500
    assert HISTORY_LOGS_HARD_CAP == 5000


# ===========================================================================
# ROW 13 -- confirm the existing successor is not itself vacuous, then skip
# ===========================================================================


def test_row_13_is_covered_by_an_existing_bind_parameter_test():
    """Not a duplicate test -- a structural check that the citation holds.

    Asserts (a) the file and technique the docstring above cites actually
    exist and (b) it really does inspect the bound query parameter rather
    than the response shape, so a future reader trusts the "already
    covered, skipped" claim instead of having to go re-derive it.
    """
    import inspect

    from tests.security import test_pagination_bounds as row13

    assert hasattr(row13, "_limit_offset_params"), (
        "the bind-parameter helper this file's docstring cites is gone -- "
        "row 13 needs a real test again, not a skip"
    )
    source = inspect.getsource(row13)
    assert "before_cursor_execute" in source
    assert "HISTORY_MAX_LIMIT" in source
    # The exact vacuity trap named in the audit: SQLite treats a negative
    # OFFSET as 0 on its own, so the successor must assert the bound
    # parameter for that case specifically, not row counts.
    assert "offset == 0" in source


# ===========================================================================
# ROW 12 -- HISTORY_LOGS_HARD_CAP / HISTORY_LOGS_DEFAULT_LIMIT
# ===========================================================================


def _seed_research_with_logs(username: str, research_id: str, count: int):
    """Seed one research row plus ``count`` log rows into the caller's own
    encrypted DB, in insertion order (log 0, log 1, ...)."""
    from local_deep_research.database.models import ResearchHistory
    from local_deep_research.database.models.logs import ResearchLog
    from local_deep_research.database.session_context import (
        get_user_db_session,
    )

    with get_user_db_session(username, PASSWORD) as session:
        session.add(
            ResearchHistory(
                id=research_id,
                query="history logs limit guard",
                mode="quick_summary",
                status="completed",
                created_at="2024-01-01T00:00:00",
            )
        )
        session.flush()
        for i in range(count):
            session.add(
                ResearchLog(
                    research_id=research_id,
                    message=f"log {i}",
                    module="test",
                    function="test_fn",
                    line_no=i,
                    level="INFO",
                )
            )
        session.commit()


@pytest.fixture
def history_logs_user(app):
    """One logged-in user with 8 log rows on one research entry.

    8 is deliberately far below HISTORY_LOGS_HARD_CAP (5000): the guard
    tests below never rely on row *count* to prove the clamp, only on the
    bound query parameter, so there is no need to actually seed thousands
    of rows to exercise the cap.
    """
    username = _user("histlogs")
    client = _register_and_login(app, username)
    research_id = str(uuid.uuid4())
    _seed_research_with_logs(username, research_id, count=8)
    return {"client": client, "research_id": research_id}


class _CaptureAppLogsLimit:
    """Binds a class-level ``before_cursor_execute`` listener on ``Engine``.

    The per-user encrypted DB session lives on whichever worker thread
    Starlette dispatches the sync route handler to, which the test does
    not control and which is not the thread the fixture used to seed data
    on. Binding to ``Engine`` (the class, not an instance) is the
    documented SQLAlchemy pattern for "every engine, including ones not
    created yet" and is what makes capturing the real request's bound
    parameters possible at all here.
    """

    def __enter__(self):
        self.executed: list[tuple[str, tuple]] = []

        def _listener(
            conn, cursor, statement, parameters, context, executemany
        ):
            self.executed.append((statement, parameters))

        self._listener = _listener
        event.listen(Engine, "before_cursor_execute", _listener)
        return self

    def __exit__(self, *exc):
        event.remove(Engine, "before_cursor_execute", self._listener)

    def bound_limit(self) -> int:
        """The LIMIT parameter bound into the paged ``app_logs`` SELECT.

        Verified directly (see the module docstring's exploration): the
        compiled statement is
        ``... WHERE app_logs.research_id = ? ORDER BY app_logs.timestamp
        DESC, app_logs.id DESC LIMIT ? OFFSET ?`` -- LIMIT is always the
        second-to-last bound parameter, OFFSET (always 0 here; the route
        never sets one) the last.
        """
        for statement, parameters in self.executed:
            if "app_logs" in statement and "LIMIT" in statement.upper():
                return tuple(parameters)[-2]
        raise AssertionError(
            "no paged SELECT over app_logs was executed; the test never "
            "reached the query it is about. Statements: "
            + repr([s.split(chr(10))[0][:80] for s, _ in self.executed])
        )


def test_default_limit_is_used_when_no_limit_param_is_given(
    history_logs_user,
):
    """No ``?limit=`` at all must still reach the query as
    HISTORY_LOGS_DEFAULT_LIMIT, not as "no bound at all"."""
    research_id = history_logs_user["research_id"]
    with _CaptureAppLogsLimit() as cap:
        resp = history_logs_user["client"].get(f"/history/logs/{research_id}")

    assert resp.status_code == 200, resp.text[:300]
    assert cap.bound_limit() == HISTORY_LOGS_DEFAULT_LIMIT, (
        "GET /history/logs/<id> with no ?limit= did not bind "
        "HISTORY_LOGS_DEFAULT_LIMIT into the query"
    )


def test_oversized_limit_is_clamped_to_the_hard_cap(history_logs_user):
    """A caller cannot exceed HISTORY_LOGS_HARD_CAP.

    Asserted on the bound parameter, not response length: only 8 rows are
    seeded, so "at most N rows came back" would hold just as well with the
    cap deleted entirely -- the classic vacuity trap named in the audit.
    """
    research_id = history_logs_user["research_id"]
    with _CaptureAppLogsLimit() as cap:
        resp = history_logs_user["client"].get(
            f"/history/logs/{research_id}", params={"limit": 999999999}
        )

    assert resp.status_code == 200, resp.text[:300]
    assert cap.bound_limit() == HISTORY_LOGS_HARD_CAP, (
        f"?limit=999999999 bound {cap.bound_limit()} rows into the query; "
        f"the HISTORY_LOGS_HARD_CAP ceiling is gone"
    )


@pytest.mark.parametrize("raw_limit", ["0", "-1", "-999999"])
def test_limit_is_floored_to_one(history_logs_user, raw_limit):
    research_id = history_logs_user["research_id"]
    with _CaptureAppLogsLimit() as cap:
        resp = history_logs_user["client"].get(
            f"/history/logs/{research_id}", params={"limit": raw_limit}
        )

    assert resp.status_code == 200, resp.text[:300]
    assert cap.bound_limit() == 1, (
        f"?limit={raw_limit} bound {cap.bound_limit()} into the query; "
        f"the max(1, ...) floor is gone"
    )


# POSITIVE CONTROL. Without this, a handler that ignored ?limit= entirely
# and always used the default/hard-cap would still pass every test above.
def test_in_range_limit_is_honoured_exactly(history_logs_user):
    """An explicit in-range ?limit= must reach the query unmodified, and
    the response must reflect exactly that many of the most-recent rows.

    8 rows are seeded and 3 are requested, so -- unlike the hard-cap case
    above -- the row count here IS informative: it is smaller than the
    seed count, so a broken clamp that returned everything would fail it
    too. Both signals (bound parameter and response content) are checked.
    """
    research_id = history_logs_user["research_id"]
    with _CaptureAppLogsLimit() as cap:
        resp = history_logs_user["client"].get(
            f"/history/logs/{research_id}", params={"limit": 3}
        )

    assert resp.status_code == 200, resp.text[:300]
    assert cap.bound_limit() == 3
    logs = resp.json()["logs"]
    assert len(logs) == 3
    # get_logs_for_research takes the newest `limit` rows then reverses to
    # oldest-first -- of 8 rows (log 0..log 7), the newest 3 are 5, 6, 7.
    assert [entry["message"] for entry in logs] == ["log 5", "log 6", "log 7"]


def test_nonnumeric_limit_falls_back_to_the_default_not_a_500(
    history_logs_user,
):
    """Defensive parsing sits right next to the two guards above; a
    non-numeric ?limit= must not 500, and must fall back to the same
    default as "no limit given" -- not to 0 or an unbounded read."""
    research_id = history_logs_user["research_id"]
    with _CaptureAppLogsLimit() as cap:
        resp = history_logs_user["client"].get(
            f"/history/logs/{research_id}", params={"limit": "not-a-number"}
        )

    assert resp.status_code == 200, resp.text[:300]
    assert cap.bound_limit() == HISTORY_LOGS_DEFAULT_LIMIT


# ===========================================================================
# ROW 36 -- benchmark_run_id: int path-param typing
# ===========================================================================

# (method, path template) for all five parameterised benchmark routes.
_BENCHMARK_ID_ROUTES = [
    ("GET", "/benchmark/api/status/{}"),
    ("POST", "/benchmark/api/cancel/{}"),
    ("GET", "/benchmark/api/results/{}"),
    ("GET", "/benchmark/api/results/{}/export"),
    ("DELETE", "/benchmark/api/delete/{}"),
]

# Payloads that are not valid integers. No SQL-injection-shaped payloads
# here on purpose (see module docstring: these queries are parameterised,
# so that is not the hazard this row is about) -- these are chosen purely
# to exercise FastAPI's int path-conversion.
NON_INTEGER_SEGMENTS = [
    "abc",
    "1.5",
    # NB: "1.0" is deliberately excluded -- pydantic's lax int coercion
    # accepts a decimal string that represents a whole number (float("1.0")
    # == 1), so the route legitimately treats it as benchmark_run_id=1 and
    # this is not a case FastAPI's own validation is expected to reject.
    "1e5",
    "NaN",
    "null",
    "true",
    "0x10",
    "1,2",
    "1abc",
]


@pytest.fixture
def benchmark_client(app):
    username = _user("benchid")
    return _register_and_login(app, username)


# NB on structure: the sweep below loops inside a single test rather than
# using a full @parametrize cross-product. Each case needs an authenticated
# caller (a fresh `app` + register/login per PBKDF2-hashed user), and 5
# routes x 10 payloads as separate parametrized tests turned this file into
# a multi-minute run for no extra signal -- same tradeoff documented in
# tests/security/test_metrics_hostile_input_fastapi.py. The loop carries the
# exact case in every assertion message, so a failure still names it.
def test_non_integer_path_segment_is_rejected_with_422(benchmark_client):
    """A non-integer ``benchmark_run_id`` must be rejected by FastAPI's own
    path-conversion validation (422) before the handler runs -- never a
    500 from inside the handler, and never silently accepted.

    NOT a SQL-injection test: the routes' queries are SQLAlchemy ORM
    equality filters (parameterised regardless of the Python value), so
    the only thing a typing regression changes is 500-vs-422. See the
    module docstring.
    """
    for method, template in _BENCHMARK_ID_ROUTES:
        for segment in NON_INTEGER_SEGMENTS:
            path = template.format(segment)
            case = f"{method} {path}"
            resp = benchmark_client.request(method, path)
            assert resp.status_code == 422, (
                f"{case} returned {resp.status_code} instead of 422 "
                f"(benchmark_run_id: int no longer rejects non-integer "
                f"input): {resp.text[:300]}"
            )
            detail = resp.json().get("detail")
            assert isinstance(detail, list) and detail, (case, resp.text[:300])
            assert any(
                err.get("loc") == ["path", "benchmark_run_id"]
                and err.get("type") == "int_parsing"
                for err in detail
            ), (
                f"{case}: 422 was not FastAPI's int-path-conversion error: {detail}"
            )


# POSITIVE CONTROL. Without this, a handler that rejected everything (a
# broken auth dependency, a route that moved, ...) would satisfy every 422
# assertion above for the wrong reason.
def test_valid_integer_path_segment_reaches_the_handler(benchmark_client):
    """A syntactically valid (if nonexistent) integer id must NOT be
    rejected at the path-validation layer -- it has to reach the handler,
    which then 404s / no-ops on the missing row."""
    for method, template in _BENCHMARK_ID_ROUTES:
        path = template.format(999999999)
        case = f"{method} {path}"
        resp = benchmark_client.request(method, path)
        assert resp.status_code != 422, (
            f"{case}: a valid integer id was rejected at path validation: "
            f"{resp.status_code} {resp.text[:300]}"
        )
        # Every route in this set replies with a JSON body either way; a
        # 422 is the only "never happens on a valid int" case, so this is
        # mostly a sanity check that the request landed somewhere sane.
        assert resp.status_code < 500 or resp.json().get("success") is False, (
            f"{case}: {resp.status_code} {resp.text[:300]}"
        )