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.4 KB
Raw Blame
"""Lifecycle coverage for the cached-connection password verifier.

Companion to ``test_login_cached_connection_password.py`` and
``test_login_cached_connection_password_extra.py``. Those two files pin the
core bypass, the fail-closed contract, non-destructive rejection, the
phase-2 rekey re-entry, password-change, concurrency, and the metrics-session
path. This file fills the remaining lifecycle gaps:

1. A composed fail-closed-then-heal sequence driven THROUGH the public
   ``open_user_database`` API (not the private ``_password_matches_cached``
   helper): dropping the verifier for a cached engine must force a real
   cold-reopen on the very next call, and that cold-reopen must RE-ARM the
   verifier so a subsequent call is trusted again.
2. ``close_all_databases`` clears both ``connections`` and
   ``_password_verifiers``.
3. Multi-user isolation: two users' cached engines and verifiers never cross
   -- user A's password can never open user B's cache entry, and evicting
   A's verifier does not disturb B's.
4. The verifier is re-armed on every cold-open success, not just the first
   one (create -> close -> open must both return a working engine and leave
   the verifier trusting that same password).
5. ``is_user_connected`` reflects only liveness (cached vs. not), never the
   password argument -- it takes no password at all, but the tests here pin
   that its answer tracks close/open regardless of what password is used to
   reopen.
"""

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()


def _make_user(manager, prefix, password="CorrectHorseBattery1!"):  # noqa: S107
    """Create-and-leave-cached a fresh user, returning (username, password)."""
    username = f"{prefix}_{uuid.uuid4().hex[:8]}"
    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


@pytest.fixture
def user(manager):
    """A registered user whose database is created and left OPEN (cached)."""
    return _make_user(manager, "lifecycle")


def test_dropped_verifier_heals_through_public_api(manager, user):
    """Composed fail-closed-then-heal driven through open_user_database().

    Dropping the verifier for a live cached engine (simulating any bug or
    code path that lost track of it) must not let the correct password
    silently reuse the now-unproven cache entry. open_user_database() must
    fall through to a real cold reopen -- and that cold reopen must succeed
    and re-publish a fresh verifier, so the NEXT call is trusted again
    without a further cold open.
    """
    username, password = user

    # Simulate a verifier loss on an otherwise-live connection.
    manager._password_verifiers.pop(username, None)

    # Call 1: the public API, with the CORRECT password, on a cached engine
    # with no verifier. Must fail closed on the cache and fall through to a
    # real cold reopen rather than returning the (now unverifiable) cached
    # engine or refusing outright.
    healed_engine = manager.open_user_database(username, password)

    assert healed_engine is not None, (
        "the correct password must still succeed via a cold reopen even "
        "though the cached engine's verifier was lost"
    )
    # The cold-reopen path always disposes and rebuilds the engine (see
    # _open_user_database_cold), so this need not be the same object that
    # was cached before the verifier was dropped, but it must be live and
    # cached now.
    assert manager.is_user_connected(username)
    assert manager.connections[username] is healed_engine

    # The heal must have re-armed the verifier immediately -- no further
    # cold open should be required.
    assert username in manager._password_verifiers, (
        "the cold reopen must re-publish a verifier for the healed engine"
    )

    # Call 2: the public API again. This must now be a warm cache hit that
    # trusts the verifier (no dropped-verifier fallback this time) and
    # returns the SAME engine object rebuilt in call 1.
    second = manager.open_user_database(username, password)
    assert second is healed_engine, (
        "after healing, the cache must be reused rather than cold-opening again"
    )
    assert manager._password_matches_cached(username, password) is True, (
        "the re-armed verifier must accept the same password that healed it"
    )

    # And the safety property still holds post-heal: a wrong password must
    # still be rejected against the newly-armed verifier.
    assert manager.open_user_database(username, "still-wrong") is None
    assert manager.connections[username] is healed_engine, (
        "a rejected wrong-password attempt after healing must not disturb "
        "the healed engine"
    )


def test_close_all_databases_clears_connections_and_verifiers(manager):
    """close_all_databases must wipe both maps, not just one."""
    userA = _make_user(manager, "closeall_a")
    userB = _make_user(manager, "closeall_b")

    assert manager.connections
    assert manager._password_verifiers

    manager.close_all_databases()

    assert manager.connections == {}, (
        "close_all_databases must clear every cached engine"
    )
    assert manager._password_verifiers == {}, (
        "close_all_databases must clear every cached verifier -- a leftover "
        "verifier with no matching engine is dead weight, and if a future "
        "bug ever re-added the engine under the same username without going "
        "through _cache_connection, a stale verifier here would silently "
        "authorize it"
    )

    # Both users must be fully logged out from the manager's point of view.
    assert not manager.is_user_connected(userA[0])
    assert not manager.is_user_connected(userB[0])


def test_multi_user_isolation(manager):
    """Two users open concurrently (both left cached); their engines and
    verifiers must never cross.

    Deliberately DIFFERENT passwords for A and B -- reusing the same
    password across users would make a cross-user open trivially succeed
    for the wrong reason (both passwords are "correct"), not prove
    isolation.
    """
    userA, passwordA = _make_user(manager, "isoA", "AliceCorrectHorse1!")
    userB, passwordB = _make_user(manager, "isoB", "BobDifferentHorse2!")

    engineA = manager.connections[userA]
    engineB = manager.connections[userB]
    assert engineA is not engineB

    # Each user's own correct password returns only their own engine.
    assert manager.open_user_database(userA, passwordA) is engineA
    assert manager.open_user_database(userB, passwordB) is engineB

    # User A's password must never open user B's cache entry, and vice
    # versa -- even though both usernames have live cached connections.
    assert manager.open_user_database(userB, passwordA) is None, (
        "user A's password must not open user B's cached database"
    )
    assert manager.open_user_database(userA, passwordB) is None, (
        "user B's password must not open user A's cached database"
    )

    # The rejected cross-user attempts must not have disturbed either
    # legitimate cached engine.
    assert manager.connections[userA] is engineA
    assert manager.connections[userB] is engineB
    assert manager._password_matches_cached(userA, passwordA)
    assert manager._password_matches_cached(userB, passwordB)

    # Dropping A's verifier must not affect B's verifier or cache entry at
    # all -- the maps are keyed per-user and must not interact.
    manager._password_verifiers.pop(userA, None)

    assert userB in manager._password_verifiers
    assert manager._password_matches_cached(userB, passwordB) is True
    assert manager.connections[userB] is engineB
    assert manager.is_user_connected(userB)

    # And B's state is untouched by A's verifier being gone: B's correct
    # password still returns B's cached engine without a cold open.
    assert manager.open_user_database(userB, passwordB) is engineB


def test_verifier_rearmed_on_cold_open_after_close(manager, user):
    """create -> close -> open(correct) must return a working engine AND
    leave _password_matches_cached True -- pinning that _cache_connection
    actually ran on the cold-open path (not just at creation time)."""
    username, password = user

    manager.close_user_database(username)
    assert not manager.is_user_connected(username)
    assert username not in manager._password_verifiers

    engine = manager.open_user_database(username, password)

    assert engine is not None
    assert manager.is_user_connected(username)
    assert manager._password_matches_cached(username, password) is True, (
        "the cold-open path must re-arm the verifier, not merely return an "
        "engine"
    )

    # The engine returned must actually be usable (a real, working
    # SQLCipher-backed connection), not a stale/disposed reference.
    with engine.connect() as conn:
        from sqlalchemy import text

        result = conn.execute(text("SELECT 1")).scalar()
        assert result == 1

    # And the cache must genuinely be warm now: a second call with the same
    # password returns the identical engine object without another cold
    # open.
    assert manager.open_user_database(username, password) is engine


def test_is_user_connected_reflects_liveness_only(manager, user):
    """is_user_connected must track cached-vs-not, and must be completely
    unaffected by any password argument passed elsewhere -- it takes no
    password parameter at all, and its answer must not depend on whether the
    most recent open_user_database call used the right or wrong password."""
    username, password = user

    assert manager.is_user_connected(username) is True

    manager.close_user_database(username)
    assert manager.is_user_connected(username) is False

    # A rejected wrong-password open on a not-yet-cached user must not make
    # is_user_connected report True.
    assert (
        manager.open_user_database(username, "totally-wrong-password") is None
    )
    assert manager.is_user_connected(username) is False

    # Reopening with the correct password makes it live again.
    engine = manager.open_user_database(username, password)
    assert engine is not None
    assert manager.is_user_connected(username) is True

    # Now that it's cached, a rejected wrong-password attempt must leave it
    # connected (the legit connection survives) -- liveness is unaffected by
    # a failed credential check either way.
    assert manager.open_user_database(username, "still-wrong") is None
    assert manager.is_user_connected(username) is True

    # And an unknown user (never created) must simply read False -- no
    # password argument involved at all.
    assert (
        manager.is_user_connected(f"never-existed-{uuid.uuid4().hex[:8]}")
        is False
    )

    manager.close_user_database(username)
    assert manager.is_user_connected(username) is False