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.

8.2 KB
Raw Blame
"""Unencrypted fallback mode: the documented plaintext risk surface.

Incident context (PR #5596): the verifier machinery only means something
when there IS a credential to verify. In unencrypted mode
(``has_encryption == False``) the code DOCUMENTS that the cold path opens
the plain-SQLite database with any password and that the cache hit trusts
directly (``_cached_engine_trusted`` short-circuits; see its docstring
and ``test_password_verifier_primitive.py``). This file pins that whole
surface as ACTUAL, DOCUMENTED behavior -- not as a bypass -- by forcing
``has_encryption = False`` in a process that has sqlcipher3 installed
(monkeypatching ``DatabaseManager._check_encryption_available`` before
construction, per the round-5 instructions):

* U1 the full plaintext flow: create -> engine; the .db file is readable
  with VANILLA sqlite3 (no key) -- the at-rest risk, pinned; any password
  opens; change_password returns False with its "Cannot change password"
  warning (encrypted_db.py::change_password); and get_session works;
* U2 ``check_database_integrity`` on a HEALTHY unencrypted database
  must report True. It reported False until #6376: ``PRAGMA
  cipher_integrity_check`` on a plain-SQLite connection returned a closed
  result whose iteration raised ResourceClosedError, which the method's
  bare ``except`` swallowed into False. The method now skips the
  SQLCipher-only pragma when ``has_encryption`` is False, so this is a
  plain regression test.

* U3 a COLD open (no cached engine) with a different nonblank password
  succeeds, as docs/developing.md#unencrypted-development-fallback
  states: the fallback ``sqlite:///`` path never uses the derived key.

A "wrong password against a warm cache succeeds" test was dropped: it
duplicated ``test_login_cached_connection_password_extra.py``'s
``test_unencrypted_mode_reuses_cached_engine``.

``tests/database/test_sqlcipher_missing.py`` covers the import-level
RuntimeError / env-var gating of entering this mode; this file covers the
manager-level behavior once inside it. Each test builds its own manager
with the patch in effect (U3) and closes it in teardown.
"""

import sqlite3
import uuid

import pytest
from sqlalchemy import text

from local_deep_research.database.encrypted_db import DatabaseManager


@pytest.fixture
def unenc_manager(tmp_path, monkeypatch):
    """An UNENCRYPTED-mode DatabaseManager in a process that has
    sqlcipher3: the availability probe is patched to return False before
    ``__init__`` runs, so the constructor takes the documented fallback
    branch without needing the env-var workaround."""
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
    monkeypatch.setattr(
        DatabaseManager,
        "_check_encryption_available",
        lambda self: False,
    )
    mgr = DatabaseManager()
    assert mgr.has_encryption is False, (
        "setup: the probe patch did not force unencrypted mode"
    )
    mgr.data_dir = tmp_path
    yield mgr
    mgr.close_all_databases()


def test_unencrypted_mode_full_flow_is_plaintext_and_passwordless(
    unenc_manager, loguru_caplog_full
):
    """U1: end-to-end documented behavior of the fallback mode."""
    username = f"unenc_{uuid.uuid4().hex[:8]}"
    password = "UnencryptedModePw1!"  # noqa: S105
    engine = unenc_manager.create_user_database(username, password)
    assert engine is not None
    with engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_u_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_u_canary (id, note) VALUES (1, :note)"),
            {"note": "gap-U"},
        )

    # THE at-rest risk, pinned: the file is readable with vanilla sqlite3,
    # no key, no pragmas.
    db_path = unenc_manager._get_user_db_path(username)
    plain = sqlite3.connect(str(db_path))
    try:
        rows = plain.execute(
            "SELECT note FROM gap_u_canary WHERE id = 1"
        ).fetchall()
    finally:
        plain.close()
    assert rows == [("gap-U",)], (
        "the unencrypted-mode database was not plaintext-readable with a "
        "vanilla sqlite3 connection"
    )

    # close disposes the engine; the file remains plaintext-readable.
    unenc_manager.close_user_database(username)
    plain = sqlite3.connect(str(db_path))
    try:
        assert plain.execute(
            "SELECT note FROM gap_u_canary WHERE id = 1"
        ).fetchall() == [("gap-U",)]
    finally:
        plain.close()

    # ANY password opens (documented: no credential exists to check).
    reopened = unenc_manager.open_user_database(username, password)
    assert reopened is not None
    assert (
        unenc_manager.open_user_database(username, "totally-different")
        is reopened
    )

    # The cache trusts directly in this mode (documented short-circuit);
    # the epoch must match the durable projection for the trust to engage.
    with unenc_manager._connections_lock:
        current_epoch = unenc_manager._connection_epochs.get(username)
        assert current_epoch is not None
        assert (
            unenc_manager._cached_engine_trusted(
                username, "any-string", current_epoch
            )
            is True
        )

    # get_session works and reads data.
    session = unenc_manager.get_session(username)
    assert session is not None
    with session:
        assert (
            session.execute(
                text("SELECT note FROM gap_u_canary WHERE id = 1")
            ).scalar()
            == "gap-U"
        )

    # change_password: documented False + warning
    # (encrypted_db.py::change_password).
    with loguru_caplog_full.at_level("DEBUG"):
        changed = unenc_manager.change_password(
            username,
            password,
            "AnotherPassword2!",  # noqa: S105
        )
    assert changed is False, (
        "change_password must refuse to run without SQLCipher"
    )
    assert "Cannot change password" in loguru_caplog_full.text


def test_check_database_integrity_reports_healthy_unencrypted_database(
    unenc_manager,
):
    """U2: a healthy database must pass its integrity check in EVERY mode.

    Unencrypted mode has no HMACs to verify, so the correct answer for a
    freshly created, readable, uncorrupted database is True -- otherwise
    every caller that gates on this method treats a perfectly healthy
    deployment as corrupt.
    """
    username = f"unencint_{uuid.uuid4().hex[:8]}"
    engine = unenc_manager.create_user_database(username, "UnencIntegrityPw1!")  # noqa: S105
    with engine.begin() as conn:
        conn.execute(
            text("CREATE TABLE gap_u_int (id INTEGER PRIMARY KEY, note TEXT)")
        )
        conn.execute(text("INSERT INTO gap_u_int VALUES (1, 'gap-U2')"))
    assert unenc_manager.is_user_connected(username), (
        "setup: the engine must be warm for the accessor to inspect it"
    )

    assert unenc_manager.check_database_integrity(username) is True


def test_cold_open_with_different_password_succeeds(unenc_manager):
    """U3: no credential check on a fresh open in the fallback.

    U1 only uses a different password on a cache hit. Here the engine is
    closed first, so the different password goes through the cold path.
    """
    username = f"unenccold_{uuid.uuid4().hex[:8]}"
    engine = unenc_manager.create_user_database(username, "ColdOpenPw1!")  # noqa: S105
    with engine.begin() as conn:
        conn.execute(
            text("CREATE TABLE gap_u_cold (id INTEGER PRIMARY KEY, note TEXT)")
        )
        conn.execute(text("INSERT INTO gap_u_cold VALUES (1, 'gap-U3')"))
    unenc_manager.close_user_database(username)
    assert not unenc_manager.is_user_connected(username), (
        "setup: the cold path needs no cached engine"
    )

    reopened = unenc_manager.open_user_database(
        username,
        "an-entirely-different-password",  # noqa: S106
    )
    assert reopened is not None, (
        "the fallback cold open rejected a different nonblank password; "
        "update docs/developing.md#unencrypted-development-fallback"
    )
    with reopened.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_u_cold WHERE id = 1")
            ).scalar()
            == "gap-U3"
        )