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(db): validate SQLCipher hex keys (#7150)

LearningCircuit8fe46d72a35e
diff --git a/changelog.d/+sqlcipher-hex-key-validation.bugfix.md b/changelog.d/+sqlcipher-hex-key-validation.bugfix.md
new file mode 100644
index 0000000000..4ed40bb4da
--- /dev/null
+++ b/changelog.d/+sqlcipher-hex-key-validation.bugfix.md
@@ -0,0 +1 @@
+Reject malformed pre-derived SQLCipher keys before building the database key statement.
diff --git a/src/local_deep_research/database/sqlcipher_utils.py b/src/local_deep_research/database/sqlcipher_utils.py
index 3a68a2fd18..1658360507 100644
--- a/src/local_deep_research/database/sqlcipher_utils.py
+++ b/src/local_deep_research/database/sqlcipher_utils.py
@@ -6,6 +6,7 @@ consistent password handling and PRAGMA settings across the codebase.
"""
import os
+import re
import secrets
import sqlite3
import threading
@@ -229,6 +230,12 @@ def set_sqlcipher_key_from_hex(cursor_or_conn: Any, hex_key: str) -> None:
cursor_or_conn: SQLCipher cursor or connection object
hex_key: Pre-derived hex key string (from get_key_from_password().hex())
"""
+ if (
+ not isinstance(hex_key, str)
+ or len(hex_key) % 2 != 0
+ or re.fullmatch(r"[0-9a-fA-F]+", hex_key) is None
+ ):
+ raise ValueError("hex_key must be non-empty even-length ASCII hex")
cursor_or_conn.execute(f"PRAGMA key = \"x'{hex_key}'\"") # gitleaks:allow
diff --git a/tests/database/test_sqlcipher_utils.py b/tests/database/test_sqlcipher_utils.py
index 52259810db..6ad363756d 100644
--- a/tests/database/test_sqlcipher_utils.py
+++ b/tests/database/test_sqlcipher_utils.py
@@ -344,6 +344,28 @@ class TestSetSqlcipherKeyFromHex:
assert "PRAGMA key" in call_args
assert "abcdef0123456789" in call_args
+ @pytest.mark.parametrize(
+ "invalid_key",
+ [
+ "",
+ "abc",
+ "abzg",
+ "ab'\"; DROP TABLE sentinel; --",
+ "ab\ncd",
+ "12",
+ ],
+ )
+ def test_rejects_non_hex_before_sql(self, invalid_key):
+ """A malformed key must never become SQL text."""
+ from local_deep_research.database.sqlcipher_utils import (
+ set_sqlcipher_key_from_hex,
+ )
+
+ mock_cursor = Mock()
+ with pytest.raises(ValueError, match="hex_key"):
+ set_sqlcipher_key_from_hex(mock_cursor, invalid_key)
+ mock_cursor.execute.assert_not_called()
+
class TestApplySqlcipherPragmas:
"""Tests for apply_sqlcipher_pragmas function."""