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.

10.8 KB
Raw Blame
"""``create_thread_safe_session_for_metrics`` under attack.

Incident context (PR #5596): this method is the SECOND credential-taking
entry point the fix closed. Pre-fix, its cache hit handed a Session bound
to the cached engine to ANY caller presenting the right username with any
password -- the same shape as the login bypass (see its docstring: "a
second instance of exactly the cached-connection password bypass"). The
fix routes a rejected cache hit through ``open_user_database`` so the wrong
password must survive a real key-validating cold open, ending in
``ValueError``.

Existing coverage is single-threaded only:
``test_login_cached_connection_password_extra.py::test_metrics_session_is_
fail_closed_on_wrong_password`` pins one warm wrong attempt and one warm
correct bind. This file adds the incident-shaped depth:

* G1 a storm race through THIS entry point (round 1's gap-C storm hammered
  ``open_user_database`` directly): attacker thread hammering the metrics
  entry with wrong passwords (every call must raise ValueError and never
  return a Session) while the owner thread cycles open(correct) + SELECT +
  close, with the engine/verifier pairing invariant sampled under
  ``_connections_lock`` throughout;
* G2 engine identity + thread affinity + recovery: the metrics Session
  binds to the ONE cached engine (no second engine is built), is genuinely
  usable from a NON-main thread (``check_same_thread=False``), keeps the
  verifier armed; after a close it cold-opens fresh through the same entry
  point, and a wrong password afterwards raises without disturbing the
  cached engine.
"""

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

# Bounded concurrency: 2 threads, explicit iteration caps, one barrier.
HAMMER_ITERATIONS = 12
JOIN_TIMEOUT_S = 60
BARRIER_TIMEOUT_S = 30


@pytest.fixture
def manager(tmp_path, monkeypatch):
    """A DatabaseManager writing to an isolated data directory.

    Same self-contained pattern as the round-1 files, with the sanctioned
    test-mode KDF knob (each wrong-password attempt performs a full PBKDF2
    derivation + verify cycle).
    """
    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_metrics_entry_wrong_password_storm_racing_close_and_reopen(manager):
    """G1: attacker hammers the metrics entry with wrong passwords while
    the owner tears down and rebuilds the real engine.

    Invariants:
    * the attacker NEVER receives a Session (every call raises ValueError;
      any other outcome or exception type is recorded and fails the test);
    * the owner's open(correct) always returns an engine whose SELECT reads
      the canary row written at setup;
    * every lock-consistent snapshot shows ``connections`` and
      ``_password_verifiers`` keyed identically (atomic publication /
      eviction);
    * quiesced: neither engine nor verifier remains after the final close.
    """
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"metrics_storm_{uuid.uuid4().hex[:8]}"
    password = "MetricsStormPassword1!"  # noqa: S105
    engine = manager.create_user_database(username, password)
    with engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_g_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_g_canary (id, note) VALUES (1, :note)"),
            {"note": "gap-G"},
        )

    barrier = threading.Barrier(2)
    errors = {}
    attacker_violations = []
    owner_failures = []
    pairing_violations = []

    def attacker():
        try:
            barrier.wait(timeout=BARRIER_TIMEOUT_S)
            for i in range(HAMMER_ITERATIONS):
                try:
                    session = manager.create_thread_safe_session_for_metrics(
                        username, f"metrics-wrong-{i}"
                    )
                except ValueError:
                    continue  # the documented fail-closed contract
                attacker_violations.append(
                    f"iteration {i}: metrics entry returned {session!r} "
                    "for a wrong password"
                )
                session.close()
        except Exception as exc:  # noqa: BLE001 - surfaced via `errors` below
            errors["attacker"] = exc

    def owner():
        try:
            barrier.wait(timeout=BARRIER_TIMEOUT_S)
            for i in range(HAMMER_ITERATIONS):
                engine = manager.open_user_database(username, password)
                if engine is None:
                    owner_failures.append(
                        f"iteration {i}: open(correct) returned None"
                    )
                    continue
                try:
                    with engine.connect() as conn:
                        note = conn.execute(
                            text("SELECT note FROM gap_g_canary WHERE id = 1")
                        ).scalar()
                    if note != "gap-G":
                        owner_failures.append(
                            f"iteration {i}: canary read {note!r}"
                        )
                except Exception as exc:  # noqa: BLE001 - loud assert below
                    owner_failures.append(
                        f"iteration {i}: SELECT failed: {exc!r}"
                    )
                manager.close_user_database(username)
                with manager._connections_lock:
                    engines = set(manager.connections)
                    verifiers = set(manager._password_verifiers)
                if engines != verifiers:
                    pairing_violations.append(
                        f"iteration {i}: engines={engines} "
                        f"verifiers={verifiers}"
                    )
        except Exception as exc:  # noqa: BLE001 - surfaced via `errors` below
            errors["owner"] = exc

    threads = [
        threading.Thread(target=attacker, name="gap-g-attacker"),
        threading.Thread(target=owner, name="gap-g-owner"),
    ]
    for t in threads:
        t.start()
    for t in threads:
        t.join(timeout=JOIN_TIMEOUT_S)
    assert not any(t.is_alive() for t in threads), "storm deadlocked"
    assert not errors, f"a storm thread raised: {errors}"
    assert not attacker_violations, (
        f"metrics entry handed out sessions for wrong passwords: "
        f"{attacker_violations}"
    )
    assert not owner_failures, (
        f"legitimate metrics-path opens failed: {owner_failures}"
    )
    assert not pairing_violations, (
        f"engine/verifier pairing broke: {pairing_violations}"
    )

    with manager._connections_lock:
        assert username not in manager.connections
        assert username not in manager._password_verifiers


def test_metrics_session_identity_thread_affinity_and_recovery(manager):
    """G2: warm cache -- the metrics Session binds to the cached engine
    object (no second engine), used from a NON-main thread; the verifier
    still matches afterwards. After a close, the same entry point
    cold-opens a fresh engine that works; a wrong password then raises
    ValueError and leaves the cached engine and verifier intact.
    """
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    username = f"metrics_id_{uuid.uuid4().hex[:8]}"
    password = "MetricsIdentityPw1!"  # noqa: S105
    warm_engine = manager.create_user_database(username, password)
    with warm_engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE gap_g2_canary (id INTEGER PRIMARY KEY, note TEXT)"
            )
        )
        conn.execute(
            text("INSERT INTO gap_g2_canary (id, note) VALUES (1, :note)"),
            {"note": "gap-G2"},
        )

    outcome = {}
    errors = {}

    def background_user():
        # The whole point of this entry point: background threads. The
        # engine is built with check_same_thread=False, so a Session bound
        # to it must be usable off the main thread.
        try:
            session = manager.create_thread_safe_session_for_metrics(
                username, password
            )
            cached = manager.connections.get(username)
            outcome["bind_is_cached_engine"] = session.bind is cached
            outcome["cached_is_warm_engine"] = cached is warm_engine
            outcome["select"] = session.execute(text("SELECT 1")).scalar()
            outcome["canary"] = session.execute(
                text("SELECT note FROM gap_g2_canary WHERE id = 1")
            ).scalar()
            session.close()
        except Exception as exc:  # noqa: BLE001 - surfaced via `errors` below
            errors["background"] = exc

    worker = threading.Thread(target=background_user, name="gap-g2-worker")
    worker.start()
    worker.join(timeout=JOIN_TIMEOUT_S)
    assert not worker.is_alive(), "metrics background thread hung"
    assert not errors, f"background thread raised: {errors}"
    assert outcome["bind_is_cached_engine"] is True, (
        "the metrics Session must bind to the cached engine, not a second one"
    )
    assert outcome["cached_is_warm_engine"] is True
    assert outcome["select"] == 1
    assert outcome["canary"] == "gap-G2"
    assert manager._password_matches_cached(username, password) is True

    # Close, then cold-open through the SAME entry point.
    manager.close_user_database(username)
    cold_session = manager.create_thread_safe_session_for_metrics(
        username, password
    )
    try:
        assert cold_session.bind is manager.connections[username]
        assert cold_session.execute(text("SELECT 1")).scalar() == 1
    finally:
        cold_session.close()

    # Wrong password through the metrics entry: ValueError, and the cached
    # engine/verifier for the correct password survive undisturbed.
    cached_engine = manager.connections[username]
    with pytest.raises(ValueError):
        manager.create_thread_safe_session_for_metrics(
            username, "metrics-wrong-after-cold"
        )
    assert manager.connections[username] is cached_engine, (
        "a rejected metrics attempt must not disturb the cached engine"
    )
    assert manager._password_matches_cached(username, password) is True
    assert (
        manager._password_matches_cached(username, "metrics-wrong-after-cold")
        is False
    )