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.

9.9 KB
Raw Blame
"""Rekey kills held credentials: stale engines and sessions die at REKEY.

Incident context (PR #5596): the fix's ``change_password`` comment
explains why it evicts the cached engine inside the try -- the engine's
creator closure "still derives the OLD hex key, so any freshly pooled
connection is mis-keyed" (encrypted_db.py::change_password). Round 3
pinned the
inverse property (an engine survives its own ``close_user_database``
because the creator closure reconnects with the SAME pre-derived hex key
-- "resurrection", by design). This file pins the complement the incident
threat model needs:

    resurrection-after-CLOSE works (same key material, file unchanged);
    resurrection-after-REKEY dies (same OLD key material, file REKEYED).

* P1 a live engine E1 and a Session S obtained with the old password die
  on use after ``change_password(old -> new)`` (HMAC failure), while the
  file stays readable with the NEW password and ``open(old)`` is None;
* P2 ordering: a session that already survived a close (round 3's
  resurrection) must still die once a rekey lands -- the closure's stale
  key is not healed by close/reconnect cycles, only by a rekey-aware
  reopen;
* P3 a lingering dead session does not poison the manager: caches stay
  clean and paired, new-password opens work, wrong-password opens fail
  closed, and close_all + reopen still reads the canary.
"""

import uuid

import pytest
from sqlalchemy import text

from local_deep_research.database.encrypted_db import DatabaseManager

# 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.

    Same self-contained pattern as the round-1/2/3 files, with the
    sanctioned test-mode KDF knob (each rekey performs several PBKDF2
    derivations).
    """
    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 _write_canary(engine, note: str) -> None:
    with engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_p_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_p_canary (id, note) VALUES (1, :note)"),
            {"note": note},
        )


def _make_user(manager, note):
    username = f"rekeyheld_{uuid.uuid4().hex[:8]}"
    old = "RekeyHeldOldPw1!"  # noqa: S105
    engine = manager.create_user_database(username, old)
    _write_canary(engine, note)
    return username, old, engine


def test_rekey_kills_live_engine_and_session(manager):
    """P1: engine + session obtained pre-rekey must fail on use after the
    rekey (old hex key vs rekeyed file -> HMAC failure); the new password
    reads the canary; the old password no longer opens."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username, old, engine = _make_user(manager, "gap-P1")
    session = manager.get_session(username)
    assert session is not None and session.bind is engine
    # Sanity: the session works BEFORE the rekey (reading real table
    # data, so the post-rekey contrast below cannot be satisfied from
    # the connection's page cache).
    assert (
        session.execute(
            text("SELECT note FROM gap_p_canary WHERE id = 1")
        ).scalar()
        == "gap-P1"
    )

    assert manager.change_password(username, old, "RekeyHeldNewPw2!") is True  # noqa: S105

    # The live engine now carries a dead key: any new checkout reconnects
    # through the creator closure with the OLD pre-derived hex key and the
    # first page read HMAC-fails.
    with pytest.raises(
        Exception, match="file is not a database|decrypt|operational|HMAC"
    ):
        with engine.connect() as conn:
            conn.execute(text("SELECT 1"))
    # ... and the already-bound session dies too, though by a different
    # mechanism: change_password's in-try eviction
    # (encrypted_db.py::change_password) calls
    # close_user_database, which disposes the cached engine's pool -- and
    # dispose() closes a checked-out DBAPI connection outright rather than
    # merely marking it stale, so the session's held connection is already
    # closed by the time this query runs (sqlcipher3 raises "Cannot
    # operate on a closed database", not an HMAC/decrypt error). Either
    # failure mode proves the same security property: a session obtained
    # before the rekey cannot read a single row after it.
    with pytest.raises(
        Exception,
        match="file is not a database|decrypt|operational|HMAC|closed database",
    ):
        session.execute(text("SELECT note FROM gap_p_canary WHERE id = 1"))
    # The connection underneath is already closed (see above), so
    # Session.close()'s own internal rollback can raise the same
    # DBAPI error instead of no-op'ing -- swallow it here, the way any
    # caller of a session it knows is dead already has to.
    try:
        session.close()
    except Exception:  # noqa: BLE001 - cleanup of an already-dead session
        pass

    # The FILE is fine for the NEW password; the OLD password is dead.
    new = "RekeyHeldNewPw2!"  # noqa: S105
    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_p_canary WHERE id = 1")
            ).scalar()
            == "gap-P1"
        )
    assert manager._password_matches_cached(username, new) is True


def test_rekey_kills_session_that_survived_a_close(manager):
    """P2: ordering matters. close_user_database alone leaves the held
    session working (round 3's pinned resurrection); the rekey that
    follows must kill it anyway -- the closure's key is stale forever
    until a credential-verified reopen with the new password."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username, old, engine = _make_user(manager, "gap-P2")
    session = manager.get_session(username)
    assert session is not None

    # Step 1: close. The session survives via creator-closure reconnect
    # (round-3 finding; used here as setup, not re-pinned in depth) --
    # proven with a real table read so the contrast with step 2 is
    # meaningful.
    manager.close_user_database(username)
    assert username not in manager.connections
    assert (
        session.execute(
            text("SELECT note FROM gap_p_canary WHERE id = 1")
        ).scalar()
        == "gap-P2"
    )

    # Step 2: rekey. The resurrecting connection now derives the OLD key
    # against a rekeyed file -- it must die on the same table read.
    assert manager.change_password(username, old, "RekeyHeldNewB3!") is True  # noqa: S105
    with pytest.raises(
        Exception, match="file is not a database|decrypt|operational|HMAC"
    ):
        session.execute(text("SELECT note FROM gap_p_canary WHERE id = 1"))
    session.close()

    # Recovery: the new password opens and reads the pre-rekey data.
    new = "RekeyHeldNewB3!"  # noqa: S105
    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_p_canary WHERE id = 1")
            ).scalar()
            == "gap-P2"
        )


def test_dead_session_does_not_poison_the_manager(manager):
    """P3: a lingering dead session (and dead pre-rekey engine) leaves the
    manager exactly as healthy as change_password's contract promises:
    clean/paired caches, working new-password opens, fail-closed
    wrong-password opens, and a close_all + reopen end state that reads
    the canary."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username, old, engine = _make_user(manager, "gap-P3")
    session = manager.get_session(username)
    assert session is not None
    new = "RekeyHeldNewC4!"  # noqa: S105

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

    # The dead handles exist but the caches are clean and paired.
    with manager._connections_lock:
        assert set(manager.connections) == set(manager._password_verifiers)
        assert username not in manager.connections
        assert username not in manager._password_verifiers

    # New password works (twice: cold open + warm cache hit).
    first = manager.open_user_database(username, new)
    assert first is not None
    assert manager.open_user_database(username, new) is first

    # Wrong password fails closed without disturbing the new engine.
    assert manager.open_user_database(username, "rekey-held-wrong") is None
    assert manager.connections[username] is first
    assert manager._password_matches_cached(username, new) is True

    # End-state health: close_all + reopen still reads the canary, and
    # the dead session stays dead (no accidental healing).
    manager.close_all_databases()
    with pytest.raises(
        Exception, match="file is not a database|decrypt|operational|HMAC"
    ):
        session.execute(text("SELECT 1"))
    session.close()
    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_p_canary WHERE id = 1")
            ).scalar()
            == "gap-P3"
        )