~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
"""Planted journal artifacts and the crafted super-journal unlink class.
Research context (SQLite bug forum 2026 disclosure; sqlite.org/fileformat
§rollback journal): a crafted hot ``-journal`` whose header names a
"super-journal" (master journal) pathname can make a read-write opener
UNLINK that pathname during journal finalization -- i.e., an attacker
with data-directory write access can aim an arbitrary-file deletion at
any path the process may unlink. Related class from sqlite.org/
howtocorrupt.html: planting journal/WAL artifacts belonging to a
DIFFERENT database.
Probed and pinned against the real manager (all HOLD on this build):
* AI1 crafted super-journal: a journal carrying the real magic + a
plausible header + an out-of-directory super-journal name is consumed
harmlessly on open -- the SENTINEL file at the named path survives,
the open succeeds, and no engine/verifier anomalies occur. (The
crafted header's checksum is invalid, so SQLite discards it as a stale
journal; a fully checksum-valid super-journal header would be needed
to exercise the deletion path itself -- see the summary's scope
note.)
* AI2 a REAL hot journal stolen from another user's database (captured
mid-transaction in journal_mode=DELETE) and planted next to a closed
target: the databases are persistent WAL-mode (journal_mode lives in
the db header), so the rollback journal is not consulted at all --
the target opens, reads ONLY its own canary, and the other user's
secret is nowhere.
* AI3 garbage ``-wal`` bytes planted before open: SQLite's WAL-header
checksum fails, the file is treated as invalid and reset -- the open
succeeds, the target reads its own canary, nothing else appears.
"""
import shutil
import struct
import tempfile
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..6 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 _write_row(engine, value: str) -> None:
with engine.begin() as conn:
conn.execute(
text(
"CREATE TABLE IF NOT EXISTS gap_ai (id INTEGER PRIMARY KEY, v TEXT)"
)
)
conn.execute(text("INSERT INTO gap_ai (v) VALUES (:v)"), {"v": value})
def _run_crafted_super_journal_probe(manager, sentinel):
"""Body of AI1; split out so the sentinel cleanup can wrap it."""
username = f"ai1_{uuid.uuid4().hex[:8]}"
password = "CraftedJournalPw1!" # noqa: S105
engine = manager.create_user_database(username, password)
_write_row(engine, "gap-AI1")
manager.close_user_database(username)
db_path = manager._get_user_db_path(username)
journal = db_path.with_name(db_path.name + "-journal")
# Real journal magic + structurally plausible header (per the
# rollback-journal file-format doc), an intentionally INVALID
# checksum, and the super-journal pathname field pointing at the
# sentinel. SQLite validates the header checksum before playback and
# discards what fails -- the resist posture for crafted input.
payload = b"\xd9\xd5\x05\xf9\x20\xa1\x63\xd7"
payload += struct.pack(">I", 1) # file format version
payload += struct.pack(">I", 0) # page record count (0 = all)
payload += struct.pack(">I", 0) # checksum nonce
payload += struct.pack(">I", 1) # initial database size (pages)
payload += struct.pack(">I", 512) # sector size
payload += struct.pack(">I", 4096) # page size
payload += struct.pack(">I", 0xDEADBEEF) # header checksum (invalid)
payload += str(sentinel).encode() + b"\x00" # "super-journal" name
payload += b"\x00" * 256
journal.write_bytes(payload)
opened = manager.open_user_database(username, password)
assert opened is not None, (
"the crafted journal broke the open itself (unexpected)"
)
# THE assertion of this test: no arbitrary-file deletion happened.
assert sentinel.exists(), (
"the crafted super-journal pathname was UNLINKED during open -- "
"this SQLite build is vulnerable to the 2026 super-journal "
"deletion class"
)
assert sentinel.read_text() == "precious"
with opened.connect() as conn:
assert conn.execute(text("SELECT v FROM gap_ai")).scalar() == "gap-AI1"
manager.close_user_database(username)
def test_crafted_super_journal_does_not_unlink_arbitrary_files(manager):
"""AI1 (HOLD on this build): a planted hot journal whose payload
names an out-of-directory "super-journal" is consumed harmlessly;
the sentinel at that path survives; the open succeeds cleanly."""
if not manager.has_encryption:
pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
# The sentinel lives OUTSIDE the data directory (the attack's target).
sentinel = manager.data_dir.parent / f"r7_victim_{uuid.uuid4().hex[:8]}.txt"
sentinel.write_text("precious")
# The sentinel is in the per-worker basetemp (tmp_path.parent), which
# pytest only reclaims on its 3-run rotation, so the unlink has to run
# on the failure path too -- and on the very failure this test exists to
# catch, the sentinel is gone, hence missing_ok.
try:
_run_crafted_super_journal_probe(manager, sentinel)
finally:
sentinel.unlink(missing_ok=True)
def test_real_foreign_journal_planted_is_ignored_entirely(manager):
"""AI2 (HOLD): a genuine hot journal captured from ANOTHER user's
database, planted next to a closed target, is never consulted --
these databases are persistent WAL-mode, so rollback journals do not
participate. The target opens and serves ONLY its own data."""
if not manager.has_encryption:
pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
target = f"ai2t_{uuid.uuid4().hex[:8]}"
other = f"ai2o_{uuid.uuid4().hex[:8]}"
password = "ForeignJournalPw1!" # noqa: S105
engine_t = manager.create_user_database(target, password)
_write_row(engine_t, "target-secret")
engine_o = manager.create_user_database(other, password)
_write_row(engine_o, "OTHER-USER-SECRET")
# Capture a REAL hot journal: flip the other user into journal_mode
# DELETE and hold a transaction open across the copy.
other_db = manager._get_user_db_path(other)
other_journal = other_db.with_name(other_db.name + "-journal")
with tempfile.TemporaryDirectory() as stash:
stolen = Path(stash) / "stolen.journal"
conn = engine_o.connect()
try:
conn.execute(text("PRAGMA journal_mode=DELETE"))
conn.execute(text("INSERT INTO gap_ai (v) VALUES ('mid-txn')"))
assert other_journal.exists(), (
"setup: DELETE-mode journal expected mid-transaction"
)
shutil.copy2(other_journal, stolen)
conn.rollback() # release the hot journal
finally:
conn.close()
manager.close_all_databases()
target_db = manager._get_user_db_path(target)
planted = target_db.with_name(target_db.name + "-journal")
shutil.copy2(stolen, planted)
opened = manager.open_user_database(target, password)
assert opened is not None, (
"the planted real journal broke the target's open (unexpected: "
"WAL-mode databases should not consult rollback journals)"
)
with opened.connect() as conn:
values = sorted(
row[0]
for row in conn.execute(text("SELECT v FROM gap_ai")).fetchall()
)
assert values == ["target-secret"], (
f"cross-user data served through a planted journal: {values}"
)
# Still WAL-mode; the planted journal was irrelevant.
with opened.connect() as conn:
mode = conn.execute(text("PRAGMA journal_mode")).scalar()
assert str(mode).lower() == "wal"
manager.close_user_database(target)
def test_garbage_wal_planted_is_discarded_not_served(manager):
"""AI3 (HOLD): random bytes as -wal fail SQLite's WAL-header
checksum and are discarded/reset -- the open succeeds and the target
reads only its own canary."""
if not manager.has_encryption:
pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
username = f"ai3_{uuid.uuid4().hex[:8]}"
password = "GarbageWalPw1!" # noqa: S105
engine = manager.create_user_database(username, password)
_write_row(engine, "gap-AI3")
manager.close_user_database(username)
db_path = manager._get_user_db_path(username)
planted = db_path.with_name(db_path.name + "-wal")
planted.write_bytes(b"\xde\xad\xbe\xef" * 1024) # garbage
opened = manager.open_user_database(username, password)
assert opened is not None, (
"garbage -wal bytes broke the open (unexpected: invalid WAL "
"headers are supposed to be discarded)"
)
with opened.connect() as conn:
values = [
row[0]
for row in conn.execute(text("SELECT v FROM gap_ai")).fetchall()
]
assert values == ["gap-AI3"], (
f"garbage WAL served wrong or duplicated data: {values}"
)
manager.close_user_database(username)
# And a clean reopen cycle still works afterwards.
reopened = manager.open_user_database(username, password)
assert reopened is not None