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.

security: harden warning_checks backup glob + make is_safe_glob_result shared (#4682)

LearningCircuitc6a8eff206ac
diff --git a/src/local_deep_research/database/backup/backup_service.py b/src/local_deep_research/database/backup/backup_service.py
index aa91b75aa0..e3b042d181 100644
--- a/src/local_deep_research/database/backup/backup_service.py
+++ b/src/local_deep_research/database/backup/backup_service.py
@@ -62,7 +62,7 @@ def _get_user_lock(username: str) -> threading.Lock:
return _user_locks[username]
-def _is_safe_glob_result(path: Path, base_dir: Path) -> bool:
+def is_safe_glob_result(path: Path, base_dir: Path) -> bool:
"""Validate that a glob result is safe to use.
Rejects symlinks and paths that resolve outside the expected
@@ -175,7 +175,7 @@ class BackupService:
existing_today = [
f
for f in self.backup_dir.glob(f"ldr_backup_{today}_*.db")
- if _is_safe_glob_result(f, self.backup_dir)
+ if is_safe_glob_result(f, self.backup_dir)
]
if existing_today:
latest = max(existing_today, key=lambda p: p.name)
@@ -434,7 +434,7 @@ class BackupService:
for tmp_file in [
f
for f in self.backup_dir.glob("ldr_backup_*.db.tmp")
- if _is_safe_glob_result(f, self.backup_dir)
+ if is_safe_glob_result(f, self.backup_dir)
]:
try:
mtime = datetime.fromtimestamp(
@@ -463,7 +463,7 @@ class BackupService:
key=_safe_mtime,
reverse=True,
)
- if p.exists() and _is_safe_glob_result(p, self.backup_dir)
+ if p.exists() and is_safe_glob_result(p, self.backup_dir)
]
for i, backup in enumerate(backups):
@@ -525,7 +525,7 @@ class BackupService:
[
f
for f in self.backup_dir.glob("ldr_backup_*.db")
- if _is_safe_glob_result(f, self.backup_dir)
+ if is_safe_glob_result(f, self.backup_dir)
],
key=_safe_mtime_list,
reverse=True,
@@ -579,7 +579,7 @@ class BackupService:
for tmp_file in [
f
for f in self.backup_dir.glob("ldr_backup_*.db.tmp")
- if _is_safe_glob_result(f, self.backup_dir)
+ if is_safe_glob_result(f, self.backup_dir)
]:
try:
tmp_file.unlink()
@@ -613,7 +613,7 @@ class BackupService:
key=_safe_mtime_latest,
reverse=True,
)
- if p.exists() and _is_safe_glob_result(p, self.backup_dir)
+ if p.exists() and is_safe_glob_result(p, self.backup_dir)
]
return backups[0] if backups else None
except Exception:
diff --git a/src/local_deep_research/web/routes/settings_routes.py b/src/local_deep_research/web/routes/settings_routes.py
index de5a0c89e6..25ef2c7e67 100644
--- a/src/local_deep_research/web/routes/settings_routes.py
+++ b/src/local_deep_research/web/routes/settings_routes.py
@@ -2100,7 +2100,7 @@ def api_get_backup_status():
return jsonify({"error": "Not authenticated"}), 401
from ...utilities.formatting import human_size
- from ...database.backup.backup_service import _is_safe_glob_result
+ from ...database.backup.backup_service import is_safe_glob_result
backup_dir = get_user_backup_directory(username)
@@ -2111,7 +2111,7 @@ def api_get_backup_status():
# Skip symlinks / entries resolving outside backup_dir so a
# planted symlink can't have its (external) target's metadata
# reported here — same glob-hardening applied in BackupService.
- if not _is_safe_glob_result(b, backup_dir):
+ if not is_safe_glob_result(b, backup_dir):
continue
try:
stat = b.stat()
diff --git a/src/local_deep_research/web/warning_checks/__init__.py b/src/local_deep_research/web/warning_checks/__init__.py
index 5335f3d63b..5faf824fbf 100644
--- a/src/local_deep_research/web/warning_checks/__init__.py
+++ b/src/local_deep_research/web/warning_checks/__init__.py
@@ -259,6 +259,9 @@ def calculate_warnings() -> List[dict]:
try:
from ...config.paths import get_user_backup_directory
from ...utilities.formatting import human_size
+ from ...database.backup.backup_service import (
+ is_safe_glob_result,
+ )
username = session.get("username")
if username:
@@ -266,6 +269,11 @@ def calculate_warnings() -> List[dict]:
total_size = 0
backup_count = 0
for f in backup_dir.glob("ldr_backup_*.db"):
+ # Skip symlinks / entries resolving outside backup_dir
+ # so a planted symlink can't inflate the count/size
+ # shown in warnings — same hardening as BackupService.
+ if not is_safe_glob_result(f, backup_dir):
+ continue
try:
total_size += f.stat().st_size
backup_count += 1
diff --git a/tests/database/backup/test_backup_service.py b/tests/database/backup/test_backup_service.py
index 857148da53..2ff0df74b2 100644
--- a/tests/database/backup/test_backup_service.py
+++ b/tests/database/backup/test_backup_service.py
@@ -5981,7 +5981,7 @@ class TestDailyBackupLimit:
class TestIsSafeGlobResult:
- """Unit tests for the ``_is_safe_glob_result`` glob-hardening helper.
+ """Unit tests for the ``is_safe_glob_result`` glob-hardening helper.
This helper backs the symlink / path-traversal filtering that is applied
to every backup glob site. These tests exercise the helper directly,
@@ -5991,7 +5991,7 @@ class TestIsSafeGlobResult:
def test_accepts_regular_child_file(self, tmp_path):
"""A regular (non-symlink) file inside base_dir is accepted."""
from local_deep_research.database.backup.backup_service import (
- _is_safe_glob_result,
+ is_safe_glob_result,
)
base_dir = tmp_path / "backups"
@@ -5999,12 +5999,12 @@ class TestIsSafeGlobResult:
backup = base_dir / "ldr_backup_20250101_120000.db"
backup.write_bytes(b"data")
- assert _is_safe_glob_result(backup, base_dir) is True
+ assert is_safe_glob_result(backup, base_dir) is True
def test_rejects_symlink_pointing_outside(self, tmp_path):
"""A symlink that escapes base_dir is rejected."""
from local_deep_research.database.backup.backup_service import (
- _is_safe_glob_result,
+ is_safe_glob_result,
)
base_dir = tmp_path / "backups"
@@ -6015,7 +6015,7 @@ class TestIsSafeGlobResult:
link = base_dir / "ldr_backup_evil.db"
link.symlink_to(outside)
- assert _is_safe_glob_result(link, base_dir) is False
+ assert is_safe_glob_result(link, base_dir) is False
def test_rejects_symlink_even_when_target_inside(self, tmp_path):
"""Symlinks are rejected outright, even pointing back inside base_dir.
@@ -6024,7 +6024,7 @@ class TestIsSafeGlobResult:
all" — this documents that intentional behavior.
"""
from local_deep_research.database.backup.backup_service import (
- _is_safe_glob_result,
+ is_safe_glob_result,
)
base_dir = tmp_path / "backups"
@@ -6035,12 +6035,12 @@ class TestIsSafeGlobResult:
link = base_dir / "ldr_backup_link.db"
link.symlink_to(real)
- assert _is_safe_glob_result(link, base_dir) is False
+ assert is_safe_glob_result(link, base_dir) is False
def test_rejects_path_resolving_outside_base(self, tmp_path):
"""A non-symlink path that resolves outside base_dir is rejected."""
from local_deep_research.database.backup.backup_service import (
- _is_safe_glob_result,
+ is_safe_glob_result,
)
base_dir = tmp_path / "backups"
@@ -6050,7 +6050,7 @@ class TestIsSafeGlobResult:
stray = other_dir / "ldr_backup_20250101_120000.db"
stray.write_bytes(b"data")
- assert _is_safe_glob_result(stray, base_dir) is False
+ assert is_safe_glob_result(stray, base_dir) is False
def test_accepts_real_file_through_symlinked_base_dir(self, tmp_path):
"""Real files are kept even when base_dir is reached via a symlink.
@@ -6060,7 +6060,7 @@ class TestIsSafeGlobResult:
``$HOME``) must NOT cause legitimate backups to be silently dropped.
"""
from local_deep_research.database.backup.backup_service import (
- _is_safe_glob_result,
+ is_safe_glob_result,
)
real_base = tmp_path / "real_backups"
@@ -6074,7 +6074,7 @@ class TestIsSafeGlobResult:
# The file reached through the symlinked parent is itself a real file.
via_link = symlinked_base / "ldr_backup_20250101_120000.db"
assert not via_link.is_symlink()
- assert _is_safe_glob_result(via_link, symlinked_base) is True
+ assert is_safe_glob_result(via_link, symlinked_base) is True
class TestGlobHardeningIntegration:
diff --git a/tests/web/warning_checks/test_orchestrator.py b/tests/web/warning_checks/test_orchestrator.py
index 953be8862c..a1b5245fc1 100644
--- a/tests/web/warning_checks/test_orchestrator.py
+++ b/tests/web/warning_checks/test_orchestrator.py
@@ -526,3 +526,37 @@ class TestCalculateWarningsNoneProvider:
# Should not crash — outer except catches AttributeError from None.lower()
assert isinstance(result, list)
+
+
+class TestCalculateWarningsBackupGlobHardening:
+ """The backup glob in calculate_warnings() must skip symlinks, so a
+ planted symlink cannot inflate the backup count/size used for warnings.
+ """
+
+ def test_symlink_is_not_counted_as_a_backup(self, tmp_path):
+ from local_deep_research.web.warning_checks import calculate_warnings
+
+ backup_dir = tmp_path / "backups"
+ backup_dir.mkdir()
+
+ # The ONLY entry matching the glob is a symlink to an existing file
+ # outside the backup dir. With the hardening it is skipped, leaving
+ # zero real backups; without it, stat() would follow the link and
+ # count it.
+ outside = tmp_path / "outside_secret.db"
+ outside.write_bytes(b"x" * 4096)
+ (backup_dir / "ldr_backup_29991231_235959.db").symlink_to(outside)
+
+ settings_manager = _make_settings_manager() # backup.enabled=True
+ with _patch_orchestrator(settings_manager):
+ with patch(
+ "local_deep_research.config.paths.get_user_backup_directory",
+ return_value=backup_dir,
+ ):
+ warnings = calculate_warnings()
+
+ types = {w["type"] for w in warnings}
+ # Symlink excluded -> backup_count == 0 -> the "no backups" warning
+ # fires and no "backup_info" (which would carry the link target size).
+ assert "no_backups" in types
+ assert "backup_info" not in types