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: apply glob hardening to backup-status API route (follow-up to #3098) (#4663)

LearningCircuit970bc7d2c44a
diff --git a/src/local_deep_research/web/routes/settings_routes.py b/src/local_deep_research/web/routes/settings_routes.py
index 271635ff1f..de5a0c89e6 100644
--- a/src/local_deep_research/web/routes/settings_routes.py
+++ b/src/local_deep_research/web/routes/settings_routes.py
@@ -2100,6 +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
backup_dir = get_user_backup_directory(username)
@@ -2107,6 +2108,11 @@ def api_get_backup_status():
backup_list = []
total_size = 0
for b in backup_dir.glob("ldr_backup_*.db"):
+ # 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):
+ continue
try:
stat = b.stat()
total_size += stat.st_size
diff --git a/tests/web/routes/test_backup_status_api.py b/tests/web/routes/test_backup_status_api.py
index 721cb8945b..847e83904e 100644
--- a/tests/web/routes/test_backup_status_api.py
+++ b/tests/web/routes/test_backup_status_api.py
@@ -116,3 +116,58 @@ class TestBackupStatusResponseShape:
total = sum(b.stat().st_size for b in backups)
assert total == 3000
+
+
+class TestBackupStatusEndpointGlobHardening:
+ """Real endpoint tests that drive GET /settings/api/backup-status.
+
+ Unlike the response-shape tests above (which replicate the glob), these
+ actually invoke the route so they verify the symlink hardening is wired
+ into the handler, not just that pathlib behaves.
+ """
+
+ def test_symlinked_backup_entry_is_excluded(
+ self, authenticated_client, tmp_path
+ ):
+ """A symlink planted in the backup dir must not appear in the API
+ response, so an external target's metadata is never reported."""
+ from local_deep_research.config.paths import (
+ get_user_backup_directory,
+ )
+
+ with authenticated_client.session_transaction() as sess:
+ username = sess.get("username")
+ assert username, "authenticated_client should have a logged-in user"
+
+ backup_dir = get_user_backup_directory(username)
+ backup_dir.mkdir(parents=True, exist_ok=True)
+
+ # A legitimate backup.
+ real = backup_dir / "ldr_backup_20260101_120000.db"
+ real.write_bytes(b"x" * 2048)
+
+ # A malicious symlink whose name matches the glob but targets a file
+ # outside the backup directory.
+ outside = tmp_path / "outside_secret.db"
+ outside.write_bytes(b"secret-data-of-known-size")
+ evil = backup_dir / "ldr_backup_29991231_235959.db"
+ evil.symlink_to(outside)
+
+ resp = authenticated_client.get("/settings/api/backup-status")
+ assert resp.status_code == 200
+ data = resp.get_json()
+ names = [b["filename"] for b in data["backups"]]
+
+ assert "ldr_backup_20260101_120000.db" in names
+ assert "ldr_backup_29991231_235959.db" not in names
+ # The aggregate must never count more than what is listed — guards a
+ # future regression where an entry is filtered from the list but its
+ # (external) target size still leaks into total_size_bytes. Asserted as
+ # an invariant (not an absolute byte count) so an async post-login
+ # backup landing a real file in this dir can't make it flaky.
+ assert data["total_size_bytes"] == sum(
+ b["size_bytes"] for b in data["backups"]
+ )
+ # Listing must be non-destructive: the symlink and its target survive.
+ assert evil.is_symlink()
+ assert outside.exists()