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.

13.1 KB
Raw Blame
"""Route-level proof that PR #5596 restores account-lockout accounting.

Pre-fix, ``open_user_database`` returned the cached engine for ANY password
whenever a connection was already warm (see ``encrypted_db.py``). The login
route (``web/auth/routes.py``) treats a non-None engine as a successful
login and calls ``lockout_mgr.record_success(username)`` -- which *clears*
the failure counter. So pre-fix, a wrong password against a warm cached
connection did not just bypass authentication: it actively evaded account
lockout, clearing any accumulated failure count on every attempt. A
credential-stuffing run against a logged-in victim could never trip the
lockout threshold.

The companion ``test_login_cached_connection_password_route.py`` pins the
authentication outcome (never a 302 / no session minted). This file pins
the *lockout accounting* side effect of that same code path, asserting
against the real module-level ``AccountLockoutManager`` singleton the route
actually uses.

Reproduces the multi-agent review's lockout-evasion finding for PR #5596.
"""

import uuid

import pytest


@pytest.fixture
def app_client(tmp_path, monkeypatch):
    """A real app + a live module-level db_manager pointed at a temp data dir.

    Same pattern as test_login_cached_connection_password_route.py: the
    login route reads the module-level ``db_manager`` singleton (so the
    warm-cache state must live on that exact instance) AND the module-level
    account-lockout singleton (so the lockout assertions are against what
    the route itself mutates, not a private copy).
    """
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_TESTING_WITH_MOCKS", "true")
    # NOTE: `LDR_RATE_LIMITING_ENABLED` (used by the companion route test) is
    # a *different* knob entirely -- it controls the adaptive search-engine
    # rate limiter, not the Flask HTTP rate limiter guarding /auth/login (see
    # settings/env_registry.py:is_rate_limiting_enabled's own docstring
    # warning about this exact name collision, issue #3905). The login route
    # is also wrapped in `login_limit` ("5 per 15 minutes" per IP by
    # default), which we WOULD hit partway through the lockout-threshold
    # loop below (masking record_failure() calls behind 429s instead of
    # 401s) without disabling it via the canonical var here.
    monkeypatch.setenv("LDR_DISABLE_RATE_LIMITING", "true")

    from local_deep_research.database.auth_db import init_auth_database
    from local_deep_research.database.encrypted_db import db_manager
    from local_deep_research.security.account_lockout import (
        get_account_lockout_manager,
    )
    from local_deep_research.web.fastapi_app import app as fastapi_app
    import local_deep_research.web.routers.auth as auth_routes

    if not db_manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    # Everything that mutates the module-level singleton lives inside
    # try/finally from here on, INCLUDING setup -- mirroring the companion
    # route fixture. init_auth_database()/create_app() can raise, and a
    # setup-time raise before the try would leave db_manager.data_dir pointed
    # at this test's (soon-deleted) tmp_path for every later test that touches
    # the singleton (_get_user_db_path reads the persistent self.data_dir, not
    # the env, so the leak is real). registered_users/lockout_mgr are pre-bound
    # so the finally can run even if setup raises before they'd be assigned.
    original_data_dir = db_manager.data_dir
    registered_users = []
    lockout_mgr = None
    try:
        db_manager.data_dir = tmp_path / "encrypted_databases"
        init_auth_database()

        # Ported from Flask: there is no create_app()/app.config here --
        # the FastAPI app is a module-level singleton, and CSRF is enforced by
        # ASGI middleware rather than a config flag, so _client() fetches a
        # real token instead of disabling the check.
        app = fastapi_app

        # Keep the synchronous test off the real post-login worker threads.
        monkeypatch.setattr(
            auth_routes,
            "_perform_post_login_tasks",
            lambda _u, _p, _sid=None: None,
        )

        lockout_mgr = get_account_lockout_manager()

        yield app, db_manager, lockout_mgr, registered_users
    finally:
        # Belt-and-suspenders isolation: tests/conftest.py's autouse
        # reset_all_singletons fixture already replaces the lockout
        # singleton (account_lockout._manager = None) around every test, but
        # explicitly clear the state for the users THIS test touched too, in
        # case that ordering ever changes.
        if lockout_mgr is not None:
            for username in registered_users:
                lockout_mgr._state.pop(username, None)
        db_manager.close_all_databases()
        db_manager.data_dir = original_data_dir


def _client(app):
    """A TestClient with its own X-Forwarded-For.

    Each client gets a distinct peer so the per-IP limiter cannot bucket
    several of these together; the lockout counter under test is per-USERNAME
    and must be observed without the IP limiter masking it.
    """
    from fastapi.testclient import TestClient

    client = TestClient(app, raise_server_exceptions=False)
    client.headers.update(
        {
            "X-Forwarded-For": f"10.{uuid.uuid4().int % 254 + 1}.{uuid.uuid4().int % 254 + 1}.7"
        }
    )
    return client


def _csrf(client):
    client.get("/auth/login")
    resp = client.get("/auth/csrf-token")
    return resp.json().get("csrf_token", "") if resp.status_code == 200 else ""


def _whoami(client):
    """The username the app believes this client is, or None."""
    resp = client.get("/auth/check")
    if resp.status_code != 200:
        return None
    return resp.json().get("username")


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


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


def _register_and_warm(app, db_manager, registered_users, prefix, password):
    """Register a fresh user, leaving their connection cached (warm)."""
    username = f"{prefix}_{uuid.uuid4().hex[:8]}"
    registered_users.append(username)
    client = _client(app)
    reg = _register(client, username, password)
    assert reg.status_code in (200, 302), (
        f"registration failed: {reg.status_code} / {reg.text[:500]}"
    )
    assert db_manager.is_user_connected(username), (
        "the connection must be cached (warm) after registration for this "
        "test to be meaningful"
    )
    return username


def test_warm_cache_wrong_password_increments_lockout_failure_count(
    app_client,
):
    """A wrong password against a warm connection must take the
    record_failure() branch, not record_success()."""
    app, db_manager, lockout_mgr, registered_users = app_client
    good = "V!ctimPassw0rd123"  # noqa: S105
    bad = "attacker-does-not-know-this"  # noqa: S105

    username = _register_and_warm(
        app, db_manager, registered_users, "lockout_wrong", good
    )

    assert lockout_mgr.is_locked(username) is False
    before = lockout_mgr._state.get(username, {}).get("count", 0)

    attacker = _client(app)
    resp = _login(attacker, username, bad)
    assert resp.status_code != 302, (
        "wrong password on a warm cache must not authenticate"
    )

    entry = lockout_mgr._state.get(username)
    assert entry is not None, (
        "a wrong password against a warm cached connection must create/ "
        "increment a lockout failure entry -- pre-fix, the cached-connection "
        "bypass reached record_success() instead, which pops any entry "
        "rather than creating one, so this would be None"
    )
    assert entry["count"] == before + 1, (
        "the failure count must increment by exactly one per wrong-password "
        "attempt against the warm cache"
    )
    assert lockout_mgr.is_locked(username) is False, (
        "a single failed attempt must not itself trigger a lockout "
        "(threshold is well above 1)"
    )


def test_repeated_warm_cache_wrong_passwords_eventually_lock_account(
    app_client,
):
    """Pre-fix, repeated wrong-password attempts against a warm connection
    could never lock the account (each attempt cleared the counter via
    record_success()). Post-fix, they must accumulate and lock at
    threshold."""
    app, db_manager, lockout_mgr, registered_users = app_client
    good = "V!ctimPassw0rd123"  # noqa: S105
    bad = "attacker-does-not-know-this"  # noqa: S105

    username = _register_and_warm(
        app, db_manager, registered_users, "lockout_repeat", good
    )

    threshold = lockout_mgr.threshold
    assert lockout_mgr.is_locked(username) is False

    last_resp = None
    for attempt in range(1, threshold + 1):
        assert db_manager.is_user_connected(username), (
            "the connection must stay warm/cached across attempts for this "
            "test to be meaningful -- a wrong password must never evict the "
            "legitimate cached engine"
        )
        attacker = _client(app)
        last_resp = _login(attacker, username, bad)
        assert last_resp.status_code != 302, (
            f"wrong password must never authenticate (attempt {attempt})"
        )

    assert lockout_mgr.is_locked(username) is True, (
        f"after {threshold} wrong-password attempts against a warm cached "
        "connection, the account must be locked -- pre-fix this could never "
        "happen because every attempt hit record_success() and reset the "
        "counter to zero"
    )
    # Once locked, the route itself must refuse further attempts with 429
    # regardless of password correctness.
    locked_resp = _login(_client(app), username, good)
    assert locked_resp.status_code == 429, (
        "a locked account must be refused even with the CORRECT password"
    )


def test_correct_login_clears_failure_count_and_is_not_locked(app_client):
    """A correct login (whether cold or against a warm connection) must
    call record_success() and leave the account unlocked, with a clean
    failure count."""
    app, db_manager, lockout_mgr, registered_users = app_client
    good = "V!ctimPassw0rd123"  # noqa: S105
    bad = "attacker-does-not-know-this"  # noqa: S105

    username = _register_and_warm(
        app, db_manager, registered_users, "lockout_success", good
    )

    # Accumulate a few failures first (but stay below threshold) so we can
    # observe them being cleared.
    for _ in range(3):
        _login(_client(app), username, bad)
    assert lockout_mgr._state.get(username, {}).get("count", 0) == 3
    assert lockout_mgr.is_locked(username) is False

    ok = _login(_client(app), username, good)
    assert ok.status_code == 302, "the correct password must authenticate"

    assert lockout_mgr.is_locked(username) is False
    assert username not in lockout_mgr._state, (
        "a successful login must clear the failure-count entry entirely "
        "(record_success pops the state), not merely avoid incrementing it"
    )


def test_register_logout_relogin_end_to_end(app_client):
    """Sanity: register -> logout -> re-login with the correct password
    still works end to end through the real route stack."""
    app, db_manager, lockout_mgr, registered_users = app_client
    good = "V!ctimPassw0rd123"  # noqa: S105

    username = f"lockout_e2e_{uuid.uuid4().hex[:8]}"
    registered_users.append(username)

    # Use the SAME client throughout so the session cookie set at
    # registration carries through to the logout/re-login calls below.
    client = _client(app)
    reg = _register(client, username, good)
    assert reg.status_code in (200, 302), (
        f"registration failed: {reg.status_code} / {reg.text[:500]}"
    )
    assert db_manager.is_user_connected(username), (
        "the connection must be cached (warm) after registration"
    )
    # Ported from Flask's session_transaction(), which has no Starlette
    # equivalent: assert on what the app actually reports instead of poking
    # at server-side session internals. /auth/check is the app's own answer
    # to "am I logged in", so this is a stronger check, not a weaker one.
    assert _whoami(client) == username, "registration must also log the user in"

    logout_resp = client.post(
        "/auth/logout",
        headers={"X-CSRFToken": _csrf(client)},
        follow_redirects=False,
    )
    assert logout_resp.status_code in (200, 302)
    assert _whoami(client) is None, "logout must clear the session"

    relogin = _login(client, username, good)
    assert relogin.status_code == 302, (
        "re-login with the correct password must succeed after logout"
    )
    assert _whoami(client) == username

    assert lockout_mgr.is_locked(username) is False
    assert username not in lockout_mgr._state