~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(backup): accept Windows paths in the ATTACH backup literal (#7198)
EnragedAntelopec4667cb7ecfc
diff --git a/changelog.d/7197.bugfix.md b/changelog.d/7197.bugfix.mdnew file mode 100644index 0000000000..7b6aad5774--- /dev/null+++ b/changelog.d/7197.bugfix.md@@ -0,0 +1 @@+Automatic database backups now work on Windows: the `ATTACH` backup path is normalised to forward slashes (which SQLite accepts on every platform) instead of being rejected as an unsafe character for containing the Windows separator.diff --git a/src/local_deep_research/database/backup/backup_service.py b/src/local_deep_research/database/backup/backup_service.pyindex 27088c79e0..28f06e7f23 100644--- a/src/local_deep_research/database/backup/backup_service.py+++ b/src/local_deep_research/database/backup/backup_service.py@@ -40,12 +40,16 @@ from ..sqlcipher_utils import (_user_locks: dict[str, threading.Lock] = {}_user_locks_lock = threading.Lock()-# Characters that must never appear in a backup path. ATTACH DATABASE takes a-# string literal and cannot be parameterized in SQLite/SQLCipher, so the path is-# interpolated into SQL. A single quote IS allowed — it is escaped by doubling-# (per the SQLite literal grammar) so an apostrophe in the data-dir path (a-# home dir named O'Brien, say) doesn't break backups. Backslash, double-quote,-# NUL and control chars are still rejected (never valid in a real backup path).+# Characters that must never appear in the validated backup path string.+# ATTACH DATABASE takes a string literal and cannot be parameterized in+# SQLite/SQLCipher, so the path is interpolated into SQL. A single quote IS+# allowed — it is escaped by doubling (per the SQLite literal grammar) so an+# apostrophe in the data-dir path (a home dir named O'Brien, say) doesn't+# break backups. Double-quote, NUL and control chars are rejected outright. A+# backslash is in the set, but never appears in the validated string in a real+# backup path: it is the Windows separator, normalised to "/" before this check+# (see _create_backup_impl); on POSIX a real separator is always "/", so a+# literal backslash there is conservatively rejected._UNSAFE_BACKUP_PATH_CHARS = frozenset('"\\\0\n\r\t')@@ -274,7 +278,15 @@ class BackupService:# parameterizable). Single quotes are escaped (see below); any# other unsafe char is rejected. Don't log the full path — it can# contain a username — only the offending characters.+ # Windows paths carry backslashes, which the ATTACH literal+ # rejects (see _UNSAFE_BACKUP_PATH_CHARS) - so automatic+ # backups failed on every Windows install. SQLite/SQLCipher+ # accept forward slashes on every platform, so normalise the+ # string used in the SQL statement only; ``temp_path`` (a+ # Path) keeps the real separators for filesystem work.temp_path_str = str(temp_path)+ if os.sep == "\\":+ temp_path_str = temp_path_str.replace("\\", "/")bad_chars = _UNSAFE_BACKUP_PATH_CHARS & set(temp_path_str)if bad_chars:raise ValueError(diff --git a/tests/database/backup/test_backup_service.py b/tests/database/backup/test_backup_service.pyindex e437a74456..24e042fef2 100644--- a/tests/database/backup/test_backup_service.py+++ b/tests/database/backup/test_backup_service.py@@ -6192,12 +6192,17 @@ class TestGlobHardeningIntegration:class TestUnsafeBackupPathChars:- """Tests for the widened SQL-injection character guard on the ATTACH path."""+ """Tests for the widened SQL-injection character guard on the ATTACH path.++ The backslash is deliberately not in the list below: it is the path+ separator on Windows, so it is normalised into the ATTACH literal there+ rather than rejected. Both platform outcomes are pinned by+ ``test_backup_windows_attach_path.py``.+ """@pytest.mark.parametrize("bad_char, label",[- ("\\", "backslash"),("\0", "null"),("\n", "newline"),("\r", "carriage_return"),@@ -6228,12 +6233,14 @@ class TestUnsafeBackupPathChars:label,tmp_path,):- """Reject backslash, NUL, CR, LF and tab in the backup path.+ """Reject NUL, CR, LF and tab in the backup path.``'`` is intentionally NOT rejected — it is escaped (doubled) in the- ATTACH literal so apostrophe data dirs work (#4808). ``temp_path`` is- server-generated, so the only way these reach the ``ATTACH DATABASE``- literal is via the backup directory, which is simulated here.+ ATTACH literal so apostrophe data dirs work (#4808), and a backslash is+ the host separator on Windows, so it is normalised there rather than+ rejected. ``temp_path`` is server-generated, so the only way these+ reach the ``ATTACH DATABASE`` literal is via the backup directory,+ which is simulated here."""db_dir = tmp_path / "encrypted_databases"db_dir.mkdir()diff --git a/tests/database/backup/test_backup_windows_attach_path.py b/tests/database/backup/test_backup_windows_attach_path.pynew file mode 100644index 0000000000..e31588ed3a--- /dev/null+++ b/tests/database/backup/test_backup_windows_attach_path.py@@ -0,0 +1,188 @@+"""A backup path carrying Windows separators must not be rejected.++`_UNSAFE_BACKUP_PATH_CHARS` rejects a backslash because the ATTACH DATABASE+path is interpolated into the SQL literal - but every Windows path contains+one, so automatic backups failed on Windows with "Backup path contains+characters not allowed in a SQLCipher ATTACH statement: '\\\\'". SQLite accepts+forward slashes on every platform, so the literal is normalised when the host+separator is a backslash (see the Windows-path regression for the same class of+bug in the journal-quality data sources).++The host separator is patched rather than the filesystem, because no Linux CI+runner can produce a Windows path any other way - and the ATTACH statement is+what the fix changes, so that literal is the assertion.+"""++from __future__ import annotations++import os+import re+from pathlib import Path+from unittest.mock import MagicMock, patch++import pytest++from local_deep_research.database.backup.backup_service import BackupService++_ATTACH_PATH = re.compile(r"ATTACH DATABASE '((?:[^']|'')*)'")+++def windows_host(monkeypatch):+ monkeypatch.setattr(os, "sep", "\\")+++def test_attach_statement_accepts_a_windows_backup_path(tmp_path, windows_host):+ db_dir = tmp_path / "encrypted_databases"+ db_dir.mkdir()+ db_file = db_dir / "ldr_user_win.db"+ db_file.write_bytes(b"x" * 1000)+ if os.name == "nt":+ backup_dir = tmp_path / "backups"+ else:+ backup_dir = tmp_path / "back\\ups"+ backup_dir.mkdir()++ attach_paths: list[str] = []++ def fake_execute(sql, *args, **kwargs):+ match = _ATTACH_PATH.search(sql) if isinstance(sql, str) else None+ if not match:+ return+ attach_paths.append(match.group(1))+ (backup_dir / Path(match.group(1)).name).write_bytes(+ b"backup_data" * 100+ )+ written = Path(match.group(1))+ if not written.exists():+ written.parent.mkdir(parents=True, exist_ok=True)+ written.write_bytes(b"backup_data" * 100)++ mock_cursor = MagicMock()+ mock_cursor.execute.side_effect = fake_execute+ mock_conn = MagicMock()+ mock_conn.cursor.return_value = mock_cursor++ with (+ patch(+ "local_deep_research.database.backup.backup_service"+ ".get_encrypted_database_path",+ return_value=db_dir,+ ),+ patch(+ "local_deep_research.database.backup.backup_service"+ ".get_user_database_filename",+ return_value=db_file.name,+ ),+ patch(+ "local_deep_research.database.backup.backup_service"+ ".get_user_backup_directory",+ return_value=backup_dir,+ ),+ patch(+ "local_deep_research.database.backup.backup_service"+ ".create_sqlcipher_connection",+ return_value=mock_conn,+ ),+ patch("shutil.disk_usage", return_value=MagicMock(free=10_000_000)),+ ):+ service = BackupService(+ username="winuser", password="pw", max_backups=3, max_age_days=7+ )+ with patch.object(service, "_verify_backup", return_value=True):+ result = service.create_backup(force=True)++ assert result.success, f"Backup failed: {result.error}"+ assert attach_paths, "no ATTACH DATABASE statement was executed"+ literal = attach_paths[0]+ # The literal must be the real temp path with separators normalised to+ # "/", not merely backslash-free: a corrupted normalisation (an injected+ # "/../", say) would pass a weaker "no backslash" check on its own.+ expected_prefix = str(backup_dir).replace("\\", "/") + "/"+ assert literal.startswith(expected_prefix), literal+ assert re.fullmatch(+ r"ldr_backup_\d{8}_\d{6}\.db\.tmp", literal[len(expected_prefix) :]+ ), literal+++def test_attach_statement_still_rejects_a_quote_unsafe_path(+ tmp_path, windows_host+):+ """Normalising separators must not widen the denylist."""+ db_dir = tmp_path / "encrypted_databases"+ db_dir.mkdir()+ db_file = db_dir / "ldr_user_bad.db"+ db_file.write_bytes(b"x" * 1000)+ backup_dir = Path(str(tmp_path) + os.sep + 'quo"te')++ with (+ patch(+ "local_deep_research.database.backup.backup_service"+ ".get_encrypted_database_path",+ return_value=db_dir,+ ),+ patch(+ "local_deep_research.database.backup.backup_service"+ ".get_user_database_filename",+ return_value=db_file.name,+ ),+ patch(+ "local_deep_research.database.backup.backup_service"+ ".get_user_backup_directory",+ return_value=backup_dir,+ ),+ patch(+ "local_deep_research.database.backup.backup_service"+ ".create_sqlcipher_connection",+ return_value=MagicMock(),+ ),+ patch("shutil.disk_usage", return_value=MagicMock(free=10_000_000)),+ ):+ service = BackupService(username="baduser", password="pw")+ result = service.create_backup(force=True)++ assert result.success is False+ assert "not allowed in a SQLCipher ATTACH statement" in result.error+++ os.sep == "\\",+ reason="needs a host where a backslash is a literal path character",+)+def test_posix_backslash_in_backup_path_is_still_rejected(tmp_path):+ """Normalising separators must not disable the POSIX denylist."""+ db_dir = tmp_path / "encrypted_databases"+ db_dir.mkdir()+ db_file = db_dir / "ldr_user_posix.db"+ db_file.write_bytes(b"x" * 1000)+ backup_dir = tmp_path / "back\\ups"+ backup_dir.mkdir()++ with (+ patch(+ "local_deep_research.database.backup.backup_service"+ ".get_encrypted_database_path",+ return_value=db_dir,+ ),+ patch(+ "local_deep_research.database.backup.backup_service"+ ".get_user_database_filename",+ return_value=db_file.name,+ ),+ patch(+ "local_deep_research.database.backup.backup_service"+ ".get_user_backup_directory",+ return_value=backup_dir,+ ),+ patch(+ "local_deep_research.database.backup.backup_service"+ ".create_sqlcipher_connection",+ return_value=MagicMock(),+ ),+ patch("shutil.disk_usage", return_value=MagicMock(free=10_000_000)),+ ):+ service = BackupService(username="posixuser", password="pw")+ result = service.create_backup(force=True)++ assert result.success is False+ assert "not allowed in a SQLCipher ATTACH statement" in result.error