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.

11.0 KB
Raw Blame
"""Extra regression coverage for the cached-connection password verifier.

Companion to ``test_login_cached_connection_password.py``. That file pins the
core bypass and the fail-closed contract; this one pins the surrounding
behaviours a reviewer worried about the fix would want proven:

* a rejected wrong-password attempt on a warm cache must not disturb the
  legitimate user's cached engine/verifier (a mismatch that evicted the live
  engine would turn every wrong guess into a DoS against the real session);
* the phase-2 RAG re-key that re-enters ``open_user_database`` mid-login with
  the same password must reuse the cache, not trigger a second cold open;
* changing a password must invalidate the old verifier and arm the new one,
  and a *failed* change must leave no stale verifier/engine behind;
* two threads racing a first open with a wrong password must never be handed
  an engine;
* the metrics-session path fails closed on a wrong password just like login;
* unencrypted mode (no credential to verify) must still reuse the cache
  instead of thrashing cold opens.

These cases were surfaced by a multi-agent review of PR #5596.
"""

import threading
import uuid

import pytest

from local_deep_research.database.encrypted_db import DatabaseManager


@pytest.fixture
def manager(tmp_path, monkeypatch):
    """A DatabaseManager writing to an isolated data directory."""
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    mgr = DatabaseManager()
    mgr.data_dir = tmp_path
    yield mgr
    # Dispose the real SQLCipher engines these tests open (StaticPool under
    # TESTING=1 holds one live connection per engine indefinitely otherwise).
    # Matches the established pattern in tests/auth_tests/test_encrypted_db.py.
    mgr.close_all_databases()


@pytest.fixture
def user(manager):
    """A registered user whose database is created and left OPEN (cached)."""
    username = f"cacheextra_{uuid.uuid4().hex[:8]}"
    password = "CorrectHorseBattery1!"  # noqa: S105
    engine = manager.create_user_database(username, password)
    assert engine is not None, "setup failed: could not create the user DB"
    assert manager.is_user_connected(username), (
        "setup failed: the connection should be cached after creation"
    )
    return username, password


def test_wrong_password_leaves_legit_cached_connection_intact(manager, user):
    """A rejected wrong-password attempt must not evict or replace the
    legitimate cached engine, and the correct password must still return that
    same engine object afterwards."""
    username, password = user
    original = manager.connections[username]

    rejected = manager.open_user_database(username, "totally-wrong-password")

    assert rejected is None
    assert manager.is_user_connected(username), (
        "the wrong-password attempt must not have closed the legit connection"
    )
    assert manager.connections[username] is original, (
        "the wrong-password attempt must not have replaced the cached engine"
    )
    assert manager._password_matches_cached(username, password), (
        "the legitimate verifier must survive a rejected attempt"
    )
    assert manager.open_user_database(username, password) is original, (
        "the correct password must still return the same cached engine"
    )


def test_phase2_rekey_reentry_reuses_cached_engine(manager, monkeypatch):
    """The phase-2 RAG re-key re-enters open_user_database() mid-login with the
    same password. That re-entry must hit the freshly-populated cache, not do a
    second cold open, and its verifier check must match."""
    username = f"rekey_{uuid.uuid4().hex[:8]}"
    password = "CorrectHorseBattery1!"  # noqa: S105
    manager.create_user_database(username, password)
    manager.close_user_database(username)

    cold_passwords = []
    original_cold = manager._open_user_database_cold

    def counting_cold(u, p, start, prederived_hex_key=None):
        cold_passwords.append(p)
        return original_cold(u, p, start, prederived_hex_key)

    monkeypatch.setattr(manager, "_open_user_database_cold", counting_cold)

    reentry = {}

    def fake_rekey(u, db_password):
        # Exactly what legacy_rekey does: re-enter with the same password.
        reentry["engine"] = manager.open_user_database(u, db_password)
        reentry["verifier_matched"] = manager._password_matches_cached(
            u, db_password
        )

    monkeypatch.setattr(
        "local_deep_research.vector_stores.legacy_rekey.rekey_user_indexes",
        fake_rekey,
    )

    engine = manager.open_user_database(username, password)

    assert reentry, "the phase-2 re-key hook did not run on the cold open"
    assert len(cold_passwords) == 1, (
        "the re-key re-entry must reuse the cache, not trigger a 2nd cold open"
    )
    assert reentry["engine"] is engine, (
        "the re-key re-entry must get the same cached engine"
    )
    assert reentry["verifier_matched"] is True


def test_password_change_invalidates_old_verifier_and_arms_new(manager):
    """After change_password, the old password must be rejected (even while the
    new connection is warm) and the new password accepted."""
    username = f"chgpw_{uuid.uuid4().hex[:8]}"
    old = "OldCorrectHorse1!"  # noqa: S105
    new = "NewCorrectHorse2!"  # noqa: S105
    manager.create_user_database(username, old)

    assert manager.change_password(username, old, new) is True

    # change_password closes the connection; the old verifier must be gone.
    assert username not in manager._password_verifiers
    assert manager.open_user_database(username, old) is None, (
        "the old password must not open the re-keyed database"
    )
    engine = manager.open_user_database(username, new)
    assert engine is not None
    assert manager._password_matches_cached(username, new)
    assert not manager._password_matches_cached(username, old)
    # Old password rejected while the NEW connection is warm (the cache path).
    assert manager.open_user_database(username, old) is None
    assert manager.connections[username] is engine, (
        "the rejected old-password attempt must not replace the new engine"
    )


def test_failed_password_change_leaves_no_stale_state(manager):
    """A change_password with the wrong current password must fail and leave no
    stale verifier or connection behind."""
    username = f"chgpwfail_{uuid.uuid4().hex[:8]}"
    password = "CorrectHorseBattery1!"  # noqa: S105
    manager.create_user_database(username, password)
    manager.close_user_database(username)
    manager.open_user_database(username, password)

    assert manager.change_password(username, "wrong-old", "Another3!") is False
    assert not manager.is_user_connected(username)
    assert username not in manager._password_verifiers
    # The real password still works after the failed change.
    assert manager.open_user_database(username, password) is not None


def test_concurrent_first_open_never_returns_engine_for_wrong_password(
    manager,
):
    """Two threads racing the very first open of a user -- one with the correct
    password, one with a wrong one -- must never hand the wrong-password caller
    an engine, regardless of interleaving on the per-user init lock."""
    username = f"race_{uuid.uuid4().hex[:8]}"
    password = "CorrectHorseBattery1!"  # noqa: S105
    # Create then close so the first concurrent open is a genuine cold open.
    manager.create_user_database(username, password)
    manager.close_user_database(username)

    results = {}
    errors = {}
    barrier = threading.Barrier(2)

    def attempt(key, pw):
        barrier.wait(timeout=30)
        try:
            results[key] = manager.open_user_database(username, pw)
        except Exception as exc:  # noqa: BLE001 - surfaced via `errors` below
            # A bare `results[key] = manager.open_user_database(...)` would
            # let an unexpected exception in this thread vanish (Thread
            # swallows it) and turn into an opaque KeyError on `results[key]`
            # below instead of the real failure.
            errors[key] = exc

    threads = [
        threading.Thread(target=attempt, args=("good", password)),
        threading.Thread(target=attempt, args=("bad", "totally-wrong")),
    ]
    for t in threads:
        t.start()
    for t in threads:
        t.join(timeout=60)

    # Fail fast rather than hang the whole suite if a future lock-ordering
    # regression deadlocks the racing opens.
    assert not any(t.is_alive() for t in threads), (
        "open_user_database deadlocked under a concurrent wrong/right race"
    )
    assert not errors, f"open_user_database raised in a racing thread: {errors}"
    assert results["good"] is not None, "the correct password must succeed"
    assert results["bad"] is None, (
        "the wrong password must never be handed an engine, even while racing "
        "a legitimate concurrent open"
    )


def test_metrics_session_is_fail_closed_on_wrong_password(manager, user):
    """create_thread_safe_session_for_metrics must not hand out a session on a
    cached engine for a wrong password -- it is the same cache-hit-returns-
    engine shape as login and must fail closed the same way.

    A wrong password falls through to a real open, which rejects it; the
    correct password binds a session to the cached engine.
    """
    username, password = user
    cached = manager.connections[username]

    with pytest.raises(ValueError):
        manager.create_thread_safe_session_for_metrics(
            username, "totally-wrong-password"
        )
    # The rejected attempt must not have disturbed the legit cached engine.
    assert manager.connections[username] is cached

    session = manager.create_thread_safe_session_for_metrics(username, password)
    try:
        assert session.bind is cached, (
            "the correct password must reuse the cached engine"
        )
    finally:
        session.close()


def test_unencrypted_mode_reuses_cached_engine(tmp_path, monkeypatch):
    """Unencrypted mode must reuse the cached engine across the different
    placeholder 'passwords' the app uses, not thrash cold opens.

    There is no credential to verify in unencrypted mode (the cold path opens
    the plain-SQLite DB with any password), so the verifier check is
    short-circuited (see _cached_engine_trusted); otherwise the placeholder
    passwords used by session_context/database_middleware would mismatch and
    force repeated cold opens that orphan engines.
    """
    monkeypatch.setenv("LDR_BOOTSTRAP_ALLOW_UNENCRYPTED", "true")
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    mgr = DatabaseManager()
    mgr.has_encryption = False
    mgr.data_dir = tmp_path

    try:
        username = f"unenc_{uuid.uuid4().hex[:8]}"
        engine = mgr.create_user_database(username, "RealUserPassword123!")
        assert engine is not None

        # The three strings the app actually uses for the same unencrypted DB.
        assert (
            mgr.open_user_database(username, "RealUserPassword123!") is engine
        )
        assert mgr.open_user_database(username, "unencrypted-mode") is engine
        assert mgr.open_user_database(username, "dummy") is engine
    finally:
        mgr.close_all_databases()