~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Log hygiene and auditability of failed credential attempts.
Incident context (PR #5596): the original cached-connection bypass was
invisible -- every attack "succeeded", so it cleared the lockout counter,
matched nothing anomalous in the logs, and left no trace. Two opposite
requirements follow for the fixed failure paths, both pinned here at the
real-SQLCipher level using the repo's canonical ``loguru_caplog_full``
fixture (the package disables its own loguru logging by default; the
fixture re-enables it and captures message + rendered exception block):
* I1 SECRECY -- a NEGATIVE property, deliberately scoped: no presented
password, no derived hex key and no verifier salt/digest appears in any
record captured while running (a) warm wrong-password opens, (b) cold
wrong-password opens, (c) failed metrics sessions and (d) a failed
change_password. It pins the observable outcome of those four paths and
nothing more. In particular it is NOT a test of ``redact_secrets``: on
all four paths the plaintext password never reaches ``str(e)`` in the
first place (the engine creator closure is handed only the pre-derived
``hex_key``), and the traceback half is unobservable here because
``loguru_caplog_full`` fixes ``diagnose=False`` to match production. I1
would therefore still hold with the redaction call deleted -- the
redaction helpers are covered by ``test_password_leakage.py`` /
``test_credential_leak_followup.py``, and the rekey statement's own
scrubbing by ``test_sqlcipher_rekey_key_not_leaked.py``. Path (d) is
kept for the credential-bearing frames it drives under capture, NOT as
coverage of ``change_password``'s handler, which it cannot reach: with
a wrong old password ``open_user_database`` returns None and
``change_password`` returns False before the ``try`` body can raise.
A positive control asserts that a package-originated record --
``_open_user_database_cold``'s own failure warning, which names the
target username, the same shape I2 below pins -- IS present in the
capture block, so the "not in captured" assertions cannot pass against
a capture that recorded nothing at all.
* I2 AUDITABILITY: a failed attempt must not be SILENT either -- the
cold-open failure path logs ``Failed to open database for user
{username} ...`` at warning level, so every failed warm-cache
wrong-password attempt leaves at least one record naming the target
account. (Anti-invisibility regression guard: the incident's attacks
produced zero failure records.)
The password-leakage unit tests in test_password_leakage.py /
test_credential_leak_followup.py cover the redaction helpers with mocked
databases; this file exercises the real database paths end to end and also
asserts the derived-key and verifier-material never reach the capture.
"""
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_key_from_password
# 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 files, with the sanctioned
test-mode KDF knob (so in-test key derivation for the hex-leak oracle
is cheap).
"""
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_failed_credential_attempts_never_leak_secrets_into_logs(
manager, loguru_caplog_full
):
"""I1: across warm opens, cold opens, metrics sessions and a failed
change_password, no captured record contains any presented password,
any derived hex key, or the verifier salt/digest.
Negative property only -- see the module docstring for what this does
and does not pin. The positive control below is what keeps it from
passing vacuously.
"""
if not manager.has_encryption:
pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
username = f"leakcheck_{uuid.uuid4().hex[:8]}"
correct = f"R2Correct-Zebra-{uuid.uuid4().hex[:10]}" # noqa: S105
wrong_warm = f"R2WarmWrong-Tiger-{uuid.uuid4().hex[:10]}" # noqa: S105
wrong_cold = f"R2ColdWrong-Puma-{uuid.uuid4().hex[:10]}" # noqa: S105
wrong_metrics = f"R2MetricsWrong-Lynx-{uuid.uuid4().hex[:10]}" # noqa: S105
wrong_old = f"R2OldWrong-Bear-{uuid.uuid4().hex[:10]}" # noqa: S105
new_password = f"R2New-Panda-{uuid.uuid4().hex[:10]}" # noqa: S105
engine = manager.create_user_database(username, correct)
with engine.begin() as conn:
conn.execute(
text(
"CREATE TABLE gap_i_canary (id INTEGER PRIMARY KEY, note TEXT)"
)
)
conn.execute(
text("INSERT INTO gap_i_canary (id, note) VALUES (1, :note)"),
{"note": "gap-I"},
)
# Capture the verifier material while the engine is warm; it must never
# surface in any captured record later.
salt, digest = manager._password_verifiers[username]
db_path = manager._get_user_db_path(username)
hex_keys = [
get_key_from_password(pw, db_path=db_path).hex()
for pw in (correct, wrong_warm, wrong_cold, new_password)
]
with loguru_caplog_full.at_level("DEBUG"):
# (a) warm-cache wrong-password open
assert manager.open_user_database(username, wrong_warm) is None
# (b) cold wrong-password open
manager.close_user_database(username)
assert manager.open_user_database(username, wrong_cold) is None
# (c) failed metrics session (cache is cold; falls through to open)
with pytest.raises(ValueError):
manager.create_thread_safe_session_for_metrics(
username, wrong_metrics
)
# (d) failed change_password (wrong current password). This
# returns False from the early `if not engine` inside the try --
# it never reaches change_password's own except handler. It is
# here for the credential-bearing frames it drives under capture,
# not as coverage of that handler.
assert (
manager.change_password(username, wrong_old, new_password) is False
)
captured = loguru_caplog_full.text
assert captured, "capture sanity: no records captured at all"
assert username in captured, (
"positive control missing: `_open_user_database_cold`'s own "
"failure warning (which names the target username) did not reach "
"`captured`, so the absence assertions below prove nothing"
)
for secret in (
correct,
wrong_warm,
wrong_cold,
wrong_metrics,
wrong_old,
new_password,
):
assert secret not in captured, (
f"presented password leaked into a log record: {secret!r}"
)
for hex_key in hex_keys:
assert hex_key not in captured, (
"a derived SQLCipher hex key leaked into logs"
)
assert salt.hex() not in captured, "verifier salt leaked into logs"
assert digest.hex() not in captured, "verifier digest leaked into logs"
# Control: the account still works after all the failed attempts.
reopened = manager.open_user_database(username, correct)
assert reopened is not None
with reopened.connect() as conn:
assert (
conn.execute(
text("SELECT note FROM gap_i_canary WHERE id = 1")
).scalar()
== "gap-I"
)
def test_failed_warm_wrong_password_attempt_is_logged_with_username(
manager, loguru_caplog_full
):
"""I2 (anti-invisibility): the incident's attacks produced NO failure
records (they counted as successes). Today every failed warm-cache
wrong-password attempt falls through to a real cold open whose failure
is logged naming the target username -- pin that, so a regression back
to silent acceptance cannot hide.
Verified against the current code first: ``_open_user_database_cold``'s
failure handler emits ``logger.warning(f"Failed to open database for
user {username} after ...")``, and the no-file path emits
``logger.error(f"No database found for user {username}")`` -- both
name the account, so the assertion below holds today.
"""
if not manager.has_encryption:
pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")
username = f"audit_{uuid.uuid4().hex[:8]}"
password = f"AuditOwl-{uuid.uuid4().hex[:10]}" # noqa: S105
manager.create_user_database(username, password)
with loguru_caplog_full.at_level("DEBUG"):
for i in range(3):
loguru_caplog_full.clear()
assert (
manager.open_user_database(username, f"audit-wrong-{i}") is None
)
captured = loguru_caplog_full.text
assert username in captured, (
f"failed warm-cache attempt #{i} left no log record naming "
"the target account -- a silent-acceptance regression would "
"be invisible again (the exact property that made the "
"original incident undetectable)"
)
# And the records it did leave must not carry the credential.
assert f"audit-wrong-{i}" not in captured, (
f"attempt #{i}'s wrong password appears in a log record"
)