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.6 KB
Raw Blame
"""Session creation at login is fenced by the epoch the login proved.

Worker A's login proves the OLD password against the durable epoch E1
(``open_user_database`` returns an engine). Before A creates the session,
worker B's ``change_password`` rekeys the database and commits E2. The
session must be bound to what A PROVED (E1), not to whatever epoch is
committed by the time ``create_session`` runs (E2): bound to E2 it would
survive the password change it straddled, and the old password would
keep a live session.

The interleaving is forced deterministically: the real
``open_user_database`` runs, then a second ``DatabaseManager`` on the
same data directory (worker B) performs a real SQLCipher rekey before
the route continues to ``create_session``.

A login whose epoch cannot be read at session creation or right after
its cold open verified the password, whose committed epoch moved under
that cold open, or whose open recorded no epoch proof for the user,
answers 503 rather than issuing a
session it cannot fence. None of these refusals touches the lockout
counter: it is not incremented (not a guessing failure) and
not reset (no session was issued).
"""

import uuid

import pytest
from fastapi.testclient import TestClient

pytest.importorskip("sqlcipher3", reason="requires SQLCipher (encrypted mode)")

# Real durable epochs end to end: the autouse projection shim is off.
pytestmark = pytest.mark.real_durable_epoch


@pytest.fixture
def app(tmp_path, monkeypatch):
    """The real app on a throwaway data directory with test KDF rounds."""
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    # Production PBKDF2 rounds (256000) would dominate wall-clock: every
    # test here derives several keys. sqlcipher_utils allows low rounds
    # under pytest (MIN_KDF_ITERATIONS_TESTING).
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")

    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

    databases = tmp_path / "encrypted_databases"
    databases.mkdir(parents=True, exist_ok=True)
    # Pinned through monkeypatch so the ORIGINAL override is restored.
    monkeypatch.setattr(db_manager, "_data_dir_override", databases)
    init_auth_database()
    monkeypatch.setattr(
        "local_deep_research.web.routers.auth._perform_post_login_tasks",
        lambda *_a, **_k: None,
    )
    try:
        yield fastapi_app
    finally:
        db_manager.close_all_databases()


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


def _csrf(client: TestClient) -> str:
    client.get("/auth/login")
    return client.get("/auth/csrf-token").json().get("csrf_token", "")


def _register_and_logout(app, user: str, pw: str) -> None:
    client = _client(app)
    resp = client.post(
        "/auth/register",
        data={
            "username": user,
            "password": pw,
            "confirm_password": pw,
            "acknowledge": "true",
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )
    if resp.status_code != 302:
        pytest.fail(f"registration returned {resp.status_code}")
    token = client.get("/auth/csrf-token").json().get("csrf_token", "")
    client.post(
        "/auth/logout", headers={"X-CSRFToken": token}, follow_redirects=False
    )


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


def _user_sessions(username: str) -> list[str]:
    from local_deep_research.web.auth.session_manager import session_manager

    with session_manager._lock:
        return [
            sid
            for sid, data in session_manager.sessions.items()
            if data["username"] == username
        ]


def _failure_count(username: str) -> int:
    from local_deep_research.security.account_lockout import (
        get_account_lockout_manager,
    )

    return (
        get_account_lockout_manager()._state.get(username, {}).get("count", 0)
    )


def _one_prior_failure(username: str) -> None:
    from local_deep_research.security.account_lockout import (
        get_account_lockout_manager,
    )

    get_account_lockout_manager().record_failure(username)
    assert _failure_count(username) == 1


def test_old_password_login_straddling_a_rekey_gets_no_session(app):
    from local_deep_research.database.encrypted_db import (
        DatabaseManager,
        db_manager,
    )

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

    user = f"straddle_{uuid.uuid4().hex[:8]}"
    old_pw = "StraddleOld-Password1!"  # noqa: S105
    new_pw = "StraddleNew-Password2!"  # noqa: S105
    _register_and_logout(app, user, old_pw)
    _one_prior_failure(user)

    # Worker B: a separate manager over the same data directory.
    worker_b = DatabaseManager()
    worker_b.data_dir = db_manager.data_dir
    real_open = db_manager.open_user_database
    rekeyed = []

    def open_then_rekey_elsewhere(username, password):
        engine = real_open(username, password)
        if engine is not None and not rekeyed:
            rekeyed.append(worker_b.change_password(username, old_pw, new_pw))
        return engine

    try:
        with pytest.MonkeyPatch.context() as mp:
            mp.setattr(
                db_manager, "open_user_database", open_then_rekey_elsewhere
            )
            resp = _login(app, user, old_pw)
    finally:
        worker_b.close_all_databases()

    assert rekeyed == [True], "worker B's rekey did not commit"
    # The old password proved E1; E2 is committed now. No session.
    assert resp.status_code == 401, (
        f"old-password login straddling a rekey answered {resp.status_code}"
    )
    assert _user_sessions(user) == []
    # Refused before any session was issued: the earlier failure is
    # neither cleared (record_success) nor added to.
    assert _failure_count(user) == 1

    # The new password still logs in normally, and that clears the count.
    assert _login(app, user, new_pw).status_code == 302
    assert _failure_count(user) == 0


def test_unreadable_epoch_at_session_creation_answers_503(app, monkeypatch):
    from local_deep_research.database.encrypted_db import db_manager
    from local_deep_research.database.rekey_lock import (
        RekeyRecoveryRequiredError,
    )
    from local_deep_research.web.auth import session_manager as sm_module

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

    user = f"epoch503_{uuid.uuid4().hex[:8]}"
    pw = "Epoch503-Password1!"  # noqa: S105
    _register_and_logout(app, user, pw)
    _one_prior_failure(user)
    db_path = db_manager._get_user_db_path(user)

    reads = []

    def unreadable(username: str) -> str:
        reads.append(username)
        raise RekeyRecoveryRequiredError(db_path)

    # The open proves the password; only the session-creation read fails.
    monkeypatch.setattr(sm_module, "read_user_authorization_epoch", unreadable)
    resp = _login(app, user, pw)

    # Exactly one epoch read, and it was create_session's: the 503 comes
    # from the session-creation path, not from some other reader.
    assert reads == [user]
    assert resp.status_code == 503
    assert _user_sessions(user) == []
    assert _failure_count(user) == 1


def test_login_without_a_proof_for_the_user_answers_503(app, monkeypatch):
    # Every engine-returning open records a proof today; if one ever does
    # not (or records it for another username), the login must refuse
    # rather than create a session with the straddle check skipped.
    from local_deep_research.database import encrypted_db
    from local_deep_research.database.encrypted_db import db_manager

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

    user = f"noproof_{uuid.uuid4().hex[:8]}"
    pw = "NoProof-Password1!"  # noqa: S105
    _register_and_logout(app, user, pw)
    _one_prior_failure(user)
    real_open = db_manager.open_user_database

    def open_recording_a_foreign_proof(username, password):
        engine = real_open(username, password)
        encrypted_db._record_login_proof("someone-else", "not-this-epoch")
        return engine

    monkeypatch.setattr(
        db_manager, "open_user_database", open_recording_a_foreign_proof
    )
    resp = _login(app, user, pw)

    assert resp.status_code == 503
    assert _user_sessions(user) == []
    # Not a guessing failure, and no session was issued.
    assert _failure_count(user) == 1


def test_unreadable_epoch_after_a_correct_cold_open_answers_503(
    app, monkeypatch
):
    # The cold open verified the CORRECT password; the epoch re-read right
    # after it then fails (EISDIR here; EMFILE/EIO behave the same). That
    # proves nothing about the credential, so the login answers 503 and
    # leaves the lockout counter alone instead of reporting "invalid
    # credentials" (401) and counting a failure.
    from local_deep_research.database.encrypted_db import db_manager

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

    user = f"recheck_{uuid.uuid4().hex[:8]}"
    pw = "Recheck-Password1!"  # noqa: S105
    _register_and_logout(app, user, pw)
    _one_prior_failure(user)
    # Force the cold-open path (no cached engine to claim).
    db_manager.close_user_database(user)
    db_path = db_manager._get_user_db_path(user)
    epoch_file = db_path.with_name(f"{db_path.name}.rekey.epoch")
    real_cold_open = db_manager._open_user_database_cold
    cold_opens = []

    def cold_open_then_epoch_unreadable(*args, **kwargs):
        engine = real_cold_open(*args, **kwargs)
        cold_opens.append(engine is not None)
        epoch_file.unlink()
        epoch_file.mkdir()
        return engine

    monkeypatch.setattr(
        db_manager, "_open_user_database_cold", cold_open_then_epoch_unreadable
    )
    resp = _login(app, user, pw)

    assert cold_opens == [True], "the correct password did not cold-open"
    assert resp.status_code == 503
    assert _user_sessions(user) == []
    assert _failure_count(user) == 1
    assert user not in db_manager.connections


def test_epoch_moved_under_a_correct_cold_open_answers_503(app, monkeypatch):
    # The cold open verified the CORRECT password; the committed epoch is
    # then found to have moved (only a writer that bypassed the rekey
    # sidecar this open holds can do that). The password opened the
    # database, so it is not a guessing failure: the login answers 503 and
    # leaves the lockout counter alone instead of reporting "invalid
    # credentials" (401) and counting a failure.
    from local_deep_research.database import rekey_lock
    from local_deep_research.database.encrypted_db import db_manager

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

    user = f"moved_{uuid.uuid4().hex[:8]}"
    pw = "Moved-Password1!"  # noqa: S105
    _register_and_logout(app, user, pw)
    _one_prior_failure(user)
    # Force the cold-open path (no cached engine to claim).
    db_manager.close_user_database(user)
    db_path = db_manager._get_user_db_path(user)
    real_cold_open = db_manager._open_user_database_cold
    cold_opens = []

    def cold_open_then_epoch_moves(*args, **kwargs):
        engine = real_cold_open(*args, **kwargs)
        cold_opens.append(engine is not None)
        rekey_lock.advance_rekey_epoch(db_path)
        return engine

    monkeypatch.setattr(
        db_manager, "_open_user_database_cold", cold_open_then_epoch_moves
    )
    resp = _login(app, user, pw)

    assert cold_opens == [True], "the correct password did not cold-open"
    assert resp.status_code == 503
    assert _user_sessions(user) == []
    assert _failure_count(user) == 1
    assert user not in db_manager.connections


def test_unreadable_epoch_at_registration_auto_login_answers_503(
    app, monkeypatch
):
    # Registration auto-logs in through the same create_session, so an
    # epoch that cannot be read there gets login's retryable 503, not a
    # 500 (#7830). The account it just created stays: once the fault
    # clears, the same credentials sign in.
    import errno

    from local_deep_research.database.encrypted_db import db_manager
    from local_deep_research.database.rekey_lock import (
        RekeyEpochUnreadableError,
    )
    from local_deep_research.web.auth import session_manager as sm_module

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

    user = f"reg503_{uuid.uuid4().hex[:8]}"
    pw = "Register503-Password1!"  # noqa: S105
    reads = []

    def unreadable(username: str) -> str:
        reads.append(username)
        raise RekeyEpochUnreadableError(
            db_manager._get_user_db_path(username), errno.EMFILE
        )

    client = _client(app)
    with pytest.MonkeyPatch.context() as mp:
        mp.setattr(sm_module, "read_user_authorization_epoch", unreadable)
        resp = client.post(
            "/auth/register",
            data={
                "username": user,
                "password": pw,
                "confirm_password": pw,
                "acknowledge": "true",
                "csrf_token": _csrf(client),
            },
            follow_redirects=False,
        )

    # The refusal came from create_session's epoch read.
    assert reads == [user]
    assert resp.status_code == 503
    assert _user_sessions(user) == []
    assert _login(app, user, pw).status_code == 302