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.0 KB
Raw Blame
"""Legacy v1 (shared-salt) databases on the post-#5596 connection layer.

Incident context (PR #5596): the fix's verifier is keyed by the password
alone (``_cache_connection`` publishes engine+verifier together on every
credential-proving open) -- it does not care HOW the SQLCipher key was
derived. LDR's v1 format derives keys with the shared
``LEGACY_PBKDF2_SALT`` when no per-database ``.salt`` file exists
(``get_salt_for_database``, sqlcipher_utils.py:42); v2 uses a random
per-DB salt written at creation (``create_database_salt``, line 77). The
open path warns when it sees a v1 database
(encrypted_db.py::_open_user_database_cold).

A genuine v1 database is built here by neutering the single
``create_database_salt`` call inside ``create_user_database`` (monkeypatch
to a no-op): with no ``.salt`` file, every key derivation -- creation,
open, rekey -- takes the legacy-salt-by-omission path. This file pins:

* V1 cold open of a v1 DB: succeeds, caches the engine, ARMS the verifier
  (the fix covers all opens regardless of salt format), and logs the
  legacy-salt warning naming the user; wrong password fails cleanly;
  correct password works warm and cold;
* V2 change_password on a v1 DB (ACTUAL behavior, read from
  ``change_password`` + ``set_sqlcipher_rekey``): the rekey itself also
  derives via ``get_key_from_password(new, db_path)`` -- salt-by-omission
  -- so it SUCCEEDS and the database STAYS v1 (no ``.salt`` file is ever
  created); old key dead, new key opens, data survives;
* V3 a v1 user and a v2 user coexist on one manager: both open, verifiers
  independent, cross-user passwords fail, close_all clears both.
"""

import uuid

import pytest
from sqlalchemy import text

import local_deep_research.database.encrypted_db as encrypted_db_module
from local_deep_research.database.encrypted_db import DatabaseManager
from local_deep_research.database.sqlcipher_utils import get_salt_file_path

# Module-level gate. Without a working SQLCipher build every test below
# is a no-op, and the dozens of in-body ``pytest.skip`` calls let a CI
# lane report this whole battery green having executed nothing. Skipping
# at import time makes the missing dependency one visible, uniform signal
# per module instead.
pytest.importorskip("sqlcipher3", reason="requires SQLCipher (encrypted mode)")

# The real salt writer, captured at import time so the v1-construction
# helper can restore it after neutering it for one creation.
_REAL_CREATE_DATABASE_SALT = encrypted_db_module.create_database_salt


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

    Same self-contained pattern as the round-1/2/3/4 files, with the
    sanctioned test-mode KDF knob.
    """
    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
    mgr.close_all_databases()


def _create_v1_user(manager, monkeypatch, note):
    """Create a GENUINE v1 user: no .salt file is written, so every key
    derivation (creation included) uses LEGACY_PBKDF2_SALT by omission."""
    username = f"legacyv1_{uuid.uuid4().hex[:8]}"
    password = "LegacyV1Password1!"  # noqa: S105
    monkeypatch.setattr(
        encrypted_db_module, "create_database_salt", lambda _path: None
    )
    try:
        engine = manager.create_user_database(username, password)
    finally:
        # Restore immediately: later creates in the same test (v2 users)
        # must go through the real salt writer.
        monkeypatch.setattr(
            encrypted_db_module,
            "create_database_salt",
            _REAL_CREATE_DATABASE_SALT,
        )
    assert engine is not None
    with engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_v_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_v_canary (id, note) VALUES (1, :note)"),
            {"note": note},
        )
    manager.close_user_database(username)
    db_path = manager._get_user_db_path(username)
    assert not get_salt_file_path(db_path).exists(), (
        "setup: the v1 user must NOT have a .salt file"
    )
    return username, password


def test_v1_legacy_db_opens_and_arms_verifier_with_warning(
    manager, monkeypatch, loguru_caplog_full
):
    """V1: cold open of a legacy shared-salt database succeeds, caches the
    engine, arms the verifier, and logs the legacy warning; wrong
    password fails cleanly; correct password works warm and cold."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username, password = _create_v1_user(manager, monkeypatch, "gap-V1")

    with loguru_caplog_full.at_level("DEBUG"):
        engine = manager.open_user_database(username, password)
    assert engine is not None, "a v1 database failed to open with its password"
    assert manager.connections[username] is engine
    assert manager._password_matches_cached(username, password) is True, (
        "the verifier must be armed for a v1 open exactly as for v2 -- "
        "the fix's cache contract is salt-format independent"
    )
    assert username in loguru_caplog_full.text, (
        "the legacy-salt warning must name the user when opening a v1 DB"
    )
    assert "legacy shared salt" in loguru_caplog_full.text

    # Warm: the correct password reuses the cached engine.
    assert manager.open_user_database(username, password) is engine

    # Wrong password: clean refusal on the warm cache, nothing published.
    assert manager.open_user_database(username, "legacy-v1-wrong") is None
    assert manager.connections[username] is engine

    # Cold: same story after a close.
    manager.close_user_database(username)
    assert manager.open_user_database(username, "legacy-v1-wrong") is None
    reopened = manager.open_user_database(username, password)
    assert reopened is not None
    with reopened.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_v_canary WHERE id = 1")
            ).scalar()
            == "gap-V1"
        )


def test_change_password_on_v1_db_succeeds_and_stays_v1(manager, monkeypatch):
    """V2 (ACTUAL, read from the code): change_password rekeys through
    set_sqlcipher_rekey -> get_key_from_password(new, db_path), which
    ALSO derives by salt-omission for this user -- so the rekey succeeds,
    the old key dies, the new key opens with data intact, and the
    database REMAINS v1 (no .salt file is created; no migration to v2
    happens on the rekey path)."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username, old = _create_v1_user(manager, monkeypatch, "gap-V2")
    new = "LegacyV1NewPw2!"  # noqa: S105
    db_path = manager._get_user_db_path(username)

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

    # Old key dead, new key alive, data survived.
    assert manager.open_user_database(username, old) is None
    reopened = manager.open_user_database(username, new)
    assert reopened is not None
    with reopened.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_v_canary WHERE id = 1")
            ).scalar()
            == "gap-V2"
        )
    assert manager._password_matches_cached(username, new) is True

    # Format pin: still v1 -- no .salt file materialized anywhere.
    assert not get_salt_file_path(db_path).exists(), (
        "change_password created a per-database salt for a v1 user -- "
        "the rekey path was expected to stay salt-by-omission (pin "
        "updated if this intentionally changed)"
    )


def test_v1_and_v2_users_coexist_with_independent_verifiers(
    manager, monkeypatch
):
    """V3: a legacy user and a fresh per-salt user on one manager: both
    open with their own passwords, verifiers stay independent, cross-user
    passwords fail, close_all clears both."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    v1_user, v1_pw = _create_v1_user(manager, monkeypatch, "gap-V3-v1")

    v2_user = f"freshv2_{uuid.uuid4().hex[:8]}"
    v2_pw = "FreshV2Password2!"  # noqa: S105
    v2_engine = manager.create_user_database(v2_user, v2_pw)
    with v2_engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_v_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_v_canary (id, note) VALUES (1, :note)"),
            {"note": "gap-V3-v2"},
        )
    manager.close_user_database(v2_user)
    assert get_salt_file_path(manager._get_user_db_path(v2_user)).exists()

    # Both open; each keeps its own verifier.
    e1 = manager.open_user_database(v1_user, v1_pw)
    e2 = manager.open_user_database(v2_user, v2_pw)
    assert e1 is not None and e2 is not None and e1 is not e2
    assert manager._password_matches_cached(v1_user, v1_pw) is True
    assert manager._password_matches_cached(v2_user, v2_pw) is True
    with manager._connections_lock:
        assert set(manager.connections) == set(manager._password_verifiers)

    # Cross-user passwords fail against both the warm cache and the file.
    assert manager.open_user_database(v1_user, v2_pw) is None
    assert manager.open_user_database(v2_user, v1_pw) is None
    assert manager.connections[v1_user] is e1
    assert manager.connections[v2_user] is e2

    # Each engine reads only its own canary.
    with e1.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_v_canary WHERE id = 1")
            ).scalar()
            == "gap-V3-v1"
        )
    with e2.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_v_canary WHERE id = 1")
            ).scalar()
            == "gap-V3-v2"
        )

    manager.close_all_databases()
    with manager._connections_lock:
        assert manager.connections == {}
        assert manager._password_verifiers == {}