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): close the raw SQLCipher connection when the reserved-file identity check refuses (#6713)

Ziyang Guo2704df0e116b
diff --git a/changelog.d/+reserved-file-refusal-closes-conn.security.md b/changelog.d/+reserved-file-refusal-closes-conn.security.md
new file mode 100644
index 0000000000..92918dd222
--- /dev/null
+++ b/changelog.d/+reserved-file-refusal-closes-conn.security.md
@@ -0,0 +1,3 @@
+Creating a user database no longer leaks the raw SQLCipher connection when the
+reserved-file identity check refuses a swapped path — the refusal now closes the
+connection before propagating.
diff --git a/src/local_deep_research/database/encrypted_db.py b/src/local_deep_research/database/encrypted_db.py
index 27ebab78b7..69d796399e 100644
--- a/src/local_deep_research/database/encrypted_db.py
+++ b/src/local_deep_research/database/encrypted_db.py
@@ -858,9 +858,13 @@ class DatabaseManager:
# The open above is where a swapped pathname would have
# taken effect, so this is the first point the reservation
# can be checked against what SQLCipher actually opened.
- _assert_reserved_file(db_path, reserved)
- _best_effort_chmod(db_path, 0o600, warn=True)
+ # The check and the chmod run INSIDE the try below: a
+ # refusal must close the raw connection the open just
+ # returned, not leak it past the block that owns it (#6608).
try:
+ _assert_reserved_file(db_path, reserved)
+ _best_effort_chmod(db_path, 0o600, warn=True)
+
# Get the CREATE TABLE statements from SQLAlchemy models
from sqlalchemy.dialects import sqlite
from sqlalchemy.schema import CreateIndex, CreateTable
diff --git a/tests/security/test_create_user_database_symlink.py b/tests/security/test_create_user_database_symlink.py
index ad013cb271..291e64ed1d 100644
--- a/tests/security/test_create_user_database_symlink.py
+++ b/tests/security/test_create_user_database_symlink.py
@@ -49,6 +49,16 @@ def _plant_dangling_link(manager, username, victim):
return db_path
+class _RecordingConnection:
+ """Stand-in for the raw SQLCipher connection that records close()."""
+
+ def __init__(self):
+ self.closed = False
+
+ def close(self):
+ self.closed = True
+
+
def test_dangling_symlink_at_db_path_is_refused(manager, victim):
db_path = _plant_dangling_link(manager, "linked_user", victim)
@@ -159,6 +169,48 @@ def test_file_swapped_after_reservation_is_refused(
assert victim.exists()
+def test_reservation_refusal_closes_the_raw_sqlcipher_connection(
+ manager, victim, monkeypatch
+):
+ """The identity check must not leak the raw connection it refuses.
+
+ The reservation check used to run before the ``try/finally`` that owns
+ the connection ``create_sqlcipher_connection`` returned, so a refusal
+ propagated with that connection still open (#6608). It now runs inside
+ the block, and the refusal closes it before re-raising.
+ """
+ username = "leaked_conn_user"
+ db_path = manager._get_user_db_path(username)
+ real_create_private_file = encrypted_db._create_private_file
+ conn = _RecordingConnection()
+
+ # Drive the encrypted branch with a recording stub instead of the real
+ # SQLCipher binding, so the connection's lifecycle is what is asserted.
+ manager.has_encryption = True
+
+ def reserve_then_swap(path):
+ reserved = real_create_private_file(path)
+ if Path(path) == db_path:
+ os.unlink(path)
+ os.symlink(victim, path)
+ return reserved
+
+ monkeypatch.setattr(encrypted_db, "_create_private_file", reserve_then_swap)
+ monkeypatch.setattr(
+ encrypted_db, "create_sqlcipher_connection", lambda *a, **kw: conn
+ )
+
+ with pytest.raises(ValueError, match="was replaced"):
+ manager.create_user_database(username, PASSWORD)
+
+ # The leaked-connection half of the fix.
+ assert conn.closed
+ # The refusal still cleans up after itself, as before.
+ assert not db_path.exists() and not db_path.is_symlink()
+ assert not get_salt_file_path(db_path).exists()
+ assert username not in manager.connections
+
+
def test_unencrypted_swap_is_refused_before_the_migrations_write(
tmp_path, victim, monkeypatch
):