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.

14.3 KB
Raw Blame
"""HTTP-level tests for account lockout enforcement in POST /auth/login.

Mutation-testing proof (see PR review): patching
``local_deep_research.web.routers.auth.get_account_lockout_manager`` to a
stub whose ``is_locked``/``record_failure``/``record_success`` are all
no-ops leaves the pre-existing HTTP-level auth test files green -- the
lockout wiring in ``routers/auth.py`` (``is_locked`` gating the route with
429, ``record_failure`` on bad credentials, ``record_success`` on success)
is only ever tested by directly constructing ``AccountLockoutManager`` in
``tests/security/test_account_lockout.py`` and
``tests/web/auth/test_auth_coverage.py`` -- never through the route itself.
These tests close that gap by driving real failed/successful logins through
POST /auth/login and asserting on the route's actual HTTP responses.

Separating lockout from the per-IP rate limiter
-------------------------------------------------
``LOGIN_RATE_LIMIT`` defaults to "5 per 15 minutes" per IP
(``web/dependencies/rate_limit.py``), while ``AccountLockoutManager``'s
default threshold is 10 failed attempts per USERNAME
(``defaults/settings_security.json``:
``security.account_lockout_threshold``). A burst of >= 10 failed logins
against one IP therefore trips the per-IP rate limiter's 429 several
attempts before account lockout would ever fire -- masking the very
behaviour this file exists to prove (both return 429, so a naive
`status_code == 429` assertion can't tell them apart).

This module separates the two mechanisms two ways:

1. An autouse fixture forces ``limiter.enabled = False`` for the whole
   file (the mirror of ``test_auth_rate_limits.py``'s autouse fixture,
   which forces it ON) -- restored afterwards. That removes slowapi as a
   possible source of 429 entirely, so every 429 observed below can only
   come from account lockout.
2. Belt and suspenders: ``_assert_is_lockout_429`` also inspects the
   response BODY, not just the status code. The lockout path renders
   ``auth/login.html`` (HTML) with the flashed copy "Account is
   temporarily locked..."; the rate limiter's 429
   (``fastapi_app.py``'s ``_rate_limit_exceeded`` handler) is a JSON body
   shaped ``{"error": "Too many requests", "message": ...}`` and never
   contains the lockout copy. So even if (1) were ever silently
   ineffective, a rate-limiter 429 would fail the body check rather than
   being accepted as proof of lockout.

Each test also uses a unique X-Forwarded-For per client (the established
convention from ``test_auth_rate_limits.py``), so this file behaves
identically if it were ever run with rate limiting forced back on.
"""

import uuid

import pytest
from fastapi.testclient import TestClient

from local_deep_research.security.account_lockout import (
    get_account_lockout_manager,
)
from local_deep_research.web.dependencies.rate_limit import limiter

LOCKOUT_MESSAGE = "Account is temporarily locked"
# Matches tests/conftest.py's authenticated_client fixture -- already
# proven to satisfy PasswordValidator.validate_strength().
TEST_PASSWORD = "TestPass123"  # noqa: S105


@pytest.fixture(autouse=True)
def rate_limiting_disabled():
    """Force slowapi OFF for every test in this file.

    See module docstring: without this, a burst of failed logins large
    enough to trip account lockout (default threshold 10) also trips the
    per-IP login rate limit (default 5/15min) first, and that 429 would be
    indistinguishable at the status-code level from the lockout 429 this
    file exists to prove.
    """
    original = limiter.enabled
    limiter.enabled = False
    yield
    limiter.enabled = original


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) -> TestClient:
    """Fresh, unauthenticated client pinned to its own forwarded IP.

    Mirrors ``test_auth_rate_limits.py``'s ``_make_client``: primes the
    session via GET /auth/login, then arms the client with a real
    X-CSRFToken header so subsequent POSTs pass CSRFMiddleware.
    """
    client = TestClient(app, raise_server_exceptions=False)
    client.headers.update({"X-Forwarded-For": _unique_ip()})
    client.get("/auth/login")
    token = client.get("/auth/csrf-token").json()["csrf_token"]
    client.headers.update({"X-CSRFToken": token})
    return client


def _assert_is_lockout_429(resp, context: str) -> None:
    """Assert `resp` is the LOCKOUT 429, specifically -- not the rate
    limiter's. See module docstring for how the two are distinguished.
    """
    assert resp.status_code == 429, (
        f"{context}: expected 429 (account lockout), got "
        f"{resp.status_code}: {resp.text[:300]}"
    )
    assert LOCKOUT_MESSAGE in resp.text, (
        f"{context}: got a 429 but the body doesn't carry the lockout "
        f"flash message -- this looks like the rate limiter's 429, not "
        f"account lockout's. Body: {resp.text[:300]}"
    )
    assert "Too many requests" not in resp.text, (
        f"{context}: response body contains the rate-limiter's message "
        "alongside (or instead of) the lockout message"
    )


def _bad_login(client: TestClient, username: str, password: str = "wrong-pw"):
    return client.post(
        "/auth/login",
        data={"username": username, "password": password},
        follow_redirects=False,
    )


def _drive_to_pre_lock_boundary(
    client: TestClient, username: str, threshold: int, label: str = "attempt"
) -> None:
    """POST exactly `threshold` failed logins for `username`, asserting
    each is a plain 401 -- not yet the lockout 429.

    Boundary detail (see ``routers/auth.py``): ``is_locked()`` is checked
    at the START of a request using the counter's state from PREVIOUS
    attempts, and ``record_failure()`` (which sets ``locked_until`` once
    the count reaches ``threshold``) only runs AFTER that check, on the
    same request. So the request that pushes the count up to `threshold`
    is itself still answered 401 -- account lockout only starts
    REJECTING starting from request `threshold + 1`. This drives exactly
    up to (and including) that boundary request, leaving the very next
    login as the first one that should see 429.
    """
    for i in range(threshold):
        resp = _bad_login(client, username)
        assert resp.status_code == 401, (
            f"{label} {i + 1}/{threshold} should be a plain failed login "
            f"(not locked yet); got {resp.status_code}: {resp.text[:300]}"
        )


def _logout(client: TestClient) -> None:
    """Close `client`'s session via a real POST /auth/logout.

    This is what evicts the process-global cached DB connection for the
    session's username (``EncryptedDatabaseManager.close_user_database``,
    called from ``routers/auth.py``'s logout handler) -- see ``_register``
    for why that matters. A successful login/register clears the session
    (session-fixation defence) and invalidates whatever CSRF token the
    client was carrying, so this always fetches a fresh one first.
    """
    token = client.get("/auth/csrf-token").json()["csrf_token"]
    client.headers.update({"X-CSRFToken": token})
    resp = client.post("/auth/logout", follow_redirects=False)
    assert resp.status_code == 302, (
        f"logout failed (expected 302): {resp.status_code} {resp.text[:300]}"
    )


def _register(client: TestClient, username: str, password: str) -> None:
    """Register a real user through the HTTP route, then log back out.

    Registration logs the new user in AND caches the opened DB connection
    in-process (``EncryptedDatabaseManager.connections``); logging out is
    what calls ``close_user_database`` to evict that cache. Without doing
    so here, a later ``open_user_database`` call for this username hits
    the "already open" cache before it ever looks at the password
    (``encrypted_db.py`` around line 804-807) -- which would make a
    WRONG-password login attempt spuriously succeed right after
    registration and defeat the whole test. Logging out closes the
    connection so subsequent logins genuinely re-validate the password,
    and leaves the client clean/unauthenticated for the failed-login
    burst that follows.
    """
    resp = client.post(
        "/auth/register",
        data={
            "username": username,
            "password": password,
            "confirm_password": password,
            "acknowledge": "true",
        },
        follow_redirects=False,
    )
    assert resp.status_code == 302, (
        f"registration setup failed (expected 302): {resp.status_code} "
        f"{resp.text[:300]}"
    )
    _logout(client)
    client.cookies.clear()
    client.get("/auth/login")
    token = client.get("/auth/csrf-token").json()["csrf_token"]
    client.headers.update({"X-CSRFToken": token})


class TestLockoutTriggersAtThreshold:
    def test_burst_of_failed_logins_locks_account_with_429(self, app):
        """`threshold` failed logins all stay plain 401s (the last of them
        sets ``locked_until`` internally, but is itself answered before
        that state is checked); attempt `threshold + 1` is the first
        lockout 429, and it persists on the next attempt too (the
        is_locked() gate short-circuits before any DB-open attempt).
        """
        mgr = get_account_lockout_manager()
        threshold = mgr.threshold
        username = f"lockout_target_{uuid.uuid4().hex[:10]}"
        client = _make_client(app)

        _drive_to_pre_lock_boundary(client, username, threshold, "attempt")

        resp = _bad_login(client, username)
        _assert_is_lockout_429(
            resp, f"attempt {threshold + 1} (first attempt past threshold)"
        )

        resp = _bad_login(client, username)
        _assert_is_lockout_429(resp, f"attempt {threshold + 2} (still locked)")


class TestSuccessClearsLockoutCounter:
    def test_successful_login_resets_failure_count(self, app):
        """A successful login must invoke record_success() and zero the
        failure counter -- not merely "not lock immediately" (a stub that
        never locks anyone would pass a weaker check trivially), but
        genuinely reset it: walking a FULL second threshold-sized burst
        after the success must reproduce the exact same shape as the
        first burst (401s up to the boundary, then a 429 past it).

        Round 1 deliberately stops at (threshold - 1) failures rather than
        driving all the way to the pre-lock boundary: reaching `threshold`
        failures sets ``locked_until`` internally (see
        ``_drive_to_pre_lock_boundary``), and once that happens the
        is_locked() gate rejects EVERY subsequent request -- including a
        correct-password one -- before it ever reaches record_success().
        Stopping one short keeps the account genuinely unlocked so the
        success below can reach, and exercise, record_success().
        """
        mgr = get_account_lockout_manager()
        threshold = mgr.threshold
        username = f"lockout_reset_{uuid.uuid4().hex[:10]}"
        client = _make_client(app)
        _register(client, username, TEST_PASSWORD)

        # Round 1: rack up failures, stop one short of the pre-lock
        # boundary (see docstring above for why).
        for i in range(threshold - 1):
            resp = _bad_login(client, username)
            assert resp.status_code == 401, (
                f"round 1 attempt {i + 1}/{threshold - 1} should not be "
                f"locked yet; got {resp.status_code}: {resp.text[:300]}"
            )

        # Successful login with the correct password.
        resp = client.post(
            "/auth/login",
            data={"username": username, "password": TEST_PASSWORD},
            follow_redirects=False,
        )
        assert resp.status_code == 302, (
            f"correct-password login should succeed; got "
            f"{resp.status_code}: {resp.text[:300]}"
        )
        # Close the connection this successful login just cached, so
        # round 2's WRONG-password attempts genuinely re-validate the
        # password instead of hitting open_user_database's "already
        # open" cache (see _register's docstring for the same issue).
        _logout(client)

        # Round 2, from a fresh unauthenticated client, same username. If
        # record_success() had NOT cleared the counter, the leftover
        # (threshold - 1) failures from round 1 would tip lockout over
        # partway through this second burst instead of needing the FULL
        # threshold again -- e.g. a no-op record_success would lock on
        # round 2's very first attempt (count would already be at
        # threshold - 1 + 1 = threshold). Reproducing the exact same
        # shape as round 1 in TestLockoutTriggersAtThreshold (`threshold`
        # 401s, then 429 on attempt `threshold + 1`) proves the reset
        # genuinely happened.
        client2 = _make_client(app)
        _drive_to_pre_lock_boundary(client2, username, threshold, "round 2")

        resp = _bad_login(client2, username)
        _assert_is_lockout_429(
            resp, "round 2 hits the threshold again post-reset"
        )


class TestLockoutIsPerUsername:
    def test_locking_one_username_leaves_another_unaffected(self, app):
        """Lockout is keyed by USERNAME, not by client/IP: the same
        client, hammering a second username right after locking the
        first, gets a plain 401 for the second -- not swept up by the
        first username's lockout.
        """
        mgr = get_account_lockout_manager()
        threshold = mgr.threshold
        locked_user = f"lockout_a_{uuid.uuid4().hex[:10]}"
        other_user = f"lockout_b_{uuid.uuid4().hex[:10]}"

        # Deliberately the SAME client (same forwarded IP, same session)
        # for both usernames.
        client = _make_client(app)

        _drive_to_pre_lock_boundary(
            client, locked_user, threshold, "locked_user"
        )
        resp = _bad_login(client, locked_user)
        _assert_is_lockout_429(resp, "locked_user past the threshold")

        resp = _bad_login(client, locked_user)
        _assert_is_lockout_429(resp, "locked_user remains locked")

        # other_user, from the exact same client, must be unaffected.
        resp = _bad_login(client, other_user)
        assert resp.status_code == 401, (
            "a different username from the same client must not be "
            f"locked out by locked_user's failures; got "
            f"{resp.status_code}: {resp.text[:300]}"
        )