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.

12.1 KB
Raw Blame
"""Ciphertext & salt tampering resilience at the DatabaseManager level.

Incident context (PR #5596): the fix's fail-closed cache means a tampered
database must never yield an engine through ANY path -- the cold open is
the last line of defense, and nothing it does on failure may leave
publishable state behind (an engine or verifier cached from a failed open
would re-arm the original bypass shape).

``tests/database/test_sqlcipher_integration.py`` covers tampering only
shallowly: ``test_corrupted_database_returns_none`` overwrites the whole
file with garbage and asserts None (no cache assertions, no wrong-password
variant, no recovery, no salt tampering), and
``test_cipher_integrity_check_detects_tampering`` accepts "may or may not
be detected". ``tests/database/test_encryption_constants.py`` pins the
wrong-size salt ValueError at the UTIL level only. This file pins the
MANAGER-level properties, deterministically:

* K1 in-place ciphertext tamper (bytes flipped inside page 1, so the
  sqlite_master read on the cold path must HMAC-fail): open(correct) and
  open(wrong) both refuse, cache NOTHING, and a restore from a pre-tamper
  backup reopens cleanly with the canary intact;
* K2 salt tampering at the manager level: deleted salt (falls back to the
  legacy salt -> different key -> clean None; and NO auto-recreation of
  the salt -- silent regeneration would destroy the data), swapped salt
  (another user's 32 bytes -> different key -> None), truncated salt
  (ValueError propagates from the derivation chokepoint; nothing cached);
  restore always reopens;
* K3 tamper racing concurrent opens: every post-tamper open refuses, the
  engine/verifier pairing invariant holds under the connections lock, and
  the stabilized cache is empty for the user.
"""

import shutil
import threading
import uuid
from pathlib import Path

import pytest
from sqlalchemy import text

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

JOIN_TIMEOUT_S = 60
BARRIER_TIMEOUT_S = 30


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

    Same self-contained pattern as the round-1/2 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 _tamper_bytes(path: Path, offsets) -> None:
    """XOR-flip one byte at each offset, in place."""
    size = path.stat().st_size
    usable = [off for off in offsets if 0 < off < size]
    assert usable, f"setup: no tamper offset fits file of {size} bytes"
    with open(path, "r+b") as handle:
        for off in usable:
            handle.seek(off)
            byte = handle.read(1)
            handle.seek(off)
            handle.write(bytes([byte[0] ^ 0xFF]))


def _write_canary(engine, note: str) -> None:
    with engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_k_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_k_canary (id, note) VALUES (1, :note)"),
            {"note": note},
        )


def _assert_nothing_cached(manager, username):
    assert username not in manager.connections, (
        "a refused open cached an engine"
    )
    assert username not in manager._password_verifiers, (
        "a refused open cached a verifier"
    )
    # NOTE: ``_init_locks`` gaining an entry on any open attempt is
    # by-design (created before the cold open, retained on close -- see
    # tests/database/test_concurrent_user_db_open.py), so emptiness of
    # that dict is deliberately NOT asserted.


def _setup_user(manager, note):
    username = f"tamper_{uuid.uuid4().hex[:8]}"
    password = "TamperProofPassword1!"  # noqa: S105
    engine = manager.create_user_database(username, password)
    _write_canary(engine, note)
    manager.close_user_database(username)
    db_path = manager._get_user_db_path(username)
    salt_path = get_salt_file_path(db_path)
    return username, password, db_path, salt_path


def test_ciphertext_tamper_refuses_open_and_restore_reopens(manager):
    """K1: flipping bytes inside page 1 makes the sqlite_master read
    HMAC-fail, so the cold open must refuse for the CORRECT password as
    well as a wrong one, cache nothing, and a restored file must reopen
    with its data intact."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username, password, db_path, salt_path = _setup_user(manager, "gap-K1")
    backup_db = db_path.with_name(db_path.name + ".backup")
    backup_salt = salt_path.with_name(salt_path.name + ".backup")
    shutil.copy2(db_path, backup_db)
    shutil.copy2(salt_path, backup_salt)

    # Tamper page 1 (page size is 16KB; sqlite_master lives on page 1) and
    # the start of page 2. The connection was checkpointed on close, so
    # the .db holds everything.
    _tamper_bytes(db_path, [100, 5000, 10000, 15000, 17000])

    assert manager.open_user_database(username, password) is None, (
        "the CORRECT password opened a tampered database -- HMAC failure "
        "was not enforced on the cold path"
    )
    assert manager.open_user_database(username, "tamper-wrong") is None
    _assert_nothing_cached(manager, username)

    # Restore the pristine artifacts -> the same password reopens and the
    # pre-tamper data is intact.
    shutil.copy2(backup_db, db_path)
    shutil.copy2(backup_salt, salt_path)
    reopened = manager.open_user_database(username, password)
    assert reopened is not None, "restore-from-backup failed to reopen"
    with reopened.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_k_canary WHERE id = 1")
            ).scalar()
            == "gap-K1"
        )
    assert manager._password_matches_cached(username, password) is True


def test_salt_tamper_refuses_open_without_side_effects(manager):
    """K2: deleting the salt (legacy-salt fallback -> different key ->
    clean None, and the salt is NOT auto-recreated), swapping in another
    user's salt (valid 32 bytes, wrong key material -> None), and
    truncating the salt (ValueError from the derivation chokepoint)
    all refuse without publishing state; restoring always reopens."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username, password, db_path, salt_path = _setup_user(manager, "gap-K2")
    other, other_password, other_db, other_salt = _setup_user(
        manager, "gap-K2-other"
    )
    backup_salt = salt_path.with_name(salt_path.name + ".backup")
    shutil.copy2(salt_path, backup_salt)

    # --- Deleted salt: get_salt_for_database falls back to the LEGACY salt
    # (read-only, sqlcipher_utils.py:60-66), the derived key changes, the
    # HMAC check fails, open returns None. No writer may recreate it.
    salt_path.unlink()
    assert manager.open_user_database(username, password) is None, (
        "open succeeded with a deleted salt (wrong key material accepted)"
    )
    _assert_nothing_cached(manager, username)
    assert not salt_path.exists(), (
        "the manager auto-recreated a deleted salt for an EXISTING "
        "database -- silent salt regeneration would permanently destroy "
        "the data (the original key could never be derived again)"
    )

    # --- Swapped salt: another user's valid 32-byte salt is the right
    # size but the wrong key material.
    shutil.copy2(other_salt, salt_path)
    assert manager.open_user_database(username, password) is None, (
        "open succeeded with another user's salt (cross-user key "
        "material accepted)"
    )
    assert manager.open_user_database(username, other_password) is None, (
        "open succeeded with the other user's PASSWORD on this user's DB"
    )
    _assert_nothing_cached(manager, username)

    # --- Truncated salt: wrong size -> ValueError from the salt reader
    # (manager-level behavior; the util-level raise is pinned in
    # test_encryption_constants.py). It propagates out of open_user_database
    # BEFORE any engine is built or cached.
    salt_path.write_bytes(salt_path.read_bytes()[:10])
    with pytest.raises(ValueError):
        manager.open_user_database(username, password)
    _assert_nothing_cached(manager, username)

    # --- Restore: the original salt reopens the original data.
    shutil.copy2(backup_salt, salt_path)
    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_k_canary WHERE id = 1")
            ).scalar()
            == "gap-K2"
        )


def test_tamper_racing_concurrent_opens_never_publishes(manager):
    """K3: with the file tampered, two threads hammering open(correct) (one
    also closing between attempts) must never obtain or publish anything;
    the pairing invariant holds at every sampled instant; the stabilized
    cache is empty for the user; restore reopens."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username, password, db_path, salt_path = _setup_user(manager, "gap-K3")
    backup_db = db_path.with_name(db_path.name + ".backup")
    backup_salt = salt_path.with_name(salt_path.name + ".backup")
    shutil.copy2(db_path, backup_db)
    shutil.copy2(salt_path, backup_salt)
    _tamper_bytes(db_path, [100, 5000, 10000, 15000])

    barrier = threading.Barrier(2)
    errors = {}
    violations = []
    pairing_violations = []

    def opener(label, do_close):
        try:
            barrier.wait(timeout=BARRIER_TIMEOUT_S)
            for i in range(4):
                engine = manager.open_user_database(username, password)
                if engine is not None:
                    violations.append(f"{label} #{i}: got {engine!r}")
                if do_close:
                    manager.close_user_database(username)
                with manager._connections_lock:
                    engines = set(manager.connections)
                    verifiers = set(manager._password_verifiers)
                if engines != verifiers:
                    pairing_violations.append(
                        f"{label} #{i}: {engines} vs {verifiers}"
                    )
        except Exception as exc:  # noqa: BLE001 - surfaced via `errors` below
            errors[label] = exc

    threads = [
        threading.Thread(target=opener, args=("open-close", True)),
        threading.Thread(target=opener, args=("open-only", False)),
    ]
    for t in threads:
        t.start()
    for t in threads:
        t.join(timeout=JOIN_TIMEOUT_S)
    assert not any(t.is_alive() for t in threads), "tamper race deadlocked"
    assert not errors, f"a racing opener raised: {errors}"
    assert not violations, f"a tampered DB handed out engines: {violations}"
    assert not pairing_violations, (
        f"engine/verifier pairing broke: {pairing_violations}"
    )

    # Stabilized: nothing reachable for this user from cached state.
    with manager._connections_lock:
        assert username not in manager.connections
        assert username not in manager._password_verifiers

    # Restore and recover.
    shutil.copy2(backup_db, db_path)
    shutil.copy2(backup_salt, salt_path)
    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_k_canary WHERE id = 1")
            ).scalar()
            == "gap-K3"
        )