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.

4.5 KB
Raw Blame
"""Login must verify the password even when the user's DB is already open.

``DatabaseManager.connections`` is keyed by username ALONE. A cache hit
therefore proves only that *someone* opened that user's database -- not that
the caller supplied the right password. ``open_user_database`` used to return
the cached engine on a bare hit, and the login route's only credential check
is whether that call returns non-None:

    engine = db_manager.open_user_database(username, password)

So while any session for a username was live -- the normal state whenever that
user is logged in anywhere -- knowing only the USERNAME was enough to
authenticate as them. It also called ``record_success()``, so it never tripped
the lockout counter or the rate limiter: there was nothing to brute-force and
nothing in the logs resembling an attack.

The cold-open path always validated the key against SQLCipher, which is why
the same wrong password was correctly rejected once the connection closed.
These tests pin both halves so the cache path can never diverge from it again.
"""

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.

    Leaving it cached is the whole point: it is the state that made the
    bypass reachable.
    """
    username = f"cachetest_{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_rejected_while_connection_is_cached(manager, user):
    """The bypass itself."""
    username, _ = user

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

    assert engine is None, (
        "a wrong password opened the database because the connection was "
        "cached -- the cache hit skipped credential verification entirely"
    )


def test_correct_password_still_uses_the_cache(manager, user):
    """The fix must not turn every login into a cold open."""
    username, password = user

    first = manager.open_user_database(username, password)
    second = manager.open_user_database(username, password)

    assert first is not None
    assert second is first, (
        "the correct password should return the cached engine, not rebuild it"
    )


def test_wrong_password_rejected_when_connection_is_cold(manager, user):
    """Control: the cold path was always correct. If this ever fails, the
    test is lying about what it proves."""
    username, _ = user
    manager.close_user_database(username)
    assert not manager.is_user_connected(username)

    assert (
        manager.open_user_database(username, "totally-wrong-password") is None
    )


def test_correct_password_works_after_close(manager, user):
    """A real close-then-reopen must still succeed -- the verifier is dropped
    with the connection, and the reopen re-proves the password."""
    username, password = user
    manager.close_user_database(username)

    assert manager.open_user_database(username, password) is not None


def test_verifier_is_dropped_when_the_connection_closes(manager, user):
    """No verifier may outlive the engine it described."""
    username, _ = user
    assert username in manager._password_verifiers

    manager.close_user_database(username)

    assert username not in manager._password_verifiers


def test_cache_is_unusable_without_a_verifier(manager, user):
    """Fail closed.

    If a connection is cached but no verifier was recorded (a path that
    forgot to call _record_password_verifier), the cache must not be trusted.
    Returning True there would reopen exactly this hole.
    """
    username, password = user
    manager._password_verifiers.pop(username, None)

    assert manager._password_matches_cached(username, password) is False