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.

perf: defer PBKDF2 key derivation until engine cache miss in metrics path (#3378)

LearningCircuit1703e38b186e
diff --git a/src/local_deep_research/database/encrypted_db.py b/src/local_deep_research/database/encrypted_db.py
index e4d536df78..5d7cc054ef 100644
--- a/src/local_deep_research/database/encrypted_db.py
+++ b/src/local_deep_research/database/encrypted_db.py
@@ -889,12 +889,6 @@ class DatabaseManager:
"""
db_path = self._get_user_db_path(username)
- # Prevent timing attacks: always derive key before checking file existence
- # This ensures both existing and non-existent users take the same amount of time,
- # preventing username enumeration via timing analysis.
- # Pre-derive key before closures to avoid capturing plaintext password
- hex_key = get_key_from_password(password, db_path=db_path).hex()
-
if not db_path.exists():
raise ValueError(f"No database found for user {username}")
@@ -935,6 +929,12 @@ class DatabaseManager:
)
del self._thread_engines[engine_key]
+ # Only derive key when creating a new engine (cache miss).
+ # This method is only called for already-authenticated users in
+ # background threads, so timing-attack resistance is not needed here
+ # (unlike open_user_database which handles login).
+ hex_key = get_key_from_password(password, db_path=db_path).hex()
+
# Create a thread-local engine
if self.has_encryption:
diff --git a/tests/database/test_encrypted_db_manager.py b/tests/database/test_encrypted_db_manager.py
index d32029e991..a37054f1bc 100644
--- a/tests/database/test_encrypted_db_manager.py
+++ b/tests/database/test_encrypted_db_manager.py
@@ -1436,10 +1436,16 @@ class TestTimingAttackPrevention:
mock_derive.assert_any_call("password123", db_path=db_path)
@patch("local_deep_research.database.encrypted_db.get_data_directory")
- def test_metrics_session_derives_key_before_file_check(
+ def test_metrics_session_skips_key_derivation_for_nonexistent_db(
self, mock_data_dir, tmp_path
):
- """create_thread_safe_session_for_metrics derives key before db check."""
+ """create_thread_safe_session_for_metrics skips key derivation when db missing.
+
+ Unlike open_user_database (the login path), this method does not need
+ timing-attack resistance because it is only called for already-authenticated
+ users in background threads. It should fail fast without wasting ~0.5s on
+ PBKDF2 when the database file does not exist.
+ """
from local_deep_research.database.encrypted_db import DatabaseManager
mock_data_dir.return_value = tmp_path
@@ -1459,7 +1465,7 @@ class TestTimingAttackPrevention:
"nonexistent_user", "password123"
)
- mock_derive.assert_called_once()
+ mock_derive.assert_not_called()
@patch("local_deep_research.database.encrypted_db.get_data_directory")
def test_salt_warning_not_called_for_nonexistent_db(
@@ -1475,16 +1481,9 @@ class TestTimingAttackPrevention:
):
manager = DatabaseManager()
- with (
- patch(
- "local_deep_research.database.encrypted_db.get_key_from_password"
- ) as mock_derive,
- patch(
- "local_deep_research.database.encrypted_db.has_per_database_salt"
- ) as mock_salt,
- ):
- mock_derive.return_value = b"\x00" * 32
-
+ with patch(
+ "local_deep_research.database.encrypted_db.has_per_database_salt"
+ ) as mock_salt:
with pytest.raises(ValueError, match="No database found"):
manager.create_thread_safe_session_for_metrics(
"nonexistent_user", "password123"