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.

6.8 KB
Raw Blame
"""``change_password`` edge cases: same-password, invalid-new, wrong-old.

Incident context (PR #5596): change_password is a credential-taking
write path built from public pieces (close -> open(old) -> PRAGMA rekey
-> evict). Its guards decide whether a bad request can hurt the file.
Round-4 P pinned the rekey's effect on held credentials; this file pins
the INPUT-validation edges:

* AB1 rekey to the SAME password: succeeds (True), the single password
  keeps working cold, data survives, exactly one cache entry after
  reopen (no double caching);
* AB2 invalid NEW password: raises ValueError before touching the
  database, preserving the canary, salt and existing password. Empty,
  whitespace-only and missing passwords must all be rejected, matching
  the validation on create/open.
* AB3 wrong OLD password: clean False (the open(old) inside fails), no
  rekey (old password still opens, the would-be new one still fails),
  canary intact, salt bytes unchanged, nothing cached afterwards.
"""

import hashlib
import uuid

import pytest
from sqlalchemy import text

from local_deep_research.database.encrypted_db import DatabaseManager
from local_deep_research.database.sqlcipher_utils import get_salt_file_path

# 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 _make_user(manager, note):
    username = f"chgpwedge_{uuid.uuid4().hex[:8]}"
    password = "EdgeOldPw1!"  # noqa: S105
    engine = manager.create_user_database(username, password)
    with engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_ab_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_ab_canary (id, note) VALUES (1, :note)"),
            {"note": note},
        )
    manager.close_user_database(username)
    return username, password


def _salt_digest(manager, username) -> str:
    salt_path = get_salt_file_path(manager._get_user_db_path(username))
    return hashlib.sha256(salt_path.read_bytes()).hexdigest()


def test_change_password_to_same_password_is_a_clean_no_op_rekey(manager):
    """AB1: old == new. Returns True, the password keeps working cold,
    data survives, and exactly one engine/verifier entry exists after
    reopen."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username, password = _make_user(manager, "gap-AB1")
    salt_before = _salt_digest(manager, username)

    assert manager.change_password(username, password, password) is True

    # change_password evicts; the (single) password reopens cold.
    assert username not in manager.connections
    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_ab_canary WHERE id = 1")
            ).scalar()
            == "gap-AB1"
        )
    assert manager._password_matches_cached(username, password) is True
    with manager._connections_lock:
        assert set(manager.connections) == set(manager._password_verifiers)
        assert set(manager.connections) == {username}
    # Rekeying to the same key material leaves the salt untouched.
    assert _salt_digest(manager, username) == salt_before


@pytest.mark.parametrize(
    "new_password",
    ["", " \t\n", None],
    ids=["empty", "whitespace", "missing"],
)
def test_change_password_refuses_invalid_new_password(manager, new_password):
    """AB2: reject an invalid key before rekeying; old credentials work cold."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username, password = _make_user(manager, "gap-AB2")
    salt_before = _salt_digest(manager, username)

    with pytest.raises(
        ValueError, match="Invalid encryption key: new password"
    ):
        manager.change_password(username, password, new_password)

    # Reopen cold so a cached connection cannot hide an unwanted rekey.
    assert username not in manager.connections
    assert username not in manager._password_verifiers
    reopened = manager.open_user_database(username, password)
    assert reopened is not None, (
        "the old password no longer opens the database -- the refused "
        "change_password rekeyed anyway"
    )
    with reopened.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_ab_canary WHERE id = 1")
            ).scalar()
            == "gap-AB2"
        )
    # The invalid password must also remain rejected on the open path.
    with pytest.raises(ValueError, match="Invalid encryption key"):
        manager.open_user_database(username, new_password)
    assert _salt_digest(manager, username) == salt_before


def test_change_password_with_wrong_old_password_fails_without_rekey(manager):
    """AB3: wrong current password -> False, no rekey (old still opens,
    would-be new fails), canary intact, salt bytes unchanged, nothing
    cached afterwards."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username, password = _make_user(manager, "gap-AB3")
    new_password = "WouldBeNewPw2!"  # noqa: S105
    salt_before = _salt_digest(manager, username)

    assert (
        manager.change_password(username, "definitely-wrong", new_password)
        is False
    )

    # No rekey happened.
    assert manager.open_user_database(username, new_password) is None
    reopened = manager.open_user_database(username, password)
    assert reopened is not None, "the real password must still open"
    with reopened.connect() as conn:
        assert (
            conn.execute(
                text("SELECT note FROM gap_ab_canary WHERE id = 1")
            ).scalar()
            == "gap-AB3"
        )
    assert manager._password_matches_cached(username, password) is True
    assert manager._password_matches_cached(username, new_password) is False
    assert _salt_digest(manager, username) == salt_before
    with manager._connections_lock:
        assert set(manager.connections) == set(manager._password_verifiers)