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.

fix(security): redact + break chain on DatabaseInitializationError re-raise (#4182) (#4542)

LearningCircuit24d03d191926
diff --git a/changelog.d/+db-init-error-redaction.security.md b/changelog.d/+db-init-error-redaction.security.md
new file mode 100644
index 0000000000..478481a263
--- /dev/null
+++ b/changelog.d/+db-init-error-redaction.security.md
@@ -0,0 +1 @@
+Closed a residual SQLCipher-password leak left after the #4182 sweep: `open_user_database` redacted the migration-failure log line but still re-raised `DatabaseInitializationError` with the unredacted original error embedded in its message and chained via `from init_err`. The caller (`thread_local_session`) logs that typed error with `logger.exception`, which re-rendered the chain — and the password its frame locals carry under `diagnose=True` — defeating the redaction. The typed error now carries the redacted message and breaks the chain with `from None` (ADR-0003). See #4182.
diff --git a/src/local_deep_research/database/encrypted_db.py b/src/local_deep_research/database/encrypted_db.py
index f0d8d0e5a2..2d27a86cec 100644
--- a/src/local_deep_research/database/encrypted_db.py
+++ b/src/local_deep_research/database/encrypted_db.py
@@ -722,9 +722,17 @@ class DatabaseManager:
f"after {elapsed_ms:.0f}ms — refusing login: {safe_msg}"
)
engine.dispose()
+ # Sanitised message + ``from None``: ``init_err`` (its
+ # str() and its chained traceback frames) can carry the
+ # plaintext password, and the caller that catches this
+ # typed error logs it with ``logger.exception``
+ # (thread_local_session) — which would render the chain
+ # and defeat the redaction applied just above. Break the
+ # chain per ADR-0003; the redacted detail is already
+ # logged at this site.
raise DatabaseInitializationError(
- f"Database initialisation failed for {username}: {init_err}"
- ) from init_err
+ f"Database initialisation failed for {username}: {safe_msg}"
+ ) from None
# Store connection AFTER migrations complete
with self._connections_lock:
diff --git a/tests/security/test_password_leakage.py b/tests/security/test_password_leakage.py
index 7925f486fb..78334ff151 100644
--- a/tests/security/test_password_leakage.py
+++ b/tests/security/test_password_leakage.py
@@ -177,6 +177,81 @@ class TestEncryptedDBPasswordLeakage:
)
assert "Failed to open database" in loguru_caplog_full.text
+ def test_open_user_database_migration_failure_sanitizes_typed_error(
+ self, loguru_caplog_full, tmp_path
+ ):
+ """A migration failure re-raises as ``DatabaseInitializationError``.
+ The typed error must carry a redacted message and no exception
+ chain (``from None``, ADR-0003): the caller
+ (``thread_local_session``) logs it with ``logger.exception``,
+ which would otherwise render the original exception (and its
+ frame locals holding the password) in full — defeating the
+ redaction applied at the raise site.
+ """
+ from local_deep_research.database.encrypted_db import (
+ DatabaseInitializationError,
+ DatabaseManager,
+ )
+
+ manager = DatabaseManager.__new__(DatabaseManager)
+ manager.has_encryption = True
+ manager.connections = {}
+ manager._connections_lock = MagicMock()
+ manager._connections_lock.__enter__ = lambda *a: None
+ manager._connections_lock.__exit__ = lambda *a: None
+ manager._pool_class = type("StubPool", (), {})
+ manager._get_pool_kwargs = lambda: {}
+ db_path = tmp_path / "alice.db"
+ db_path.touch()
+ manager._get_user_db_path = lambda u: db_path
+
+ # The SELECT 1 connection check passes; the migration step then
+ # raises with the password embedded (simulating an
+ # OperationalError that carries frame locals).
+ exc = RuntimeError(
+ f"migration failed: dsn=sqlite:///x?key={_LEAKED_PASSWORD}"
+ )
+
+ with loguru_caplog_full.at_level("DEBUG"):
+ with patch(
+ "local_deep_research.database.encrypted_db.create_engine",
+ return_value=MagicMock(),
+ ):
+ with patch(
+ "local_deep_research.database.encrypted_db.has_per_database_salt",
+ return_value=True,
+ ):
+ with patch(
+ "local_deep_research.database.alembic_runner.needs_migration",
+ return_value=False,
+ ):
+ with patch(
+ "local_deep_research.database.initialize.initialize_database",
+ side_effect=exc,
+ ):
+ with pytest.raises(
+ DatabaseInitializationError
+ ) as excinfo:
+ manager.open_user_database(
+ "alice", _LEAKED_PASSWORD
+ )
+
+ # The typed error's own message must be redacted, and the chain
+ # broken so a downstream logger.exception can't render init_err.
+ _assert_no_leak(
+ str(excinfo.value),
+ _LEAKED_PASSWORD,
+ "DatabaseInitializationError message",
+ )
+ assert excinfo.value.__cause__ is None
+ assert excinfo.value.__suppress_context__ is True
+ _assert_no_leak(
+ loguru_caplog_full.text,
+ _LEAKED_PASSWORD,
+ "open_user_database migration catch",
+ )
+ assert "Database migration failed" in loguru_caplog_full.text
+
# ---------------------------------------------------------------------------
# web/queue/processor_v2.py