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.

7.1 KB
Raw Blame
"""KDF-iteration configuration drift resistance at open time.

Incident context (PR #5596): the fail-closed cache means every open is
ultimately backed by a real key validation. LDR derives SQLCipher keys
PYTHON-side (``get_key_from_password`` -> ``pbkdf2_hmac`` with
``get_sqlcipher_settings()["kdf_iterations"]``) and passes raw hex keys to
``PRAGMA key``, so the iteration count is part of the effective key
material even though it is NOT stored with the database (see
``warn_if_weak_kdf_with_existing_databases``'s docstring -- the
KDF-mismatch symptom class behind PR #4775).

If the environment's iteration count drifts between create and open, the
derived key changes and SQLCipher's HMAC check must fail. This file pins
that the manager does NOT silently absorb the drift -- no engine, no
verifier, no fallback -- in either direction, and that restoring the
creation-time count reopens the data:

* L1 downgrade (1000 -> 2000 between create and open): clean refusal,
  nothing published; restore -> opens, canary reads;
* L2 upgrade (1000 -> 4000): same clean refusal (direction-independence);
* L3 no residue / no silent fallback: repeated wrong-iter opens publish
  nothing and leave no state that could poison the later correct open.

``tests/database/test_kdf_iterations.py`` covers the settings-level floor
logic only (production vs test-mode minimums); the cross-OPEN behavior
here is new. Settings are read live per access (env_settings.py reads
``os.environ`` on every ``get``; no caching), which is what makes the
in-test env dance a faithful model of a drifted deployment.
"""

import uuid

import pytest
from sqlalchemy import text

from local_deep_research.database.encrypted_db import DatabaseManager
from local_deep_research.database.sqlcipher_utils import get_sqlcipher_settings

# 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)")


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

    Same self-contained pattern as the round-1/2 files; the iteration
    count is the sanctioned test-mode 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 _assert_nothing_published(manager, username):
    assert username not in manager.connections, (
        "a wrong-iteration open published an engine"
    )
    assert username not in manager._password_verifiers, (
        "a wrong-iteration open published a verifier"
    )
    assert manager.is_user_connected(username) is False


def _make_user_with_canary(manager, note):
    username = f"kdfdrift_{uuid.uuid4().hex[:8]}"
    password = "KdfDriftPassword1!"  # noqa: S105
    engine = manager.create_user_database(username, password)
    with engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_l_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_l_canary (id, note) VALUES (1, :note)"),
            {"note": note},
        )
    manager.close_user_database(username)
    return username, password


@pytest.mark.parametrize(
    ("drift_iterations", "direction"),
    [("2000", "downgrade"), ("4000", "upgrade")],
)
def test_kdf_iteration_drift_refuses_open_and_restore_reopens(
    manager, monkeypatch, drift_iterations, direction
):
    """L1 + L2: changing the effective KDF iterations between create and
    open changes the derived key, so the open must fail cleanly (None,
    nothing published) regardless of direction; restoring the creation-time
    count reopens and reads the canary."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username, password = _make_user_with_canary(manager, f"gap-L-{direction}")

    # Sanity: the env knob genuinely reaches the live settings read that
    # open_user_database's key derivation uses.
    assert get_sqlcipher_settings()["kdf_iterations"] == 1000
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", drift_iterations)
    assert get_sqlcipher_settings()["kdf_iterations"] == int(drift_iterations)

    assert manager.open_user_database(username, password) is None, (
        f"a {direction} of KDF iterations (1000 -> {drift_iterations}) "
        "still opened the database -- the differently-derived key was "
        "accepted without a fallback"
    )
    _assert_nothing_published(manager, username)

    # Restore the creation-time count: the original key is derived again,
    # the open succeeds, and the data survived the whole drift window.
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
    reopened = manager.open_user_database(username, password)
    assert reopened is not None, (
        "restore of the creation-time KDF count failed to reopen"
    )
    with reopened.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_l_canary WHERE id = 1")
            ).scalar()
            == f"gap-L-{direction}"
        )
    assert manager._password_matches_cached(username, password) is True


def test_repeated_wrong_iteration_opens_leave_no_residue(manager, monkeypatch):
    """L3: no silent fallback and no poisoning. Repeated opens under the
    wrong iteration count each refuse and publish NOTHING (an engine that
    'worked' via a hardcoded fallback would defeat the at-rest work factor
    -- the weak-KDF class PR #4816/#4775 warns about); afterwards the
    correct count opens cleanly, and a wrong password still fails closed
    through the verifier."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username, password = _make_user_with_canary(manager, "gap-L3")

    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "3000")
    for i in range(3):
        assert manager.open_user_database(username, password) is None, (
            f"wrong-iteration attempt #{i} opened the database"
        )
        _assert_nothing_published(manager, username)

    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
    reopened = manager.open_user_database(username, password)
    assert reopened is not None, (
        "residue from failed drift opens poisoned recovery"
    )
    with reopened.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_l_canary WHERE id = 1")
            ).scalar()
            == "gap-L3"
        )

    # The recovered cache is still fail-closed for a wrong password.
    assert manager.open_user_database(username, "kdf-wrong") is None
    assert manager.connections[username] is reopened
    assert manager._password_matches_cached(username, password) is True