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.

6.4 KB
Raw Blame
"""End-to-end proof, through the real /auth/login route, that a warm cached
connection no longer lets a wrong password authenticate.

The companion unit tests exercise ``DatabaseManager`` directly. The *impact* of
the bug, though, lived entirely in the login route treating any non-None engine
as authenticated (``web/auth/routes.py``: ``engine = db_manager.open_user_database(...)``
then ``record_success`` + session creation). This test drives the actual HTTP
endpoint with a genuinely cached connection -- the state that made the bypass
reachable -- so the route wiring can never silently diverge from the fix.

Reproduces the multi-agent review's end-to-end repro 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.

    The login/history/settings blueprints all share the module-level
    ``db_manager`` singleton, so the attacker's cross-blueprint reads only work
    if that same instance holds the warm connection -- hence we use the real
    singleton and just isolate its data dir, restoring it afterwards.
    """
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_TESTING_WITH_MOCKS", "true")
    monkeypatch.setenv("LDR_RATE_LIMITING_ENABLED", "false")

    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.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 touches the singleton lives inside try/finally from
    # here on, INCLUDING setup (init_auth_database/create_app can raise) --
    # not just the yield. A setup-time raise before try/finally would leave
    # db_manager.data_dir pointed at this test's (soon-deleted) tmp_path for
    # every later test that touches the singleton.
    original_data_dir = db_manager.data_dir
    try:
        db_manager.data_dir = tmp_path / "encrypted_databases"
        init_auth_database()

        # Ported from Flask: the FastAPI app is a module-level
        # singleton and CSRF is ASGI middleware, so the helpers below
        # fetch a real token rather than switching a config flag off.
        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,
        )

        yield app, db_manager
    finally:
        db_manager.close_all_databases()
        db_manager.data_dir = original_data_dir


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 _client(app):
    """A TestClient with its own X-Forwarded-For, so the per-IP limiter cannot
    bucket the victim's and the attacker's requests together."""
    import uuid as _u

    from fastapi.testclient import TestClient

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


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 _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 test_warm_cache_wrong_password_is_rejected_end_to_end(app_client):
    app, db_manager = app_client
    username = f"victim_{uuid.uuid4().hex[:8]}"
    good = "V!ctimPassw0rd123"  # noqa: S105
    bad = "attacker-does-not-know-this"  # noqa: S105

    victim = _client(app)
    reg = _register(victim, username, good)
    assert reg.status_code in (200, 302), (
        f"registration failed: {reg.status_code}"
    )
    assert db_manager.is_user_connected(username), (
        "the victim's connection must be cached for this test to be meaningful"
    )

    # Attacker: brand-new client, no cookies, knows only the username.
    attacker = _client(app)
    resp = attacker.post(
        "/auth/login",
        data={
            "username": username,
            "password": bad,
            "csrf_token": _csrf(attacker),
        },
        follow_redirects=False,
    )
    assert resp.status_code != 302, (
        "wrong password on a warm cache must NOT be a successful login "
        "(302 redirect) -- this is the bypass"
    )
    # Ported from Flask's session_transaction(): assert on what the app
    # reports rather than its server-side session internals. /auth/check is
    # the app's own answer to "am I logged in".
    assert _whoami(attacker) is None, (
        "no session may be minted for a wrong password"
    )
    hist = attacker.get("/history/api", follow_redirects=False)
    assert hist.status_code == 401, (
        "the attacker must not be able to read the victim's history "
        "(login_required returns 401 for an unauthenticated /api request)"
    )

    # Positive control: the correct password still authenticates end-to-end.
    legit = _client(app)
    ok = legit.post(
        "/auth/login",
        data={
            "username": username,
            "password": good,
            "csrf_token": _csrf(legit),
        },
        follow_redirects=False,
    )
    assert ok.status_code == 302
    assert _whoami(legit) == username
    assert legit.get("/history/api", follow_redirects=False).status_code == 200

    # Cold control: with the connection closed, a wrong password is still
    # rejected (the cold path was always correct).
    db_manager.close_user_database(username)
    cold_attacker = _client(app)
    cold = cold_attacker.post(
        "/auth/login",
        data={
            "username": username,
            "password": bad,
            "csrf_token": _csrf(cold_attacker),
        },
        follow_redirects=False,
    )
    assert cold.status_code != 302