~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: SQLCipher mlock() warnings — default cipher_memory_security to OFF (#2355)
LearningCircuitd0a438cc77d7
diff --git a/README.md b/README.mdindex 243d7e70f8..32f8b512fd 100644--- a/README.md+++ b/README.md@@ -152,7 +152,7 @@ flowchart LRYour data stays yours. Each user gets their own isolated SQLCipher database encrypted with AES-256 (Signal-level security). No password recovery means true zero-knowledge—even server admins can't read your data. Run fully local with Ollama + SearXNG and nothing ever leaves your machine.-**In-memory credentials**: Like all applications that use secrets at runtime — including [password managers](https://www.ise.io/casestudies/password-manager-hacking/), browsers, and API clients — credentials are held in plain text in process memory during active sessions. This is an [industry-wide accepted reality](https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html), not specific to LDR: if an attacker can read process memory, they can also read any in-process decryption key. We mitigate this with encrypted swap prevention (`mlock`), session-scoped credential lifetimes, and SQLCipher's `cipher_memory_security` (which zeros database key material after use). Ideas for further improvements are always welcome via [GitHub Issues](https://github.com/LearningCircuit/local-deep-research/issues). See our [Security Policy](SECURITY.md) for details.+**In-memory credentials**: Like all applications that use secrets at runtime — including [password managers](https://www.ise.io/casestudies/password-manager-hacking/), browsers, and API clients — credentials are held in plain text in process memory during active sessions. This is an [industry-wide accepted reality](https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html), not specific to LDR: if an attacker can read process memory, they can also read any in-process decryption key. We mitigate this with session-scoped credential lifetimes and core dump exclusion. Ideas for further improvements are always welcome via [GitHub Issues](https://github.com/LearningCircuit/local-deep-research/issues). See our [Security Policy](SECURITY.md) for details.**Supply Chain Security**: Docker images are signed with [Cosign](https://github.com/sigstore/cosign), include SLSA provenance attestations, and attach SBOMs. Verify with:```bashdiff --git a/SECURITY.md b/SECURITY.mdindex a714e9cf61..132e46ee37 100644--- a/SECURITY.md+++ b/SECURITY.md@@ -56,24 +56,55 @@ This project processes user queries and search results. Key areas:- **Search data** - Queries are processed locally when possible- **Dependencies** - Regularly updated via automated scanning-### Data at Rest+### Database Encryption-User passwords are never stored directly — each user's password serves as the encryption key for their personal SQLCipher database (AES-256, PBKDF2-HMAC-SHA512 with 256,000 iterations and per-user salt). API keys are stored inside these encrypted databases.+Local Deep Research uses **SQLCipher** (AES-256-CBC) for database encryption. Each user's database is encrypted with their login password as the key, derived via PBKDF2-HMAC-SHA512 with 256,000 iterations and a per-user random salt. There is no separate password hash — authentication works by attempting to decrypt the database. API keys stored in the database are encrypted at rest.### In-Memory Credentials-Like all applications that use secrets at runtime, credentials are held in plain text in process memory during active sessions. This is an industry-wide reality acknowledged by [OWASP](https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html), [Microsoft](https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-security-securestring) (who deprecated `SecureString` for this reason), and the [pyca/cryptography](https://cryptography.io/en/stable/limitations/) library. Independent research has shown that even dedicated security software like [password managers](https://www.ise.io/casestudies/password-manager-hacking/) cannot fully prevent secrets from residing in memory.+Like all applications that use secrets at runtime — including [password managers](https://www.ise.io/casestudies/password-manager-hacking/), browsers, and API clients — credentials are held in plain text in process memory during active sessions. This is an [industry-wide reality](https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html) acknowledged by [OWASP](https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html), [Microsoft](https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-security-securestring) (who deprecated `SecureString` for this reason), and the [pyca/cryptography](https://cryptography.io/en/stable/limitations/) library.-**Why in-process encryption does not help:** If an attacker can read process memory, they can also read any decryption key stored in the same process. As OWASP notes, for most server-side applications the cost of defending against memory-level attacks far exceeds the likely impact.+**Why in-process encryption does not help:** If an attacker can read process memory, they can also read any decryption key stored in the same process. The password exists in Flask session storage, database connection managers, and thread-local storage throughout the application's lifetime — protecting only one copy (e.g., SQLCipher's internal buffers) does not meaningfully reduce exposure.**What we do to mitigate:**-- SQLCipher's `cipher_memory_security` zeros database key material after use-- `mlock` prevents credential pages from being swapped to disk- Session-scoped credential lifetimes with automatic expiration- Core dump exclusion via container security settingsIdeas for further improvements are always welcome via [GitHub Issues](https://github.com/LearningCircuit/local-deep-research/issues).+### Memory Security (`cipher_memory_security`)++SQLCipher's `cipher_memory_security` pragma controls whether SQLCipher zeroes its internal buffers after use and calls `mlock()` to prevent memory pages from being swapped to disk.++**Default: OFF.** Since the same password is unprotected elsewhere in process memory (see above), locking only SQLCipher's internal buffers does not meaningfully reduce exposure.++To enable memory security (e.g., for compliance requirements):++```bash+# Environment variable+LDR_DB_CONFIG_CIPHER_MEMORY_SECURITY=ON+```++In Docker, `mlock()` requires the `IPC_LOCK` capability:++```yaml+# docker-compose.yml+services:+ local-deep-research:+ cap_add:+ - IPC_LOCK+ environment:+ - LDR_DB_CONFIG_CIPHER_MEMORY_SECURITY=ON+```++Or with `docker run`:++```bash+docker run --cap-add IPC_LOCK -e LDR_DB_CONFIG_CIPHER_MEMORY_SECURITY=ON ...+```++`IPC_LOCK` is a narrow Linux capability that only permits memory locking — it does not grant any other privileges.+## Supported VersionsSecurity fixes are only provided for the latest release. Please upgrade to receive patches.diff --git a/src/local_deep_research/database/sqlcipher_utils.py b/src/local_deep_research/database/sqlcipher_utils.pyindex ea94697300..8398350e3a 100644--- a/src/local_deep_research/database/sqlcipher_utils.py+++ b/src/local_deep_research/database/sqlcipher_utils.py@@ -420,14 +420,14 @@ def apply_sqlcipher_pragmas(# 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:- # cipher_memory_security is a runtime PRAGMA that does not affect stored- # database format. ON (default) zeroes memory after use for security;- # OFF improves performance at the cost of residual plaintext in memory.- mem_security = get_env_setting("db_config.cipher_memory_security", "ON")- cursor_or_conn.execute(- f"PRAGMA cipher_memory_security = {mem_security}"- )+ # cipher_memory_security is a runtime PRAGMA. ON zeroes SQLCipher buffers+ # and calls mlock() to prevent swap; OFF skips this. Defaulting to OFF+ # because the password already sits unprotected in Flask session, db_manager,+ # and thread-local storage — mlock on SQLCipher's buffers alone doesn't help.+ # Users can opt in with LDR_DB_CONFIG_CIPHER_MEMORY_SECURITY=ON + IPC_LOCK.+ # Applied on every connection (not just creation) so env var overrides work.+ mem_security = get_env_setting("db_config.cipher_memory_security", "OFF")+ cursor_or_conn.execute(f"PRAGMA cipher_memory_security = {mem_security}")def apply_performance_pragmas(cursor_or_conn: Any) -> None:diff --git a/src/local_deep_research/settings/env_definitions/db_config.py b/src/local_deep_research/settings/env_definitions/db_config.pyindex 33c04fb6a3..3792b42b35 100644--- a/src/local_deep_research/settings/env_definitions/db_config.py+++ b/src/local_deep_research/settings/env_definitions/db_config.py@@ -95,9 +95,9 @@ DB_CONFIG_SETTINGS = [# Runtime security settingsEnumSetting(key="db_config.cipher_memory_security",- description="SQLCipher memory security (ON=clear memory after use, OFF=faster)",+ description="SQLCipher memory security (ON=clear memory after use + mlock, OFF=faster). ON requires IPC_LOCK in Docker.",allowed_values={"ON", "OFF"},- default="ON",+ default="OFF",case_sensitive=False,),]diff --git a/tests/database/test_sqlcipher_utils.py b/tests/database/test_sqlcipher_utils.pyindex 058bceea21..45e738955f 100644--- a/tests/database/test_sqlcipher_utils.py+++ b/tests/database/test_sqlcipher_utils.py@@ -1230,67 +1230,71 @@ class TestCIAwareKDFMinimum:class TestCipherMemorySecurityEnvVar:"""Tests for configurable cipher_memory_security."""- def test_defaults_to_on(self):- """Test that cipher_memory_security defaults to ON."""+ def test_defaults_to_off(self):+ """Test that cipher_memory_security defaults to OFF in both modes."""from local_deep_research.database.sqlcipher_utils import (apply_sqlcipher_pragmas,)- mock_cursor = Mock()+ for mode in (True, False):+ mock_cursor = Mock()- with patch.dict(os.environ, {}, clear=False):- os.environ.pop("LDR_DB_CONFIG_CIPHER_MEMORY_SECURITY", None)- apply_sqlcipher_pragmas(mock_cursor, creation_mode=True)+ with patch.dict(os.environ, {}, clear=False):+ os.environ.pop("LDR_DB_CONFIG_CIPHER_MEMORY_SECURITY", None)+ apply_sqlcipher_pragmas(mock_cursor, creation_mode=mode)- call_args_list = [- call[0][0] for call in mock_cursor.execute.call_args_list- ]- mem_calls = [- arg for arg in call_args_list if "cipher_memory_security" in arg- ]- assert len(mem_calls) == 1- assert "ON" in mem_calls[0]+ call_args_list = [+ call[0][0] for call in mock_cursor.execute.call_args_list+ ]+ mem_calls = [+ arg for arg in call_args_list if "cipher_memory_security" in arg+ ]+ assert len(mem_calls) == 1, f"Failed for creation_mode={mode}"+ assert "OFF" in mem_calls[0], f"Failed for creation_mode={mode}"- def test_respects_off_env_var(self):- """Test that LDR_DB_CONFIG_CIPHER_MEMORY_SECURITY=OFF is respected."""+ def test_respects_on_env_var(self):+ """Test that LDR_DB_CONFIG_CIPHER_MEMORY_SECURITY=ON is respected in both modes."""from local_deep_research.database.sqlcipher_utils import (apply_sqlcipher_pragmas,)- mock_cursor = Mock()+ for mode in (True, False):+ mock_cursor = Mock()- with patch.dict(- os.environ, {"LDR_DB_CONFIG_CIPHER_MEMORY_SECURITY": "OFF"}- ):- apply_sqlcipher_pragmas(mock_cursor, creation_mode=True)+ with patch.dict(+ os.environ, {"LDR_DB_CONFIG_CIPHER_MEMORY_SECURITY": "ON"}+ ):+ apply_sqlcipher_pragmas(mock_cursor, creation_mode=mode)- call_args_list = [- call[0][0] for call in mock_cursor.execute.call_args_list- ]- mem_calls = [- arg for arg in call_args_list if "cipher_memory_security" in arg- ]- assert len(mem_calls) == 1- assert "OFF" in mem_calls[0]+ call_args_list = [+ call[0][0] for call in mock_cursor.execute.call_args_list+ ]+ mem_calls = [+ arg for arg in call_args_list if "cipher_memory_security" in arg+ ]+ assert len(mem_calls) == 1, f"Failed for creation_mode={mode}"+ assert "ON" in mem_calls[0], f"Failed for creation_mode={mode}"- def test_invalid_value_falls_back_to_on(self):- """Test that invalid value falls back to ON."""+ def test_invalid_value_falls_back_to_off(self):+ """Test that invalid value falls back to OFF (default) in both modes."""from local_deep_research.database.sqlcipher_utils import (apply_sqlcipher_pragmas,)- mock_cursor = Mock()+ for mode in (True, False):+ mock_cursor = Mock()- with patch.dict(- os.environ, {"LDR_DB_CONFIG_CIPHER_MEMORY_SECURITY": "INVALID"}- ):- apply_sqlcipher_pragmas(mock_cursor, creation_mode=True)+ with patch.dict(+ os.environ,+ {"LDR_DB_CONFIG_CIPHER_MEMORY_SECURITY": "INVALID"},+ ):+ apply_sqlcipher_pragmas(mock_cursor, creation_mode=mode)- call_args_list = [- call[0][0] for call in mock_cursor.execute.call_args_list- ]- mem_calls = [- arg for arg in call_args_list if "cipher_memory_security" in arg- ]- assert len(mem_calls) == 1- assert "ON" in mem_calls[0]+ call_args_list = [+ call[0][0] for call in mock_cursor.execute.call_args_list+ ]+ mem_calls = [+ arg for arg in call_args_list if "cipher_memory_security" in arg+ ]+ assert len(mem_calls) == 1, f"Failed for creation_mode={mode}"+ assert "OFF" in mem_calls[0], f"Failed for creation_mode={mode}"