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.

10.4 KB
Raw Blame
"""``get_session`` never-open-on-miss, proven against real SQLCipher files.

Incident context (PR #5596, cached-connection login auth bypass):
``DatabaseManager.connections`` was keyed by username alone and a cache hit
returned the engine without verifying the password. The fix's docstring for
``get_session`` documents the load-bearing guarantee that keeps this
passwordless accessor safe:

    It NEVER opens a database -- a cache miss returns ``None``. [...]

That property has no dedicated real-DB coverage today:
``tests/database/test_encrypted_db_manager.py::TestSessionManagement`` pins
``get_session`` only against ``MagicMock`` engines injected straight into
``connections``, so a regression that made ``get_session`` lazily open the
database (the exact "DO NOT add a lazy open-on-miss here" warning in the
fix) would pass every existing test and turn this post-auth accessor back
into an unauthenticated engine dispenser: any caller holding a username
would get a working engine for the on-disk database with no credential at
all -- a bypass strictly stronger than the original incident's, which at
least required a live cached connection.

These tests drive the real ``DatabaseManager`` with real SQLCipher files in
``tmp_path`` and assert the three shapes of the invariant:

* DB file exists on disk but the connection was closed -> ``None``, forever
  (no revival), and the file is not touched (content digest unchanged);
* user never opened -> ``None`` with NO side effects (no file creation, no
  cache pollution, not even a per-user init lock);
* many repeated calls -> a pure no-op, not a slow leak into any cache.
"""

import hashlib
import os
import uuid
from pathlib import Path

import pytest
from sqlalchemy import text

from local_deep_research.database.encrypted_db import DatabaseManager


@pytest.fixture
def manager(tmp_path, monkeypatch):
    """A DatabaseManager writing to an isolated data directory.

    Mirrors the incident-test fixture pattern (self-contained per file --
    ``test_login_cached_connection_password.py``), with the low test-mode
    PBKDF2 iteration knob that ``tests/conftest.py``'s own ``app`` fixture
    uses (``MIN_KDF_ITERATIONS_TESTING`` exists precisely for this); it
    speeds key derivation without weakening any distinctness property these
    tests assert on.
    """
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
    mgr = DatabaseManager()
    mgr.data_dir = tmp_path
    yield mgr
    # Dispose the real SQLCipher engines these tests open (a leaked
    # StaticPool/QueuePool engine holds live connections and FDs otherwise).
    mgr.close_all_databases()


def _dir_fingerprint(root: Path) -> dict:
    """Map every file under ``root`` to (size, sha256, mode).

    Used to prove ``get_session`` is a pure no-op: not only must it not
    create files, it must not touch, truncate, chmod or checkpoint anything
    already on disk either.
    """
    fingerprint = {}
    for path in sorted(root.rglob("*")):
        if path.is_file():
            data = path.read_bytes()
            fingerprint[path.name] = (
                len(data),
                hashlib.sha256(data).hexdigest(),
                os.stat(path).st_mode,
            )
    return fingerprint


def test_get_session_none_when_db_file_exists_but_connection_closed(manager):
    """A closed connection must stay closed: ``get_session`` returns ``None``
    even though the encrypted DB file (with real data in it) sits on disk.

    This is the "browser closed but DB idle" resting state from the
    incident. A lazy-open regression here would revive the engine with no
    credential, making the username alone sufficient again.

    ``create_user_database`` takes the per-user init lock (#6377), so the
    registry may already hold this user's entry after setup; the invariant
    is that ``get_session`` does not add or take one.
    """
    username = f"closed_db_{uuid.uuid4().hex[:8]}"
    password = "CorrectHorseBattery1!"  # noqa: S105
    engine = manager.create_user_database(username, password)
    with engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_a_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_a_canary (id, note) VALUES (1, :note)"),
            {"note": "gap-A"},
        )
    manager.close_user_database(username)

    db_path = manager._get_user_db_path(username)
    assert db_path.exists(), "setup: the closed DB file must still exist"
    before = _dir_fingerprint(manager.data_dir)
    locks_before = dict(manager._init_locks)

    # Repeated asks: no revival on the first call, the second, or the tenth.
    for _ in range(10):
        assert manager.get_session(username) is None, (
            "get_session revived/opened a database whose connection was "
            "closed -- the never-open-on-miss guarantee is the reason this "
            "passwordless accessor is safe (see the #5596 fix docstring)"
        )

    # No half-revival state either: nothing cached, no verifier armed, and
    # the on-disk artifacts byte-identical (no checkpoint/write/chmod).
    assert username not in manager.connections
    assert username not in manager._password_verifiers
    assert manager._init_locks == locks_before, (
        "get_session must not even run the open-path machinery (init lock): "
        "the registry changed"
    )
    lock = manager._init_locks.get(username)
    assert lock is None or not lock.locked()
    assert _dir_fingerprint(manager.data_dir) == before, (
        "get_session modified on-disk artifacts for a closed user"
    )

    # Control: the file is genuinely still openable -- with the credential,
    # through open_user_database, the credential-taking entry point.
    reopened = manager.open_user_database(username, password)
    assert reopened is not None
    with reopened.connect() as conn:
        note = conn.execute(
            text("SELECT note FROM gap_a_canary WHERE id = 1")
        ).scalar()
    assert note == "gap-A", "canary row must survive the close/reopen cycle"


def test_get_session_for_never_opened_user_creates_no_file(manager):
    """A username with no database anywhere must return ``None`` with zero
    side effects -- most importantly NO implicit file creation.

    If ``get_session`` ever materialized an empty database for an unknown
    username, it would become an oracle/enrollment primitive for arbitrary
    usernames and could shadow a later legitimate registration.
    """
    username = f"never_opened_{uuid.uuid4().hex[:8]}"

    assert manager.get_session(username) is None
    assert manager.get_session(username.upper()) is None  # near-miss name

    # The data directory must be exactly as it was: no DB, no salt, no
    # sidecar files, nothing.
    assert list(manager.data_dir.rglob("*")) == [], (
        "get_session created on-disk artifacts for a user that never existed"
    )
    assert manager.connections == {}
    assert manager._password_verifiers == {}
    assert manager._init_locks == {}


def test_get_session_is_not_an_auth_bypass_for_closed_db(manager):
    """Attacker shape from the incident: the caller controls only the
    username argument. While the victim's DB is closed, ``get_session`` must
    refuse; while a genuinely-connected user exists, it must work -- proving
    the refusals are policy, not a broken accessor.

    Complements test_login_cached_connection_password_route.py (which pins
    the login route end-to-end) by pinning the accessor itself at the
    connection layer against the lazy-open regression the fix explicitly
    warns about.
    """
    victim = f"victim_{uuid.uuid4().hex[:8]}"
    victim_password = "VictimSecretPassword1!"  # noqa: S105
    manager.create_user_database(victim, victim_password)
    manager.close_user_database(victim)

    friend = f"friend_{uuid.uuid4().hex[:8]}"
    friend_password = "FriendOtherPassword2!"  # noqa: S105
    friend_engine = manager.create_user_database(friend, friend_password)

    try:
        # The attacker "knows" the victim's username only.
        assert manager.get_session(victim) is None, (
            "get_session returned an engine for a closed database -- with no "
            "password argument this is an unauthenticated bypass"
        )
        assert victim not in manager.connections

        # Positive control on the same manager: the connected user DOES get
        # a usable session, bound to the cached engine.
        session = manager.get_session(friend)
        assert session is not None
        try:
            assert session.bind is friend_engine
            assert session.execute(text("SELECT 1")).scalar() == 1
        finally:
            session.close()

        # And the attacker's asks changed nothing about either user.
        assert manager.get_session(victim) is None
        assert victim not in manager.connections
        assert friend in manager.connections
    finally:
        manager.close_all_databases()


def test_get_session_repeated_calls_are_pure_noop(manager):
    """Hammering ``get_session`` for an uncached user must be a pure no-op:
    no temp files, no cache entries, no init locks -- nothing that a later
    call could mistake for state.

    Guards against a "harmless-looking" regression where a miss spawns a
    throwaway engine, session, or lock per call (FD/resource leak that also
    widens the attack surface the incident exploited).

    ``create_user_database`` takes the per-user init lock (#6377), so the
    registry may already hold this user's entry after setup; the invariant
    is that ``get_session`` does not add or take one.
    """
    username = f"noop_hammer_{uuid.uuid4().hex[:8]}"
    password = "NoOpPassword3!"  # noqa: S105
    manager.create_user_database(username, password)
    manager.close_user_database(username)
    before = _dir_fingerprint(manager.data_dir)
    locks_before = dict(manager._init_locks)

    for _ in range(50):
        assert manager.get_session(username) is None

    assert _dir_fingerprint(manager.data_dir) == before, (
        "50 get_session calls changed on-disk state for an uncached user"
    )
    assert manager.connections == {}
    assert manager._password_verifiers == {}
    assert manager._init_locks == locks_before, (
        "get_session must not even run the open-path machinery (init lock): "
        "the registry changed"
    )
    lock = manager._init_locks.get(username)
    assert lock is None or not lock.locked()
    assert manager.get_connected_usernames() == set()