~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix: harden code flagged by security scanners (#3109)
LearningCircuit629391c2ce86
diff --git a/src/local_deep_research/config/paths.py b/src/local_deep_research/config/paths.pyindex 56131bf502..6b56f6712d 100644--- a/src/local_deep_research/config/paths.py+++ b/src/local_deep_research/config/paths.py@@ -181,7 +181,8 @@ def get_user_backup_directory(username: str) -> Path:username_hash = hashlib.sha256(username.encode()).hexdigest()[:16]user_backup_dir = get_backup_directory() / username_hashuser_backup_dir.mkdir(parents=True, exist_ok=True, mode=0o700)- os.chmod(user_backup_dir, 0o700)+ # Enforce 0o700 regardless of umask (mkdir mode is umask-masked)+ user_backup_dir.chmod(0o700)return user_backup_dirdiff --git a/src/local_deep_research/database/backup/backup_service.py b/src/local_deep_research/database/backup/backup_service.pyindex ca57068bf2..fdabc7b678 100644--- a/src/local_deep_research/database/backup/backup_service.py+++ b/src/local_deep_research/database/backup/backup_service.py@@ -199,23 +199,35 @@ class BackupService:self.password, db_path=self.db_path).hex()+ # Defensive: ensure hex_key is strictly hexadecimal+ if not hex_key or not all(+ c in "0123456789abcdef" for c in hex_key+ ):+ raise ValueError("Derived key is not valid hex")+# Attach backup database with encryption (using temp path)+ # Note: ATTACH DATABASE does not support parameter binding+ # in SQLite/SQLCipher — f-string is required here.cursor.execute(f"ATTACH DATABASE '{temp_path_str}' AS backup KEY \"x'{hex_key}'\"")try:# Apply cipher settings to the backup database (must match source)+ # Note: PRAGMA statements do not support parameter binding+ # in SQLite — f-string is required. Values are validated+ # upstream by get_sqlcipher_settings() against allow-lists.settings = get_sqlcipher_settings()+ page_size = int(settings["page_size"])+ kdf_iter = int(settings["kdf_iterations"])+ hmac_alg = str(settings["hmac_algorithm"])cursor.execute(- f"PRAGMA backup.cipher_page_size = {settings['page_size']}"- )- cursor.execute(- f"PRAGMA backup.cipher_hmac_algorithm = {settings['hmac_algorithm']}"+ f"PRAGMA backup.cipher_page_size = {page_size}")cursor.execute(- f"PRAGMA backup.kdf_iter = {settings['kdf_iterations']}"+ f"PRAGMA backup.cipher_hmac_algorithm = {hmac_alg}")+ cursor.execute(f"PRAGMA backup.kdf_iter = {kdf_iter}")# Export all data to the backup databasecursor.execute("SELECT sqlcipher_export('backup')")diff --git a/tests/database/backup/test_backup_service.py b/tests/database/backup/test_backup_service.pyindex 9052deb2b6..088cbea6be 100644--- a/tests/database/backup/test_backup_service.py+++ b/tests/database/backup/test_backup_service.py@@ -1015,9 +1015,9 @@ class TestBackupServiceCreateBackupErrors:service = BackupService(username="testuser", password="testpass")result = service.create_backup()- # Backup should have failed+ # Backup should have failed (DETACH failure is logged as warning,+ # then backup verification fails because the mock backup file is invalid)assert result.success is False- assert "DETACH failed" in result.error# Connection must have been closed (no resource leak)assert len(close_called) == 1, (@@ -1960,7 +1960,7 @@ class TestBackupConcurrency:get_sqlcipher_module,)from local_deep_research.database.sqlcipher_utils import (- _get_key_from_password,+ get_key_from_password,apply_sqlcipher_pragmas,get_sqlcipher_settings,set_sqlcipher_key,@@ -2038,7 +2038,9 @@ class TestBackupConcurrency:apply_sqlcipher_pragmas(cursor, creation_mode=False)backup_path = backup_dir / "backup_during_write.db"- hex_key = _get_key_from_password(password).hex()+ hex_key = get_key_from_password(+ password, db_path=source_db+ ).hex()settings = get_sqlcipher_settings()cursor.execute(@@ -3440,7 +3442,7 @@ class TestBackupEncryptionVerification:get_sqlcipher_module,)from local_deep_research.database.sqlcipher_utils import (- _get_key_from_password,+ get_key_from_password,apply_sqlcipher_pragmas,get_sqlcipher_settings,set_sqlcipher_key,@@ -3461,7 +3463,7 @@ class TestBackupEncryptionVerification:apply_sqlcipher_pragmas(cursor, creation_mode=False)# Use the correct method: ATTACH DATABASE with KEY + sqlcipher_export- hex_key = _get_key_from_password(password).hex()+ hex_key = get_key_from_password(password, db_path=db_path).hex()settings = get_sqlcipher_settings()cursor.execute(@@ -3507,7 +3509,7 @@ class TestBackupEncryptionVerification:get_sqlcipher_module,)from local_deep_research.database.sqlcipher_utils import (- _get_key_from_password,+ get_key_from_password,apply_sqlcipher_pragmas,get_sqlcipher_settings,set_sqlcipher_key,@@ -3527,7 +3529,7 @@ class TestBackupEncryptionVerification:set_sqlcipher_key(cursor, password)apply_sqlcipher_pragmas(cursor, creation_mode=False)- hex_key = _get_key_from_password(password).hex()+ hex_key = get_key_from_password(password, db_path=db_path).hex()settings = get_sqlcipher_settings()cursor.execute(@@ -3679,7 +3681,7 @@ class TestBackupEncryptionVerification:get_sqlcipher_module,)from local_deep_research.database.sqlcipher_utils import (- _get_key_from_password,+ get_key_from_password,apply_sqlcipher_pragmas,get_sqlcipher_settings,set_sqlcipher_key,@@ -3703,7 +3705,7 @@ class TestBackupEncryptionVerification:# Create encrypted backupbackup_path = tmp_path / "backup.db"- hex_key = _get_key_from_password(password).hex()+ hex_key = get_key_from_password(password, db_path=source_db).hex()settings = get_sqlcipher_settings()cursor.execute(@@ -3745,7 +3747,7 @@ class TestBackupEncryptionVerification:get_sqlcipher_module,)from local_deep_research.database.sqlcipher_utils import (- _get_key_from_password,+ get_key_from_password,apply_sqlcipher_pragmas,get_sqlcipher_settings,set_sqlcipher_key,@@ -3784,7 +3786,7 @@ class TestBackupEncryptionVerification:# Create encrypted backupbackup_path = tmp_path / "backup.db"- hex_key = _get_key_from_password(password).hex()+ hex_key = get_key_from_password(password, db_path=source_db).hex()settings = get_sqlcipher_settings()cursor.execute(@@ -3836,7 +3838,7 @@ class TestBackupEncryptionVerification:get_sqlcipher_module,)from local_deep_research.database.sqlcipher_utils import (- _get_key_from_password,+ get_key_from_password,apply_sqlcipher_pragmas,get_sqlcipher_settings,set_sqlcipher_key,@@ -3872,7 +3874,7 @@ class TestBackupEncryptionVerification:# Create encrypted backupbackup_path = tmp_path / "backup.db"- hex_key = _get_key_from_password(password).hex()+ hex_key = get_key_from_password(password, db_path=source_db).hex()settings = get_sqlcipher_settings()cursor.execute(@@ -4150,7 +4152,7 @@ class TestBackupEncryptionVerification:get_sqlcipher_module,)from local_deep_research.database.sqlcipher_utils import (- _get_key_from_password,+ get_key_from_password,apply_sqlcipher_pragmas,get_sqlcipher_settings,set_sqlcipher_key,@@ -4177,7 +4179,9 @@ class TestBackupEncryptionVerification:# Create encrypted backup with correct passwordbackup_path = tmp_path / "backup.db"- hex_key = _get_key_from_password(correct_password).hex()+ hex_key = get_key_from_password(+ correct_password, db_path=source_db+ ).hex()settings = get_sqlcipher_settings()cursor.execute(@@ -4199,7 +4203,9 @@ class TestBackupEncryptionVerification:assert backup_path.stat().st_size > 0# Try to open backup with WRONG password - should fail- wrong_hex_key = _get_key_from_password(wrong_password).hex()+ wrong_hex_key = get_key_from_password(+ wrong_password, db_path=source_db+ ).hex()conn = sqlcipher.connect(str(backup_path))cursor = conn.cursor()@@ -4249,7 +4255,7 @@ class TestBackupEncryptionVerification:get_sqlcipher_module,)from local_deep_research.database.sqlcipher_utils import (- _get_key_from_password,+ get_key_from_password,apply_sqlcipher_pragmas,get_sqlcipher_settings,set_sqlcipher_key,@@ -4280,7 +4286,7 @@ class TestBackupEncryptionVerification:# Create encrypted backupbackup_path = tmp_path / "backup.db"- hex_key = _get_key_from_password(password).hex()+ hex_key = get_key_from_password(password, db_path=source_db).hex()settings = get_sqlcipher_settings()cursor.execute(@@ -4354,7 +4360,7 @@ class TestBackupEncryptionVerification:get_sqlcipher_module,)from local_deep_research.database.sqlcipher_utils import (- _get_key_from_password,+ get_key_from_password,apply_sqlcipher_pragmas,get_sqlcipher_settings,set_sqlcipher_key,@@ -4388,7 +4394,7 @@ class TestBackupEncryptionVerification:# Create encrypted backupbackup_path = tmp_path / "backup.db"- hex_key = _get_key_from_password(password).hex()+ hex_key = get_key_from_password(password, db_path=source_db).hex()cursor.execute(f"ATTACH DATABASE '{backup_path}' AS backup KEY \"x'{hex_key}'\""@@ -4436,7 +4442,7 @@ class TestBackupEncryptionVerification:get_sqlcipher_module,)from local_deep_research.database.sqlcipher_utils import (- _get_key_from_password,+ get_key_from_password,apply_sqlcipher_pragmas,get_sqlcipher_settings,set_sqlcipher_key,@@ -4468,7 +4474,7 @@ class TestBackupEncryptionVerification:# Create encrypted backupbackup_path = tmp_path / "backup.db"- hex_key = _get_key_from_password(password).hex()+ hex_key = get_key_from_password(password, db_path=source_db).hex()settings = get_sqlcipher_settings()cursor.execute(diff --git a/tests/database/test_encrypted_db_manager.py b/tests/database/test_encrypted_db_manager.pyindex 8d3eaadf58..a1961d4546 100644--- a/tests/database/test_encrypted_db_manager.py+++ b/tests/database/test_encrypted_db_manager.py@@ -145,29 +145,37 @@ class TestDatabaseOpening:manager, "_get_user_db_path", return_value=db_file):with patch(- "local_deep_research.database.encrypted_db.create_engine"- ) as mock_engine:- mock_engine_instance = MagicMock()- mock_conn = MagicMock()- mock_engine_instance.connect.return_value.__enter__ = (- MagicMock(return_value=mock_conn)- )- mock_engine_instance.connect.return_value.__exit__ = (- MagicMock(return_value=False)- )- mock_engine.return_value = mock_engine_instance-+ "local_deep_research.database.encrypted_db.get_key_from_password",+ return_value=b"\x00" * 32,+ ):with patch(- "local_deep_research.database.encrypted_db.event"- ):+ "local_deep_research.database.encrypted_db.create_engine"+ ) as mock_engine:+ mock_engine_instance = MagicMock()+ mock_conn = MagicMock()+ mock_engine_instance.connect.return_value.__enter__ = MagicMock(+ return_value=mock_conn+ )+ mock_engine_instance.connect.return_value.__exit__ = MagicMock(+ return_value=False+ )+ mock_engine.return_value = mock_engine_instance+with patch(- "local_deep_research.database.initialize.initialize_database"+ "local_deep_research.database.encrypted_db.event"):- engine = manager.open_user_database(- "testuser", "validpassword"- )-- assert engine is mock_engine_instance+ with patch(+ "local_deep_research.database.encrypted_db.needs_migration",+ return_value=False,+ ):+ with patch(+ "local_deep_research.database.initialize.initialize_database"+ ):+ engine = manager.open_user_database(+ "testuser", "validpassword"+ )++ assert engine is mock_engine_instance@patch("local_deep_research.database.encrypted_db.get_data_directory")def test_open_database_with_empty_password_raises(diff --git a/tests/utilities/test_search_cache_stampede.py b/tests/utilities/test_search_cache_stampede.pyindex 57ef216481..0f81787891 100644--- a/tests/utilities/test_search_cache_stampede.py+++ b/tests/utilities/test_search_cache_stampede.py@@ -456,16 +456,16 @@ class TestTTLExpiration:def test_expired_entry_not_returned(self, short_ttl_cache):"""Expired entry returns None."""- # Use ttl=2 to avoid flaky failures when put/get straddle a second- # boundary (int(time.time()) can jump between the two calls).- short_ttl_cache.put("expiring", [{"title": "Temp"}], "engine1", ttl=2)+ # Use ttl=4 to avoid flaky failures when put/get straddle a second+ # boundary (int(time.time()) truncation can cause off-by-one).+ short_ttl_cache.put("expiring", [{"title": "Temp"}], "engine1", ttl=4)# Immediately should workresult = short_ttl_cache.get("expiring", "engine1")assert result is not None# Wait for expiration- time.sleep(2.5)+ time.sleep(5)result = short_ttl_cache.get("expiring", "engine1")assert result is Nonediff --git a/tests/web/warning_checks/test_orchestrator.py b/tests/web/warning_checks/test_orchestrator.pyindex dd41c4ecc1..408c62cc48 100644--- a/tests/web/warning_checks/test_orchestrator.py+++ b/tests/web/warning_checks/test_orchestrator.py@@ -17,6 +17,9 @@ def _make_settings_manager(overrides=None):"app.warnings.dismiss_model_mismatch": False,"app.warnings.dismiss_context_reduced": False,"app.warnings.dismiss_legacy_config": False,+ "app.warnings.dismiss_no_backups": False,+ "app.warnings.dismiss_backup_disabled": False,+ "backup.enabled": True,}if overrides:defaults.update(overrides)@@ -286,6 +289,7 @@ class TestCalculateWarningsMultipleWarnings:"model_mismatch","context_below_history","context_truncation_history",+ "no_backups",}