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.

4.0 KB
Raw Blame
"""PRAGMA- and SQL-hostile passwords through the real key path.

Incident context (PR #5596): the connection layer never interpolates a
password into SQL. ``get_key_from_password``
(sqlcipher_utils.py::get_key_from_password) derives raw bytes with
PBKDF2 and the key reaches SQLCipher as a hex literal
(``PRAGMA key = "x'...'"`` / ``PRAGMA rekey``), so a password
that LOOKS like SQL or a PRAGMA is just key material. This file pins that
end-to-end for a battery of hostile strings, each as a REAL database:

* create with the hostile password -> canary write;
* warm open with the SAME hostile password reuses the cached engine
  (verifier matches -- the HMAC covers the exact bytes);
* a wrong-password sibling fails closed, warm and cold;
* close + cold reopen with the hostile password works and reads the
  canary.

Complements test_password_verifier_primitive.py (digest-level
distinctness for unusual strings): this is the full create/open/verify
cycle against real SQLCipher files. The KDF-1000 knob keeps the battery
fast.
"""

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

HOSTILE_PASSWORDS = [
    "sqlite_master",
    "key='x'",
    "x'; DROP TABLE users;--",
    "PRAGMA cipher_integrity_check",
    "M\\' OR 1=1--",
    "%s%s%s%s",
    '{"n":1}',
]


@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()


@pytest.mark.parametrize("password", HOSTILE_PASSWORDS)
def test_hostile_password_is_pure_key_material(manager, password):
    """A password that reads as SQL / a PRAGMA / a format string must
    behave as ordinary key material: create, warm reuse, fail-closed
    sibling, cold reopen -- no interpretation anywhere in the chain."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"hostile_{uuid.uuid4().hex[:8]}"
    wrong = password + "-wrong"

    engine = manager.create_user_database(username, password)
    assert engine is not None
    with engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_y_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_y_canary (id, note) VALUES (1, :note)"),
            {"note": password},  # the hostile string round-trips as data too
        )

    # Warm: the same hostile password hits the verifier and reuses the
    # engine (verifier-mismatch fail-closure for wrong passwords is
    # pinned extensively in rounds 1-4; Y's unique surface is the hostile
    # string surviving the full key path, checked cold below).
    assert manager.open_user_database(username, password) is engine
    assert manager._password_matches_cached(username, password) is True

    # Cold: close, then the hostile password reopens and the sibling fails.
    manager.close_user_database(username)
    assert manager.open_user_database(username, wrong) is None
    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_y_canary WHERE id = 1")
            ).scalar()
            == password
        ), "the hostile password must round-trip as plain data"