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: add SQLCipher 4.x compatibility for cipher pragma ordering

LearningCircuit6ffb589e9439
diff --git a/src/local_deep_research/database/encrypted_db.py b/src/local_deep_research/database/encrypted_db.py
index 3269afdafc..ab3330a4cb 100644
--- a/src/local_deep_research/database/encrypted_db.py
+++ b/src/local_deep_research/database/encrypted_db.py
@@ -18,6 +18,7 @@ from .sqlcipher_compat import get_sqlcipher_module
from .sqlcipher_utils import (
set_sqlcipher_key,
set_sqlcipher_rekey,
+ apply_cipher_settings_before_key,
apply_sqlcipher_pragmas,
apply_performance_pragmas,
verify_sqlcipher_connection,
@@ -83,6 +84,8 @@ class DatabaseManager:
sqlcipher = sqlcipher_module.dbapi2
conn = sqlcipher.connect(tmp_path)
+ # IMPORTANT: For SQLCipher 4.x, cipher settings MUST be set BEFORE the key
+ apply_cipher_settings_before_key(conn)
# Use centralized key setting
set_sqlcipher_key(conn, "testpass")
conn.execute("CREATE TABLE test (id INTEGER PRIMARY KEY)")
@@ -210,6 +213,8 @@ class DatabaseManager:
isolation_level="IMMEDIATE",
check_same_thread=False,
)
+ # IMPORTANT: For SQLCipher 4.x, cipher settings MUST be set BEFORE the key
+ apply_cipher_settings_before_key(conn)
# Use centralized SQLCipher setup
set_sqlcipher_key(conn, password)
apply_sqlcipher_pragmas(conn, creation_mode=True)
@@ -260,6 +265,9 @@ class DatabaseManager:
)
cursor = conn.cursor()
+ # IMPORTANT: For SQLCipher 4.x, cipher settings MUST be set BEFORE the key
+ apply_cipher_settings_before_key(cursor)
+
# Use centralized SQLCipher setup
set_sqlcipher_key(cursor, password)
@@ -267,7 +275,7 @@ class DatabaseManager:
if not verify_sqlcipher_connection(cursor):
raise ValueError("Failed to verify database key")
- # Apply SQLCipher and performance settings
+ # Apply SQLCipher and performance settings (those that go after the key)
apply_sqlcipher_pragmas(cursor, creation_mode=False)
apply_performance_pragmas(cursor, username)
@@ -365,6 +373,9 @@ class DatabaseManager:
)
cursor = conn.cursor()
+ # IMPORTANT: For SQLCipher 4.x, cipher settings MUST be set BEFORE the key
+ apply_cipher_settings_before_key(cursor)
+
# Use centralized SQLCipher setup
set_sqlcipher_key(cursor, password)
@@ -372,7 +383,7 @@ class DatabaseManager:
if not verify_sqlcipher_connection(cursor):
raise ValueError("Failed to verify database key")
- # Apply SQLCipher and performance settings
+ # Apply SQLCipher and performance settings (those that go after the key)
apply_sqlcipher_pragmas(cursor, creation_mode=False)
apply_performance_pragmas(cursor, username)
@@ -577,6 +588,9 @@ class DatabaseManager:
)
cursor = conn.cursor()
+ # IMPORTANT: For SQLCipher 4.x, cipher settings MUST be set BEFORE the key
+ apply_cipher_settings_before_key(cursor)
+
# Use centralized SQLCipher setup
set_sqlcipher_key(cursor, password)
apply_sqlcipher_pragmas(cursor, creation_mode=False)
diff --git a/src/local_deep_research/database/sqlcipher_utils.py b/src/local_deep_research/database/sqlcipher_utils.py
index 75eb0ecb27..b11c821683 100644
--- a/src/local_deep_research/database/sqlcipher_utils.py
+++ b/src/local_deep_research/database/sqlcipher_utils.py
@@ -133,13 +133,44 @@ def get_sqlcipher_settings(username: Optional[str] = None) -> dict:
return settings
+def apply_cipher_settings_before_key(
+ cursor_or_conn: Any,
+ username: Optional[str] = None,
+) -> None:
+ """
+ Apply SQLCipher cipher settings that MUST be set BEFORE the key.
+
+ In SQLCipher 4.x, cipher_page_size and cipher_hmac_algorithm must be
+ set BEFORE PRAGMA key is executed. This function should be called
+ immediately after connection, before set_sqlcipher_key().
+
+ Args:
+ cursor_or_conn: SQLCipher cursor or connection object
+ username: Username to get settings for (if available)
+ """
+ settings = get_sqlcipher_settings(username)
+
+ # These MUST be set BEFORE the key for SQLCipher 4.x compatibility
+ cursor_or_conn.execute(f"PRAGMA cipher_page_size = {settings['page_size']}")
+ cursor_or_conn.execute(
+ f"PRAGMA cipher_hmac_algorithm = {settings['hmac_algorithm']}"
+ )
+ cursor_or_conn.execute(
+ f"PRAGMA cipher_kdf_algorithm = {settings['kdf_algorithm']}"
+ )
+
+
def apply_sqlcipher_pragmas(
cursor_or_conn: Any,
creation_mode: bool = False,
username: Optional[str] = None,
) -> None:
"""
- Apply standard SQLCipher PRAGMA settings.
+ Apply SQLCipher PRAGMA settings that are set AFTER the key.
+
+ NOTE: For SQLCipher 4.x compatibility, you MUST call
+ apply_cipher_settings_before_key() BEFORE set_sqlcipher_key(),
+ and then call this function AFTER set_sqlcipher_key().
Args:
cursor_or_conn: SQLCipher cursor or connection object
@@ -150,11 +181,7 @@ def apply_sqlcipher_pragmas(
# Get settings (from database if available, otherwise defaults)
settings = get_sqlcipher_settings(username)
- # Core settings needed for both creation and access
- cursor_or_conn.execute(f"PRAGMA cipher_page_size = {settings['page_size']}")
- cursor_or_conn.execute(
- f"PRAGMA cipher_hmac_algorithm = {settings['hmac_algorithm']}"
- )
+ # kdf_iter can be set after the key (applies to future derivation)
cursor_or_conn.execute(f"PRAGMA kdf_iter = {settings['kdf_iterations']}")
if creation_mode:
@@ -248,10 +275,13 @@ def create_sqlcipher_connection(db_path: str, password: str) -> Any:
conn = sqlcipher3.connect(str(db_path))
cursor = conn.cursor()
+ # IMPORTANT: For SQLCipher 4.x, cipher settings MUST be set BEFORE the key
+ apply_cipher_settings_before_key(cursor)
+
# Set encryption key
set_sqlcipher_key(cursor, password)
- # Apply SQLCipher settings
+ # Apply SQLCipher settings that go after the key
apply_sqlcipher_pragmas(cursor, creation_mode=False)
# Verify connection