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.

6.7 KB
Raw Blame
"""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 '((?:[^']|'')*)'")


@pytest.fixture
def windows_host(monkeypatch):
    monkeypatch.setattr(os, "sep", "\\")


@pytest.mark.parametrize("path_suffix", ["", "'quoted"])
def test_attach_statement_accepts_a_windows_backup_path(
    tmp_path, windows_host, path_suffix
):
    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 / f"backups{path_suffix}"
    else:
        backup_dir = tmp_path / f"back\\ups{path_suffix}"
    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.
    assert result.backup_path is not None
    assert result.backup_path.parent == backup_dir
    expected = (
        str(result.backup_path.with_suffix(".db.tmp"))
        .replace("\\", "/")
        .replace("'", "''")
    )
    assert literal == expected


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


@pytest.mark.skipif(
    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