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.5 KB
Raw Blame
"""Behavioral rate-limit tests for the auth routes under slowapi.

Restores the coverage lost with main's Flask-era
``tests/auth_tests/test_auth_rate_limiting.py`` against the FastAPI
branch's setup (``web/dependencies/rate_limit.py`` + ``routers/auth.py``):

- the registration bucket returns 429 after its threshold (and the 429
  body has main's ``{"error", "message"}`` shape through the real stack);
- ``/auth/validate-password`` (the strength check the register /
  change-password forms call on every keystroke) has its OWN bucket —
  typing a password must not burn the login quota, and an exhausted
  login bucket must not block the strength check;
- ``/auth/change-password`` has its own bucket, independent of login in
  both directions, and 429s after its threshold;
- buckets are keyed per client IP: exhausting IP A leaves IP B usable
  and IP A stays blocked;
- ``/auth/integrity-check`` carries its own registered limit (introspected
  from ``limiter._route_limits``, mirroring
  ``tests/web/routers/test_notes_rate_limit_keys.py``, rather than driven
  behaviorally like the buckets above — it needs real SQLCipher/fallback
  setup to hit 200, which the plain 400-validation trick the other buckets
  use doesn't have an equivalent for).

Enforcement notes:
- ``limiter.enabled`` is resolved from env at import time (CI disables
  it), so an autouse fixture flips the existing limiter object on and
  restores it — no module reloads (which would recreate the shared
  limiter out from under the app).
- Starlette's TestClient peer is the trusted ``testclient`` sentinel, so
  ``X-Forwarded-For`` is honored by ``_get_client_ip`` — each test uses
  a unique forwarded IP so tests never share a bucket (with each other
  or with other files in the same process).
- slowapi's route decorator checks/increments BEFORE the endpoint body
  runs, so cheap 400-validation POSTs are enough to burn quota — no
  SQLCipher work needed except for the authenticated change-password
  tests, which use the ``authenticated_client`` fixture.
"""

import importlib
import os
import time
import uuid
from contextlib import closing
from types import SimpleNamespace

import pytest
from fastapi.testclient import TestClient
from limits.storage import MemoryStorage

from local_deep_research.web.dependencies.rate_limit import (
    INTEGRITY_CHECK_RATE_LIMIT,
    LOGIN_RATE_LIMIT,
    PASSWORD_CHANGE_RATE_LIMIT,
    REGISTRATION_RATE_LIMIT,
    VALIDATE_PASSWORD_RATE_LIMIT,
    limiter,
)


def _first_amount(limit_value) -> int:
    """Number of allowed hits in the first window of a limit string.

    ``"5 per 15 minutes"`` -> 5; ``"60 per minute;1000 per hour"`` -> 60.
    Parsed from the module constants so the tests exercise whatever limit
    is actually wired (env overrides included) instead of hardcoding the
    defaults.
    """
    return int(str(limit_value).split(";")[0].strip().split()[0])


LOGIN_ATTEMPTS = _first_amount(LOGIN_RATE_LIMIT)
REGISTER_ATTEMPTS = _first_amount(REGISTRATION_RATE_LIMIT)
PASSWORD_CHANGE_ATTEMPTS = _first_amount(PASSWORD_CHANGE_RATE_LIMIT)
VALIDATE_ATTEMPTS = _first_amount(VALIDATE_PASSWORD_RATE_LIMIT)


def _require_small(n: int, what: str) -> None:
    """Keep runtime bounded if an env override cranked a limit way up."""
    if n > 15:
        pytest.skip(
            f"{what} limit is {n} in this environment — too many "
            "requests to exercise the threshold quickly"
        )


@pytest.fixture(autouse=True)
def rate_limiting_enforced():
    """Force slowapi enforcement ON for each test, then restore.

    The enabled flag is read from env at import time (CI runs with
    LDR_DISABLE_RATE_LIMITING=true), so flip the live limiter object
    rather than reloading the module. Counters are cleared afterwards so
    the buckets these tests exhaust can't leak into later suites in the
    same process.
    """
    original = limiter.enabled
    limiter.enabled = True
    yield
    limiter.enabled = original
    try:
        limiter.reset()
    except Exception:
        pass


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]}"


def _make_client(app, ip: str) -> TestClient:
    """Fresh client pinned to one forwarded IP, with CSRF armed.

    CSRFMiddleware runs before routing, so a POST rejected for CSRF
    would never reach the limit decorator — the X-CSRFToken default
    header keeps every form POST counting toward its bucket.
    """
    client = TestClient(app, raise_server_exceptions=False)
    client.headers.update({"X-Forwarded-For": ip})
    client.get("/auth/login")
    token = client.get("/auth/csrf-token").json()["csrf_token"]
    client.headers.update({"X-CSRFToken": token})
    return client


# Invalid-form payloads: fail validation fast (400) with no DB work,
# while still counting toward the route's bucket (slowapi increments
# before the handler body runs).
_BAD_LOGIN = {"username": "", "password": ""}
_BAD_REGISTER = {
    "username": "x",  # too short -> 400 before any DB access
    "password": "short",
    "confirm_password": "short",
    "acknowledge": "false",
}
_BAD_PASSWORD_CHANGE = {
    "current_password": "",  # "Current password is required" -> 400
    "new_password": "NewStrongP4ss!",
    "confirm_password": "NewStrongP4ss!",
}


def _exhaust_login(client: TestClient, headers=None) -> None:
    """Drive the login bucket for this client's IP to (at least) 429.

    Tolerates quota already partially spent (e.g. the fixture login of
    authenticated_client) by posting until a 429 is seen, bounded by
    LOGIN_ATTEMPTS + 1 total posts.
    """
    for _ in range(LOGIN_ATTEMPTS + 1):
        resp = client.post(
            "/auth/login",
            data=_BAD_LOGIN,
            headers=headers or {},
            follow_redirects=False,
        )
        if resp.status_code == 429:
            return
    assert resp.status_code == 429, (
        f"login never rate-limited after {LOGIN_ATTEMPTS + 1} attempts "
        f"(last status {resp.status_code})"
    )


class TestRegistrationBucket:
    def test_registration_429_after_threshold_with_main_body_shape(self, app):
        """The register bucket allows exactly its threshold, then 429s —
        and the 429 rides the app's exception handler, whose JSON body
        must keep main's {"error", "message"} contract end-to-end."""
        _require_small(REGISTER_ATTEMPTS, "registration")
        client = _make_client(app, _unique_ip())

        for i in range(REGISTER_ATTEMPTS):
            resp = client.post(
                "/auth/register", data=_BAD_REGISTER, follow_redirects=False
            )
            assert resp.status_code == 400, (
                f"attempt {i + 1}/{REGISTER_ATTEMPTS} must be a validation "
                f"400, not rate-limited; got {resp.status_code}"
            )

        resp = client.post(
            "/auth/register", data=_BAD_REGISTER, follow_redirects=False
        )
        assert resp.status_code == 429, (
            f"attempt {REGISTER_ATTEMPTS + 1} must be rate-limited; "
            f"got {resp.status_code}"
        )
        body = resp.json()
        assert body["error"] == "Too many requests"
        assert "message" in body

    def test_registration_hourly_cap_survives_minute_resets(
        self, app, monkeypatch
    ):
        """Minute resets permit retries without erasing the hourly cap."""
        if os.environ.get("LDR_SECURITY_RATE_LIMIT_REGISTRATION"):
            pytest.skip("requires the shipped registration limits")
        assert REGISTRATION_RATE_LIMIT == "10 per minute;300 per hour"
        if not isinstance(limiter._storage, MemoryStorage):
            pytest.skip("requires the default in-memory rate-limit storage")

        # Advance only the storage clock, leaving session/CSRF timestamps
        # and the rest of the app on real time. No waiting or user DB writes.
        started = time.time()
        clock = SimpleNamespace(now=started)
        monkeypatch.setattr(
            "limits.storage.memory.time",
            SimpleNamespace(time=lambda: clock.now),
        )
        with closing(_make_client(app, _unique_ip())) as client:
            for _ in range(30):
                for _ in range(10):
                    response = client.post(
                        "/auth/register",
                        data=_BAD_REGISTER,
                        follow_redirects=False,
                    )
                    assert response.status_code == 400
                clock.now += 61

            # The minute bucket has reset, but all 300 accepted attempts
            # still lie within the same hour.
            response = client.post(
                "/auth/register", data=_BAD_REGISTER, follow_redirects=False
            )
            assert response.status_code == 429
            assert response.headers["x-ratelimit-limit"] == "300"
            assert response.headers["x-ratelimit-remaining"] == "0"

            clock.now = started + 3601
            response = client.post(
                "/auth/register", data=_BAD_REGISTER, follow_redirects=False
            )
            assert response.status_code == 400

    def test_login_exhaustion_does_not_block_registration(self, app):
        """Login and register are separate buckets for the same IP."""
        _require_small(LOGIN_ATTEMPTS, "login")
        client = _make_client(app, _unique_ip())

        _exhaust_login(client)

        resp = client.post(
            "/auth/register", data=_BAD_REGISTER, follow_redirects=False
        )
        assert resp.status_code == 400, (
            "register must not share the login bucket; got "
            f"{resp.status_code} after login was exhausted"
        )


class TestValidatePasswordBucket:
    def test_typing_a_password_does_not_burn_login_quota(self, app):
        """More strength-checks than the whole login allowance must leave
        login usable — the old bug shared the login bucket, so 6
        keystrokes locked out the actual login."""
        _require_small(LOGIN_ATTEMPTS, "login")
        if LOGIN_ATTEMPTS + 1 > VALIDATE_ATTEMPTS:
            pytest.skip(
                "validate-password limit not larger than login limit in "
                "this environment"
            )
        client = _make_client(app, _unique_ip())

        for i in range(LOGIN_ATTEMPTS + 1):
            resp = client.post(
                "/auth/validate-password", data={"password": f"weak{i}"}
            )
            assert resp.status_code == 200, (
                f"strength-check {i + 1} must pass (limit is "
                f"{VALIDATE_ATTEMPTS}/window); got {resp.status_code}"
            )
            assert resp.json()["valid"] is False

        resp = client.post(
            "/auth/login", data=_BAD_LOGIN, follow_redirects=False
        )
        assert resp.status_code == 400, (
            "login quota was burned by validate-password calls; got "
            f"{resp.status_code}"
        )

    def test_login_exhaustion_does_not_block_validate_password(self, app):
        """Other direction: a locked-out login IP can still run the
        strength check (it has its own, larger bucket)."""
        _require_small(LOGIN_ATTEMPTS, "login")
        client = _make_client(app, _unique_ip())

        _exhaust_login(client)

        resp = client.post("/auth/validate-password", data={"password": "weak"})
        assert resp.status_code == 200, (
            "validate-password must not share the login bucket; got "
            f"{resp.status_code} after login was exhausted"
        )


class TestPerIpKeying:
    def test_login_bucket_is_per_ip(self, app):
        """Exhausting IP A blocks only IP A; IP B gets a fresh bucket and
        IP A stays blocked afterwards (keying, not resetting)."""
        _require_small(LOGIN_ATTEMPTS, "login")
        ip_a, ip_b = _unique_ip(), _unique_ip()
        client = _make_client(app, ip_a)

        for i in range(LOGIN_ATTEMPTS):
            resp = client.post(
                "/auth/login", data=_BAD_LOGIN, follow_redirects=False
            )
            assert resp.status_code == 400, (
                f"IP A attempt {i + 1}/{LOGIN_ATTEMPTS} must not be "
                f"rate-limited; got {resp.status_code}"
            )

        resp = client.post(
            "/auth/login", data=_BAD_LOGIN, follow_redirects=False
        )
        assert resp.status_code == 429, (
            f"IP A attempt {LOGIN_ATTEMPTS + 1} must be rate-limited; "
            f"got {resp.status_code}"
        )

        # Same session/CSRF token, different forwarded IP -> fresh bucket.
        resp = client.post(
            "/auth/login",
            data=_BAD_LOGIN,
            headers={"X-Forwarded-For": ip_b},
            follow_redirects=False,
        )
        assert resp.status_code == 400, (
            f"IP B must have its own bucket; got {resp.status_code}"
        )

        # And IP A is still blocked — B's request didn't reset anything.
        resp = client.post(
            "/auth/login", data=_BAD_LOGIN, follow_redirects=False
        )
        assert resp.status_code == 429, (
            f"IP A must remain rate-limited; got {resp.status_code}"
        )


class TestPasswordChangeBucket:
    def test_password_change_429_after_threshold_without_draining_login(
        self, authenticated_client
    ):
        """change-password allows its threshold then 429s — and all that
        spending must not have consumed the same IP's login bucket."""
        _require_small(PASSWORD_CHANGE_ATTEMPTS, "password-change")
        client = authenticated_client

        for i in range(PASSWORD_CHANGE_ATTEMPTS):
            resp = client.post(
                "/auth/change-password",
                data=_BAD_PASSWORD_CHANGE,
                follow_redirects=False,
            )
            assert resp.status_code == 400, (
                f"attempt {i + 1}/{PASSWORD_CHANGE_ATTEMPTS} must be a "
                f"validation 400, not rate-limited; got {resp.status_code}"
            )

        resp = client.post(
            "/auth/change-password",
            data=_BAD_PASSWORD_CHANGE,
            follow_redirects=False,
        )
        assert resp.status_code == 429, (
            f"attempt {PASSWORD_CHANGE_ATTEMPTS + 1} must be rate-limited; "
            f"got {resp.status_code}"
        )

        # The fixture spent exactly one login on this IP; if the
        # change-password burst had shared the login bucket, this would
        # now be 429 instead of a plain validation 400.
        resp = client.post(
            "/auth/login", data=_BAD_LOGIN, follow_redirects=False
        )
        assert resp.status_code == 400, (
            "login bucket was drained by change-password attempts; got "
            f"{resp.status_code}"
        )

    def test_login_exhaustion_does_not_block_password_change(
        self, authenticated_client
    ):
        """A user whose IP is login-locked (e.g. someone hammering their
        login) must still be able to reach change-password."""
        _require_small(LOGIN_ATTEMPTS, "login")
        client = authenticated_client

        _exhaust_login(client)

        resp = client.post(
            "/auth/change-password",
            data=_BAD_PASSWORD_CHANGE,
            follow_redirects=False,
        )
        assert resp.status_code == 400, (
            "change-password must not share the login bucket; got "
            f"{resp.status_code} after login was exhausted"
        )


class TestWrongCredentialsUnderALiveLimiter:
    """Wrong credentials must be 401 right up to the threshold, then 429.

    Ported from main's ``tests/auth_tests/test_auth_rate_limiting.py``
    (``test_login_rate_limit_allows_5_attempts``, tightened by PUNCHLIST
    H8_STATUS_OR from ``status_code in [200, 401, 400]`` to a bare
    ``== 401``), which the migration deleted.

    Everything else in this file drives the bucket with malformed forms
    that 400 before any credential check, precisely because that is cheap
    — and the account-lockout suites that DO use real wrong passwords
    switch ``limiter.enabled`` off. So no test on this branch combines a
    live limiter with a genuine credential rejection, and the state main
    pinned here — "the first N wrong-password attempts are ordinary auth
    failures, not rate-limit failures" — is unasserted. A regression that
    made the limiter fire a request early (an off-by-one in the bucket, a
    limit accidentally applied to the GET as well) would show up as a
    premature 429 and nothing would catch it; a regression that made bad
    credentials answer 200/400 instead of 401 would equally slip past,
    since this file's other tests deliberately expect 400.

    Distinct usernames per attempt, as main did: the per-username account
    lockout is a different control with a different threshold, and reusing
    one name would let it, not the rate limiter, decide the outcome.
    """

    def test_wrong_credentials_are_401_until_the_bucket_is_spent(self, app):
        _require_small(LOGIN_ATTEMPTS, "login")
        client = _make_client(app, _unique_ip())
        run = uuid.uuid4().hex[:8]

        for i in range(LOGIN_ATTEMPTS):
            resp = client.post(
                "/auth/login",
                data={
                    "username": f"nosuchuser_{run}_{i}",
                    "password": "wrongpassword",
                },
                follow_redirects=False,
            )
            assert resp.status_code == 401, (
                f"attempt {i + 1} of {LOGIN_ATTEMPTS}: wrong-credential "
                f"login must return 401, got {resp.status_code}"
            )

        resp = client.post(
            "/auth/login",
            data={
                "username": f"nosuchuser_{run}_{LOGIN_ATTEMPTS}",
                "password": "wrongpassword",
            },
            follow_redirects=False,
        )
        assert resp.status_code == 429, (
            f"attempt {LOGIN_ATTEMPTS + 1} must be rate-limited, got "
            f"{resp.status_code}"
        )


AUTH_MODULE = "local_deep_research.web.routers.auth"


class TestIntegrityCheckRateLimit:
    """``/auth/integrity-check`` must carry its own registered limit.

    Unlike login/register/change-password/validate-password, this route
    previously had no ``@limiter.limit`` decorator at all and fell through
    to the global default (``DEFAULT_RATE_LIMIT``, 5000 per hour / 50000
    per day) — no threshold small enough to drive behaviorally in a test.
    So, like ``tests/web/routers/test_notes_rate_limit_keys.py``, this
    introspects the real registered ``slowapi.wrappers.Limit`` off the
    live ``limiter`` object instead of sending requests.

    Revert this test catches: remove
    ``@limiter.limit(INTEGRITY_CHECK_RATE_LIMIT)`` from
    ``integrity_check`` in ``web/routers/auth.py`` — the qualified name
    then has no entry in ``limiter._route_limits`` and the first
    assertion below fails.
    """

    def test_integrity_check_has_a_registered_limit(self):
        auth_mod = importlib.import_module(AUTH_MODULE)
        qualified = f"{AUTH_MODULE}.integrity_check"
        limits = auth_mod.limiter._route_limits.get(qualified, [])
        assert limits, (
            f"{qualified} has no registered rate limit — "
            "/auth/integrity-check must not fall through to the global "
            "default"
        )

        expected_amount = _first_amount(INTEGRITY_CHECK_RATE_LIMIT)
        seen = {
            (lim.limit.amount, lim.limit.GRANULARITY.name) for lim in limits
        }
        assert all(amount == expected_amount for amount, _ in seen), (
            f"expected {expected_amount} per window from "
            f"INTEGRITY_CHECK_RATE_LIMIT={INTEGRITY_CHECK_RATE_LIMIT!r}, "
            f"got {seen}"
        )