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: clarify security-sensitive code patterns in database layer

LearningCircuit1215916a3cde
diff --git a/src/local_deep_research/database/session_context.py b/src/local_deep_research/database/session_context.py
index fc02af260c..099595c0a3 100644
--- a/src/local_deep_research/database/session_context.py
+++ b/src/local_deep_research/database/session_context.py
@@ -14,6 +14,10 @@ from sqlalchemy.orm import Session
from ..utilities.thread_context import get_search_context
from .encrypted_db import db_manager
+# Placeholder password used when accessing unencrypted databases.
+# This should only be used when LDR_ALLOW_UNENCRYPTED=true is set.
+UNENCRYPTED_DB_PLACEHOLDER = "unencrypted-mode"
+
class DatabaseSessionError(Exception):
"""Raised when database session cannot be established."""
@@ -100,7 +104,11 @@ def get_user_db_session(
f"Encrypted database for {username} requires password"
)
elif not password:
- password = "dummy" # For unencrypted databases
+ logger.warning(
+ f"Accessing unencrypted database for {username} - "
+ "ensure this is intentional (LDR_ALLOW_UNENCRYPTED=true)"
+ )
+ password = UNENCRYPTED_DB_PLACEHOLDER
# Use thread-local session (will reuse existing or create new)
session = get_metrics_session(username, password)
@@ -184,7 +192,13 @@ def ensure_db_session(view_func: Callable) -> Callable:
return redirect(url_for("auth.login"))
else:
# Try to reopen unencrypted database
- engine = db_manager.open_user_database(username, "dummy")
+ logger.warning(
+ f"Reopening unencrypted database for {username} - "
+ "ensure this is intentional"
+ )
+ engine = db_manager.open_user_database(
+ username, UNENCRYPTED_DB_PLACEHOLDER
+ )
if engine:
g.db_session = db_manager.get_session(username)
diff --git a/src/local_deep_research/database/sqlcipher_utils.py b/src/local_deep_research/database/sqlcipher_utils.py
index 7d1a08c17c..4b500bfc79 100644
--- a/src/local_deep_research/database/sqlcipher_utils.py
+++ b/src/local_deep_research/database/sqlcipher_utils.py
@@ -28,10 +28,11 @@ def _get_key_from_password(password: str) -> bytes:
settings = get_sqlcipher_settings()
logger.info("Generating DB encryption key...")
- # The salt we use here doesn't actually matter because sqlcipher
- # automatically salts the encryption key.
+ # Use PBKDF2 to stretch the password into a hex key.
+ # Note: SQLCipher handles per-database salting internally, so this
+ # placeholder salt is acceptable. See PBKDF2_PLACEHOLDER_SALT docstring.
key = pbkdf2_hmac(
- "sha512", password.encode(), b"no salt", settings["kdf_iterations"]
+ "sha512", password.encode(), PBKDF2_PLACEHOLDER_SALT, settings["kdf_iterations"]
)
logger.info("Generated DB encryption key.")
@@ -89,6 +90,13 @@ DEFAULT_PAGE_SIZE = 16384 # 16KB pages for maximum performance with caching
DEFAULT_HMAC_ALGORITHM = "HMAC_SHA512"
DEFAULT_KDF_ALGORITHM = "PBKDF2_HMAC_SHA512"
+# SQLCipher handles per-database salting internally when using PRAGMA key.
+# This placeholder is used only for PBKDF2 key stretching to convert the
+# user's password into a hex key format. The actual encryption salt is
+# managed by SQLCipher on a per-database basis, providing proper salt
+# uniqueness. See: https://www.zetetic.net/sqlcipher/sqlcipher-api/#key
+PBKDF2_PLACEHOLDER_SALT = b"ldr-sqlcipher-key-derivation"
+
def get_sqlcipher_settings(username: Optional[str] = None) -> dict:
"""