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.

6.2 KB
Raw Blame
"""Integrity scan failures must not fence a user out of a database that opens.

The active key is decided by which password decrypts the schema page: a
database state never opens under both keys. ``quick_check`` and
``cipher_integrity_check`` findings describe file health, not the key, so a
database that was already damaged must still finish (or resolve) a password
change and let its user log in with the active password, with the finding
logged loudly. Real SQLCipher throughout.
"""

import uuid
from pathlib import Path

import pytest

from local_deep_research.database.encrypted_db import DatabaseManager
from local_deep_research.database.rekey_lock import (
    RekeyUnavailableError,
    begin_rekey_epoch,
    read_rekey_epoch_state,
)

OLD = "OldIntegrityHorse1!"  # noqa: S105
NEW = "NewIntegrityHorse2!"  # noqa: S105


@pytest.fixture
def manager(tmp_path, monkeypatch):
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    mgr = DatabaseManager()
    mgr.data_dir = tmp_path
    if not mgr.has_encryption:
        pytest.skip("SQLCipher not available")
    yield mgr
    mgr.close_all_databases()


def _user_db(manager: DatabaseManager) -> tuple[str, Path]:
    username = f"integrity_{uuid.uuid4().hex[:8]}"
    assert manager.create_user_database(username, OLD) is not None
    manager.close_all_databases()
    return username, manager._get_user_db_path(username)


def _share_a_root_page(db_path: Path, password: str) -> None:
    """Repro A: two tables point at one root page; every page decrypts."""
    conn = DatabaseManager._make_sqlcipher_connection(
        db_path, password, isolation_level=None
    )
    try:
        rows = conn.execute(
            "SELECT name, rootpage FROM sqlite_master "
            "WHERE type = 'table' AND rootpage > 2 ORDER BY rootpage"
        ).fetchall()
        conn.execute("PRAGMA writable_schema = ON")
        conn.execute(
            "UPDATE sqlite_master SET rootpage = ? WHERE name = ?",
            (rows[0][1], rows[1][0]),
        )
        conn.execute("PRAGMA writable_schema = OFF")
        conn.execute("PRAGMA wal_checkpoint(TRUNCATE)")
    finally:
        conn.close()
    fresh = DatabaseManager._make_sqlcipher_connection(db_path, password)
    try:
        quick = fresh.execute("PRAGMA quick_check").fetchall()
    finally:
        fresh.close()
    assert quick != [("ok",)], "the repro must fail quick_check"


def _damage_one_page(db_path: Path, password: str) -> None:
    """Repro B: one page (not page 1) fails its HMAC."""
    conn = DatabaseManager._make_sqlcipher_connection(
        db_path, password, isolation_level=None
    )
    try:
        page_size = int(conn.execute("PRAGMA page_size").fetchone()[0])
        conn.execute("PRAGMA wal_checkpoint(TRUNCATE)")
    finally:
        conn.close()
    offset = page_size * 10 + 100
    with db_path.open("r+b") as handle:
        handle.seek(offset)
        byte = handle.read(1)
        handle.seek(offset)
        handle.write(bytes([byte[0] ^ 0xFF]))


def _opens_with(db_path: Path, password: str) -> bool:
    try:
        DatabaseManager._make_sqlcipher_connection(db_path, password).close()
    except ValueError:
        return False
    return True


def test_shared_root_page_still_completes_the_password_change(manager):
    # Given a database whose schema fails quick_check while every page decrypts
    username, db_path = _user_db(manager)
    _share_a_root_page(db_path, OLD)

    # When the user changes the password
    assert manager.change_password(username, OLD, NEW) is True

    # Then the change completes and only the new password logs in
    assert read_rekey_epoch_state(db_path).pending_epoch is None
    assert manager.open_user_database(username, OLD) is None
    assert manager.open_user_database(username, NEW) is not None


def test_shared_root_page_pending_state_resolves_on_login(manager):
    # Given a quick_check-failing database left with a pending generation
    # (what an earlier failed verification left behind) after the rekey
    # applied
    username, db_path = _user_db(manager)
    _share_a_root_page(db_path, OLD)
    conn = DatabaseManager._make_sqlcipher_connection(db_path, OLD)
    try:
        from local_deep_research.database.sqlcipher_utils import (
            set_sqlcipher_rekey,
        )

        begin_rekey_epoch(db_path)
        set_sqlcipher_rekey(conn, NEW, db_path=db_path)
        conn.commit()
    finally:
        conn.close()
    assert _opens_with(db_path, NEW) and not _opens_with(db_path, OLD)
    pending = read_rekey_epoch_state(db_path).pending_epoch
    assert pending is not None

    # When the user logs in with the active (new) password
    assert manager.open_user_database(username, OLD) is None
    assert manager.open_user_database(username, NEW) is not None

    # Then the pending generation is committed and cleared
    state = read_rekey_epoch_state(db_path)
    assert state.pending_epoch is None
    assert state.committed_epoch == pending


def test_damaged_page_keeps_the_old_key_and_leaves_no_pending_state(manager):
    # Given a database with one page that fails its HMAC
    username, db_path = _user_db(manager)
    _damage_one_page(db_path, OLD)

    # When the user tries to change the password, the rekey cannot apply
    with pytest.raises(RekeyUnavailableError):
        manager.change_password(username, OLD, NEW)

    # Then the old key is provably kept, nothing is left pending, and the
    # user still logs in with the old password
    assert read_rekey_epoch_state(db_path).pending_epoch is None
    assert manager.open_user_database(username, NEW) is None
    assert manager.open_user_database(username, OLD) is not None


def test_damaged_page_pending_state_resolves_on_login(manager):
    # Given a damaged database left with a pending generation whose rekey
    # never applied
    username, db_path = _user_db(manager)
    _damage_one_page(db_path, OLD)
    pending = begin_rekey_epoch(db_path)

    # When the user logs in with the surviving (old) password
    assert manager.open_user_database(username, NEW) is None
    assert manager.open_user_database(username, OLD) is not None

    # Then the pending generation is resolved instead of fencing forever
    state = read_rekey_epoch_state(db_path)
    assert state.pending_epoch is None
    assert state.committed_epoch == pending