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.

5.4 KB
Raw Blame
"""Verifier memory hygiene: what the cache actually stores.

The password-verifier cache stores keyed HMAC digests instead of plaintext
passwords. These tests inspect that cache's tuple shape, absence of a known
password's byte fragments, and matching behavior at the real manager level.
They do not establish secrecy of a whole-process memory snapshot: usable
SQLAlchemy engine creators retain derived database keys. Nor does checking
for byte substrings prove that arbitrary reversible encodings are absent.

* AD1 ``_verifier_key`` is exactly 32 bytes; the stored ``(salt, digest)``
  entry has the expected lengths, contains no tested password fragment,
  and accepts the correct password while rejecting a wrong one.
* AD2 two live managers have different keys, and manager A's verifier
  tuple does not validate under manager B (one short manager-level
  companion to round-1 E's real-DB isolation and the primitive test's
  cross-key check).
"""

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(tmp_path, monkeypatch):
    """A DatabaseManager writing to an isolated data directory.

    Same self-contained pattern as the round-1..5 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 test_verifier_stores_no_password_material(manager):
    """AD1: check verifier lengths, known plaintext fragments and matching.

    The substring checks detect direct copies of the fixture password's
    bytes; they make no claim about whole-process memory or encodings.
    """
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    password = "XyZ__5000_End-of-Password"  # noqa: S105
    username = f"vmem_{uuid.uuid4().hex[:8]}"
    engine = manager.create_user_database(username, password)
    with engine.begin() as conn:
        conn.execute(text("SELECT 1"))
    manager.close_user_database(username)
    manager.open_user_database(username, password)

    assert isinstance(manager._verifier_key, bytes)
    assert len(manager._verifier_key) == 32, (
        "the verifier key must be the full 32 random bytes generated in "
        "__init__"
    )

    entry = manager._password_verifiers[username]
    assert isinstance(entry, tuple) and len(entry) == 2
    salt, digest = entry
    assert isinstance(salt, bytes) and len(salt) == 16
    assert isinstance(digest, bytes) and len(digest) == 32

    # The password's bytes (whole and every substring window >= 4 bytes)
    # must appear in NEITHER half.
    pw_bytes = password.encode("utf-8")
    windows = {
        pw_bytes[i:j]
        for i in range(len(pw_bytes))
        for j in range(i + 4, len(pw_bytes) + 1)
    }
    for window in windows:
        assert window not in salt, (
            f"password fragment {window!r} found in the verifier salt"
        )
        assert window not in digest, (
            f"password fragment {window!r} found in the verifier digest"
        )
    # Whole-password absence (the headline property, also explicit):
    assert pw_bytes not in salt and pw_bytes not in digest

    # And the verifier still does its job for the real password.
    assert manager._password_matches_cached(username, password) is True
    assert (
        manager._password_matches_cached(username, "not-the-password") is False
    )


def test_verifier_tuples_do_not_cross_validate_between_managers(
    tmp_path, monkeypatch
):
    """AD2: two live managers derive different keys; A's stored tuple,
    transplanted into B's map, does not validate under B (the digest is
    keyed by A's per-process random key)."""
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")

    manager_a = DatabaseManager()
    manager_b = DatabaseManager()
    if not manager_a.has_encryption:
        manager_a.close_all_databases()
        manager_b.close_all_databases()
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
    try:
        manager_a.data_dir = tmp_path
        manager_b.data_dir = tmp_path
        assert manager_a._verifier_key != manager_b._verifier_key

        password = "CrossKeyCheckPw1!"  # noqa: S105
        username = f"xkey_{uuid.uuid4().hex[:8]}"
        manager_a.create_user_database(username, password)
        a_entry = manager_a._password_verifiers[username]

        # Transplant A's tuple into B's map: it must NOT validate there.
        manager_b._password_verifiers[username] = a_entry
        with manager_b._connections_lock:
            matches = manager_b._verifier_matches(username, password)
        assert matches is False, (
            "a verifier recorded under manager A's key validated under "
            "manager B -- the digest is not actually keyed per process"
        )
    finally:
        manager_a.close_all_databases()
        manager_b.close_all_databases()