~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
"""Per-instance trust: two DatabaseManagers never share cache trust.
Incident context (PR #5596): the verifier is keyed by a per-process random
``_verifier_key`` (``secrets.token_bytes(32)`` in ``__init__``), and both
``connections`` and ``_password_verifiers`` are plain instance attributes.
``test_password_verifier_primitive.py::test_fresh_managers_do_not_share_
verifier_keys`` pins the digest-level consequence (manager B cannot
validate manager A's verifier tuple). This file pins the real-database
consequence at the ``open_user_database`` / ``get_session`` layer:
* manager A's warm cache gives manager B nothing: B's wrong-password open
fails at SQLCipher and B's correct-password open builds B's OWN engine;
* closing A's engine cannot invalidate or disturb B's live engine (a
cross-instance eviction would be an availability bug; the reverse -- A's
trust leaking into B -- would be the bypass);
* B fails closed on its own warm cache exactly like A does.
Two live managers over one data directory is a real deployment shape (e.g.
a tooling process plus the web app before per-user DB locking, or tests
constructing managers against a shared temp dir).
"""
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_pair(tmp_path, monkeypatch):
"""Two independent DatabaseManagers sharing one data directory."""
monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
manager_a = DatabaseManager()
manager_a.data_dir = tmp_path
manager_b = DatabaseManager()
manager_b.data_dir = tmp_path
assert manager_a._verifier_key != manager_b._verifier_key, (
"setup: the two managers must not share a verifier key"
)
yield manager_a, manager_b
manager_a.close_all_databases()
manager_b.close_all_databases()
def test_manager_a_cache_gives_manager_b_nothing(manager_pair):
"""A's cached engine is invisible to B; B re-proves the credential
against SQLCipher, fails closed on a wrong password, and A's later
close does not disturb B's live engine."""
manager_a, manager_b = manager_pair
if not manager_a.has_encryption:
pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
username = f"multimgr_{uuid.uuid4().hex[:8]}"
password = "MultiManagerPassword1!" # noqa: S105
engine_a = manager_a.create_user_database(username, password)
assert manager_a.is_user_connected(username), "A holds the warm cache"
# B, wrong password: cold attempt against the real file -> rejected,
# and B caches nothing from the failure.
assert manager_b.open_user_database(username, "wrong-for-B") is None
assert username not in manager_b.connections
assert username not in manager_b._password_verifiers
# B, correct password: B builds and caches its OWN engine (distinct
# object from A's), with B's own verifier.
engine_b = manager_b.open_user_database(username, password)
assert engine_b is not None
assert engine_b is not engine_a
assert manager_b.connections[username] is engine_b
assert manager_b._password_matches_cached(username, password) is True
# B fails closed on its own warm cache, exactly like A would.
assert manager_b.open_user_database(username, "wrong-for-B") is None
# A tears its engine down; B's engine and sessions must be unaffected.
manager_a.close_user_database(username)
assert not manager_a.is_user_connected(username)
session_b = manager_b.get_session(username)
assert session_b is not None, (
"A's close invalidated B's live engine -- cache trust and engine "
"lifetime must be per-instance"
)
with session_b:
assert session_b.execute(text("SELECT 1")).scalar() == 1
assert manager_b.connections[username] is engine_b
# And the teardown symmetry: B closing leaves A exactly as A was.
manager_b.close_user_database(username)
assert manager_a.connections == {}
assert manager_a._password_verifiers == {}