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.1 KB
Raw Blame
"""Regression test for six polling endpoints' `@limiter.exempt` decorator,
lost in the Flask -> FastAPI migration and restored here.

On main (Flask-Limiter), these were all decorated ``@limiter.exempt``:

  - research_routes.py:2257   GET /api/research/<id>/status
  - history_routes.py:149     GET /status/<id>            (-> /history/status/<id>)
  - benchmark_routes.py:284   GET /api/status/<id>         (-> /benchmark/api/status/<id>)
  - benchmark_routes.py:486   GET /api/results/<id>        (-> /benchmark/api/results/<id>)
  - benchmark_routes.py:1020  GET /api/search-quality      (-> /benchmark/api/search-quality)
  - rag_routes.py:3483        GET /api/collections/<id>/index/status
                                   (-> /library/api/collections/<id>/index/status)

These are exactly the endpoints a browser polls while a research run,
benchmark, or RAG index job is in progress. The migration to FastAPI +
slowapi (``web/dependencies/rate_limit.py`` / ``fastapi_app.py``)
dropped the decorator on all six, so they silently fell under
SlowAPIMiddleware's global default limit (``DEFAULT_RATE_LIMIT``,
5000/hour;50000/day, enforced per-route-per-IP). A NAT'd/multi-user
deployment or several concurrent tabs sharing one IP could exhaust that
bucket polling a single in-progress job, and the live progress UI would
start getting 429'd.

Restored using the SAME idiom already used elsewhere on this branch for
the surviving exemptions (``fastapi_app.py``'s ``/favicon.ico`` and
``/static/{path}``): ``@limiter.exempt`` placed directly above the
route function, below the ``@router.get(...)`` decorator.

This test drives the default limit down to a handful of requests per
window, hits each of the six restored paths well past that count, and
asserts none of them ever 429 -- with a known NON-exempt GET route
(``/history/api``) as a control that DOES 429 under the same lowered
default, so the test can't pass vacuously (e.g. if the fixture failed
to actually lower anything).

Mechanics, and why this mutates the live ``limiter`` singleton instead
of setting ``LDR_SECURITY_RATE_LIMIT_DEFAULT`` + reloading the module:
SlowAPIMiddleware (installed by ``fastapi_app._setup_rate_limiting``)
reads limits off ``app.state.limiter``, which IS
``web.dependencies.rate_limit.limiter`` -- the same object imported
below. Its default-limit list (``_default_limits``) and ``.enabled``
flag are read fresh on every request (see slowapi's
``SlowAPIMiddleware.dispatch`` / ``Limiter._check_request_limit``), so
flipping them on the live object -- then restoring -- changes real
request handling with no reload required. A reload IS NOT an option:
every router is decorated against this exact object at import time
(``@limiter.exempt``, ``@limiter.limit(...)``), so
``importlib.reload(rate_limit)`` would rebind the module's ``limiter``
name to a fresh, empty-registry ``Limiter`` while every router (and
``app.state.limiter``) kept using the original -- the identical gotcha
is documented at length in
``tests/web/dependencies/test_rate_limit_startup_validation_gap.py``.
"""

import uuid

import pytest
from fastapi.testclient import TestClient
from slowapi.wrappers import LimitGroup

from local_deep_research.web.dependencies.rate_limit import limiter

LOWERED_DEFAULT = "5 per 15 seconds"
ATTEMPTS = 9  # comfortably past a 5-request window

# (id, path) for the six routes restored in this change. Dummy path
# params are just strings that satisfy ROUTING -- SlowAPIMiddleware
# only needs a route match (it runs before auth/DB dependencies), not a
# real resource, so any value shaped like the path segment works.
EXEMPT_ROUTES = [
    ("research_status", "/api/research/999/status"),
    ("history_status", "/history/status/999"),
    ("benchmark_status", "/benchmark/api/status/999"),
    ("benchmark_results", "/benchmark/api/results/999"),
    ("benchmark_search_quality", "/benchmark/api/search-quality"),
    ("rag_index_status", "/library/api/collections/999/index/status"),
]

# A GET route living in one of the same router files, deliberately left
# WITHOUT @limiter.exempt, used to prove the lowered default is really
# being enforced.
CONTROL_NAME, CONTROL_PATH = "history_list", "/history/api"


def _unique_ip() -> str:
    """A private (trusted-range) IP nobody else's bucket uses."""
    parts = [uuid.uuid4().int % 254 + 1 for _ in range(3)]
    return f"10.{parts[0]}.{parts[1]}.{parts[2]}"


@pytest.fixture
def lowered_default_rate_limit():
    """Shrink the live limiter's default bucket to ``LOWERED_DEFAULT``
    for the duration of one test, then restore it exactly.

    Also forces ``limiter.enabled = True`` (CI runs with
    ``LDR_DISABLE_RATE_LIMITING=true``), matching the pattern already
    used by ``tests/web/routers/test_auth_rate_limits.py``'s
    ``rate_limiting_enforced`` fixture.
    """
    original_limits = limiter._default_limits
    original_enabled = limiter.enabled

    limiter._default_limits = [
        LimitGroup(
            LOWERED_DEFAULT,
            limiter._key_func,
            None,
            False,
            None,
            None,
            None,
            1,
            False,
        )
    ]
    limiter.enabled = True
    try:
        yield
    finally:
        limiter._default_limits = original_limits
        limiter.enabled = original_enabled
        try:
            limiter.reset()
        except Exception:
            pass


@pytest.mark.parametrize(
    "name,path", EXEMPT_ROUTES, ids=[n for n, _ in EXEMPT_ROUTES]
)
def test_polling_endpoint_exempt_from_default_rate_limit(
    app, lowered_default_rate_limit, name, path
):
    client = TestClient(app, raise_server_exceptions=False)
    client.headers.update({"X-Forwarded-For": _unique_ip()})

    # follow_redirects=False: an unauthenticated request to some of these
    # routes 302s to /auth/login, and TestClient follows redirects by
    # default -- which would then run the *login page's* GET through the
    # same lowered default and report ITS eventual 429 as if it were
    # this route's, a false failure unrelated to this route's exemption.
    # We only care about the status this route itself returns.
    statuses = [
        client.get(path, follow_redirects=False).status_code
        for _ in range(ATTEMPTS)
    ]

    assert 429 not in statuses, (
        f"{name} ({path}) returned 429 within {ATTEMPTS} requests even "
        f"though it is decorated @limiter.exempt; statuses={statuses}"
    )


def test_control_route_is_rate_limited_by_lowered_default(
    app, lowered_default_rate_limit
):
    """Sanity check: a route with NO ``@limiter.exempt`` DOES 429 under
    the same lowered default -- proves the fixture actually enforces
    something, so the exempt-route test above isn't vacuously true."""
    client = TestClient(app, raise_server_exceptions=False)
    client.headers.update({"X-Forwarded-For": _unique_ip()})

    statuses = [
        client.get(CONTROL_PATH, follow_redirects=False).status_code
        for _ in range(ATTEMPTS)
    ]

    assert 429 in statuses, (
        f"control route {CONTROL_NAME} ({CONTROL_PATH}) never 429'd "
        f"within {ATTEMPTS} requests under a {LOWERED_DEFAULT!r} default "
        f"-- the test fixture is not actually enforcing the lowered "
        f"limit; statuses={statuses}"
    )