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.

7.6 KB
Raw Blame
"""The optional plaintext report backup must not be exposed by a wide umask."""

import errno
import os
import stat
from pathlib import Path
from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.storage.database_with_file_backup import (
    DatabaseWithFileBackupStorage,
)
from local_deep_research.storage.file import FileReportStorage
from local_deep_research.security.file_write_verifier import write_file_verified


@pytest.mark.skipif(os.name != "posix", reason="POSIX mode bits")
def test_backup_keeps_flat_paths_but_restricts_directory_and_files(
    tmp_path, monkeypatch
):
    root = tmp_path / "research_outputs"
    root.mkdir()
    root.chmod(0o755)
    existing = root / "old.md"
    existing.write_text(
        "old content that is longer than the replacement", encoding="utf-8"
    )
    existing.chmod(0o644)
    monkeypatch.setattr(
        "local_deep_research.config.search_config.get_setting_from_snapshot",
        lambda *args, **kwargs: True,
    )

    old_umask = os.umask(0o022)
    try:
        storage = FileReportStorage(base_dir=root)
        assert stat.S_IMODE(root.stat().st_mode) == 0o700
        assert storage.save_report("old", "new content", {"query": "private"})
        assert storage.save_report("new", "another report")
    finally:
        os.umask(old_umask)

    assert existing.read_text(encoding="utf-8") == "new content"
    for filename in ("old.md", "old_metadata.json", "new.md"):
        assert stat.S_IMODE((root / filename).stat().st_mode) == 0o600


def test_backup_permission_failure_does_not_prevent_database_save(mock_session):
    with patch(
        "local_deep_research.storage.database_with_file_backup.FileReportStorage",
        side_effect=PermissionError("private mode unavailable"),
    ):
        storage = DatabaseWithFileBackupStorage(
            mock_session, enable_file_storage=True
        )
    storage.db_storage = MagicMock()
    storage.db_storage.save_report.return_value = True

    assert storage.file_storage is None
    assert storage.save_report("research", "saved in encrypted database")
    storage.db_storage.save_report.assert_called_once()


@pytest.mark.skipif(os.name != "posix", reason="POSIX mode bits")
def test_chmod_that_does_not_stick_refuses_directory_and_degrades_to_database(
    tmp_path, monkeypatch, mock_session, loguru_caplog
):
    root = tmp_path / "research_outputs"
    root.mkdir()
    root.chmod(0o755)
    monkeypatch.setattr(Path, "chmod", lambda self, mode: None)

    with pytest.raises(PermissionError, match="not private"):
        FileReportStorage(base_dir=root)

    monkeypatch.setattr(
        "local_deep_research.storage.file.get_research_outputs_directory",
        lambda: root,
    )
    with loguru_caplog.at_level("WARNING"):
        storage = DatabaseWithFileBackupStorage(
            mock_session, enable_file_storage=True
        )
    # The log names the cause and the remedy, not just "unavailable".
    assert "did not keep mode 0700" in loguru_caplog.text
    assert "Docker named volume" in loguru_caplog.text
    storage.db_storage = MagicMock()
    storage.db_storage.save_report.return_value = True

    assert storage.file_storage is None
    assert storage.save_report("research", "saved in encrypted database")
    storage.db_storage.save_report.assert_called_once()


def _refuse_chmod(message):
    def refuse(self, mode, **kwargs):
        raise PermissionError(errno.EPERM, message)

    return refuse


@pytest.mark.skipif(os.name != "posix", reason="POSIX ownership")
@pytest.mark.parametrize(
    "chmod_refused", [False, True], ids=["chmod-allowed", "chmod-refused"]
)
def test_directory_owned_by_another_account_is_refused(
    tmp_path, monkeypatch, mock_session, chmod_refused
):
    """A 0700 directory owned by another account stays readable by it.

    Root can chmod such a directory and an unprivileged process cannot;
    either way the refusal names the owner rather than a raw chmod error.
    """
    root = tmp_path / "research_outputs"
    root.mkdir()
    root.chmod(0o755)
    other_uid = root.stat().st_uid + 1
    monkeypatch.setattr(os, "geteuid", lambda: other_uid)
    if chmod_refused:
        monkeypatch.setattr(
            Path, "chmod", _refuse_chmod("Operation not permitted")
        )

    with pytest.raises(PermissionError, match="owned by uid"):
        FileReportStorage(base_dir=root)

    monkeypatch.setattr(
        "local_deep_research.storage.file.get_research_outputs_directory",
        lambda: root,
    )
    storage = DatabaseWithFileBackupStorage(
        mock_session, enable_file_storage=True
    )
    assert storage.file_storage is None


@pytest.mark.skipif(os.name != "posix", reason="POSIX mode bits")
def test_chmod_error_on_own_directory_keeps_its_cause(tmp_path, monkeypatch):
    root = tmp_path / "research_outputs"
    root.mkdir()
    monkeypatch.setattr(Path, "chmod", _refuse_chmod("immutable directory"))

    with pytest.raises(PermissionError, match="immutable directory"):
        FileReportStorage(base_dir=root)


@pytest.mark.skipif(os.name != "posix", reason="POSIX mode bits")
def test_mode_failure_preserves_existing_report_contents(tmp_path, monkeypatch):
    report = tmp_path / "existing.md"
    report.write_text("old report", encoding="utf-8")
    monkeypatch.setattr(
        "local_deep_research.config.search_config.get_setting_from_snapshot",
        lambda *args, **kwargs: True,
    )

    with patch(
        "local_deep_research.security.file_write_verifier.os.fchmod",
        side_effect=PermissionError("cannot restrict file mode"),
    ):
        with pytest.raises(PermissionError):
            write_file_verified(
                report,
                "replacement",
                "report.enable_file_backup",
                permissions=0o600,
            )

    assert report.read_text(encoding="utf-8") == "old report"


@pytest.mark.skipif(os.name != "posix", reason="POSIX mode bits")
def test_mode_failure_closes_the_descriptor(tmp_path, monkeypatch):
    report = tmp_path / "existing.md"
    report.write_text("old report", encoding="utf-8")
    monkeypatch.setattr(
        "local_deep_research.config.search_config.get_setting_from_snapshot",
        lambda *args, **kwargs: True,
    )
    given, closed = [], []
    real_close = os.close

    def failing_fchmod(fd, mode):
        given.append(fd)
        raise PermissionError("cannot restrict file mode")

    def recording_close(fd):
        closed.append(fd)
        real_close(fd)

    monkeypatch.setattr(os, "fchmod", failing_fchmod)
    monkeypatch.setattr(os, "close", recording_close)
    with pytest.raises(PermissionError):
        write_file_verified(
            report,
            "replacement",
            "report.enable_file_backup",
            permissions=0o600,
        )

    assert len(given) == 1
    assert given[0] in closed, "the descriptor of a failed write was leaked"


@pytest.mark.skipif(
    not hasattr(os, "O_NOFOLLOW"), reason="O_NOFOLLOW unavailable"
)
def test_private_write_refuses_a_leaf_symlink(tmp_path, monkeypatch):
    """permissions= keeps O_NOFOLLOW, so fchmod never reaches a link target."""
    monkeypatch.setattr(
        "local_deep_research.config.search_config.get_setting_from_snapshot",
        lambda *args, **kwargs: True,
    )
    victim = tmp_path / "victim.txt"
    victim.write_text("victim", encoding="utf-8")
    victim.chmod(0o644)
    link = tmp_path / "report.md"
    link.symlink_to(victim)

    with pytest.raises(OSError):
        write_file_verified(
            link, "replacement", "report.enable_file_backup", permissions=0o600
        )

    assert victim.read_text(encoding="utf-8") == "victim"
    assert stat.S_IMODE(victim.stat().st_mode) == 0o644