~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Integrity-check control and file-swap under a warm cache.
Incident context (PR #5596): the verifier protects the CONTROL plane
(which password may use the cached engine). It deliberately does not --
and cannot -- attest the DATA plane (which bytes are in the file). This
file pins both halves of that distinction at the real-file level:
* X1 ``check_database_integrity`` as the tamper control: with the engine
warm and page-1 bytes flipped (round-3 K1's tamper shape), the
quick_check read HMAC-fails inside the method and it returns False --
WITHOUT evicting or polluting the engine/verifier pair (integrity
checking must not mutate trust state); restoring the file makes the
same warm engine report True again.
A companion file-swap-under-a-warm-cache test was dropped: it asserted on
SQLite page-cache internals (which stale page a held session happens to
still hold), which is neither an LDR contract nor stable across SQLite
builds. The underlying exposure -- control-plane trust outliving
data-plane integrity -- is out of scope for this battery.
"""
import shutil
import uuid
from pathlib import Path
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/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 _tamper_page_one(db_path: Path) -> None:
"""Flip a few bytes inside page 1 (sqlite_master's page)."""
size = db_path.stat().st_size
offsets = [off for off in (100, 5000, 15000) if 0 < off < size]
assert offsets, f"setup: db too small to tamper ({size} bytes)"
with open(db_path, "r+b") as handle:
for off in offsets:
handle.seek(off)
byte = handle.read(1)
handle.seek(off)
handle.write(bytes([byte[0] ^ 0xFF]))
def test_integrity_check_detects_tamper_without_polluting_cache(manager):
"""X1: warm engine + page-1 tamper -> check_database_integrity False
(the quick_check read fails the page HMAC inside the method); the
engine/verifier pair is untouched by the check; restore -> True again
on the SAME warm engine."""
if not manager.has_encryption:
pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
username = f"integrityx_{uuid.uuid4().hex[:8]}"
password = "IntegrityXPw1!" # noqa: S105
engine = manager.create_user_database(username, password)
with engine.begin() as conn:
conn.execute(
text(
"CREATE TABLE gap_x_canary (id INTEGER PRIMARY KEY, note TEXT)"
)
)
conn.execute(
text("INSERT INTO gap_x_canary (id, note) VALUES (1, :note)"),
{"note": "gap-X1"},
)
assert manager.check_database_integrity(username) is True
# Checkpoint the canary into the main file so on-disk tamper is what
# the next fresh read sees, then back the pristine file up.
manager.close_user_database(username)
db_path = manager._get_user_db_path(username)
backup = db_path.with_name(db_path.name + ".bak")
shutil.copy2(db_path, backup)
engine = manager.open_user_database(username, password)
assert engine is not None # warm again
_tamper_page_one(db_path)
assert manager.check_database_integrity(username) is False, (
"the integrity check did not detect a page-1 tamper"
)
# The check must not have evicted or polluted the trust state.
with manager._connections_lock:
assert set(manager.connections) == set(manager._password_verifiers)
assert manager.connections[username] is engine
assert manager._password_matches_cached(username, password) is True
# Restore: the SAME warm engine reports healthy again (the pooled
# connection's next read goes back to the restored bytes).
shutil.copy2(backup, db_path)
assert manager.check_database_integrity(username) is True, (
"the integrity check did not recover after the file was restored"
)
with engine.connect() as conn:
assert (
conn.execute(
text("SELECT note FROM gap_x_canary WHERE id = 1")
).scalar()
== "gap-X1"
)