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.

test(database): guard unlistable hard-link scans (#7714)

Chanhyo Jungc04d5c606166
diff --git a/tests/database/test_rekey_lock.py b/tests/database/test_rekey_lock.py
index 6d771da538..9b2f2be752 100644
--- a/tests/database/test_rekey_lock.py
+++ b/tests/database/test_rekey_lock.py
@@ -525,6 +525,97 @@ def test_hard_linked_sidecar_is_locked_without_touching_its_mode(
assert target.read_text(encoding="utf-8") == "unrelated\n"
+ os.name == "nt" or not hasattr(os, "link"),
+ reason="POSIX hard links required",
+)
+ "lock_suffix", [".rekey.lock", ".rekey.authorization.lock"]
+)
+def test_hard_linked_sidecar_is_refused_unopened_when_directory_scan_fails(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, lock_suffix: str
+) -> None:
+ # Given an external hard link distinct from the real DB, WAL and shm,
+ # so classification must reach the directory scan to rule out aliases.
+ data_dir = tmp_path / "encrypted_databases"
+ data_dir.mkdir()
+ snapshot_dir = tmp_path / "snapshot"
+ snapshot_dir.mkdir()
+ db_path = data_dir / "user.db"
+ database_identities = set()
+ for suffix in ("", "-wal", "-shm"):
+ path = db_path.with_name(f"user.db{suffix}")
+ path.write_bytes(f"database{suffix}".encode())
+ status = path.stat()
+ database_identities.add((status.st_dev, status.st_ino))
+ assert len(database_identities) == 3
+ target = snapshot_dir / "unrelated.txt"
+ original_bytes = b"unrelated\n"
+ target.write_bytes(original_bytes)
+ target.chmod(0o644)
+ sidecar = db_path.with_name(f"user.db{lock_suffix}")
+ os.link(target, sidecar)
+ original = target.stat()
+ identity = (original.st_dev, original.st_ino)
+ original_mode = stat.S_IMODE(original.st_mode)
+ assert original.st_nlink >= 2
+ assert identity not in database_identities
+ linked = sidecar.stat()
+ assert (linked.st_dev, linked.st_ino) == identity
+ assert linked.st_nlink == original.st_nlink
+
+ real_scandir = os.scandir
+ real_open = os.open
+ scan_attempts: list[object] = []
+ open_attempts: list[str] = []
+
+ def denied_scandir(path):
+ scan_attempts.append(path)
+ if path == data_dir:
+ raise PermissionError(
+ errno.EACCES, "synthetic unlistable directory"
+ )
+ return real_scandir(path)
+
+ def recording_open(path, *args, **kwargs):
+ open_attempts.append(os.fspath(path))
+ return real_open(path, *args, **kwargs)
+
+ if lock_suffix == ".rekey.lock":
+ acquire = acquire_rekey_lock(db_path)
+ else:
+ acquire = rekey_lock_module.acquire_authorization_lease(
+ db_path, rekey_lock_module.AuthorizationLeaseMode.SHARED
+ )
+ entered = False
+ refusal = None
+ # Only the exact lock parent is unlistable; all opens remain real.
+ with monkeypatch.context() as scoped:
+ scoped.setattr(os, "scandir", denied_scandir)
+ scoped.setattr(os, "open", recording_open)
+ try:
+ with acquire:
+ entered = True
+ except rekey_lock_module.RekeySidecarIOError as error:
+ refusal = error
+
+ # Cleanup and assertions run after restoring filesystem operations.
+ assert data_dir in scan_attempts
+ assert not entered, "protected body entered despite an unlistable directory"
+ assert os.fspath(sidecar) not in open_attempts
+ assert os.fspath(target) not in open_attempts
+ assert isinstance(refusal, rekey_lock_module.RekeySidecarIOError)
+ assert refusal.operation is rekey_lock_module.RekeySidecarOperation.OPEN
+ assert refusal.errno == errno.EMLINK
+ assert refusal.db_path == db_path
+ for path in (sidecar, target):
+ status = path.stat()
+ assert (status.st_dev, status.st_ino) == identity
+ assert status.st_nlink == original.st_nlink
+ assert stat.S_IMODE(status.st_mode) == original_mode
+ assert path.read_bytes() == original_bytes
+
+
@pytest.mark.skipif(
os.name == "nt" or not hasattr(os, "link"),
reason="POSIX hard links required",