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.9 KB
Raw Blame
"""Regression tests: write_file_verified refuses to follow a symlink leaf.

The verified-write helper is the chokepoint the config/report writers use.
The library PDF write path also writes through it, but into a freshly named
staged file that it then moves into place, so its refusal of a symlinked
destination comes from its own ``is_symlink()`` check
(``PDFStorageManager._save_to_filesystem``), not from these.
Previously the helper opened the destination with a
plain ``open(path, mode)``, so a symlink pre-planted at the target leaf was
followed and the write landed on the link's target -- potentially a file
outside the intended directory. The read path already refused symlinks; these
tests pin the write side by opening with ``O_NOFOLLOW``.

On platforms with O_NOFOLLOW, a symlinked destination must raise and must
never modify the link's target. Legitimate writes to a regular/new path must
still succeed with the intended content and semantics.
"""

import os
from unittest.mock import patch

import pytest

from local_deep_research.security.file_write_verifier import (
    write_file_verified,
    write_json_verified,
)

_GATE = "test.allow_write"
_SNAP = {_GATE: True}


# ---------------------------------------------------------------------------
# Happy path -- normal writes still work
# ---------------------------------------------------------------------------


def test_text_write_to_new_path_succeeds(tmp_path):
    target = tmp_path / "report.md"
    write_file_verified(
        target, "# hello", _GATE, context="t", settings_snapshot=_SNAP
    )
    assert target.read_text(encoding="utf-8") == "# hello"


def test_binary_write_to_new_path_succeeds(tmp_path):
    target = tmp_path / "paper.pdf"
    write_file_verified(
        target,
        b"%PDF-1.4\n",
        _GATE,
        mode="wb",
        context="t",
        settings_snapshot=_SNAP,
    )
    assert target.read_bytes() == b"%PDF-1.4\n"


def test_overwrite_existing_regular_file_succeeds(tmp_path):
    target = tmp_path / "report.md"
    target.write_text("old", encoding="utf-8")
    write_file_verified(
        target, "new", _GATE, context="t", settings_snapshot=_SNAP
    )
    assert target.read_text(encoding="utf-8") == "new"


def test_write_json_verified_still_writes(tmp_path):
    target = tmp_path / "results.json"
    write_json_verified(
        target, {"accuracy": 0.9}, _GATE, settings_snapshot=_SNAP
    )
    assert '"accuracy"' in target.read_text(encoding="utf-8")


# ---------------------------------------------------------------------------
# Symlink refusal -- the write must not follow a pre-planted link
# ---------------------------------------------------------------------------


@pytest.mark.skipif(
    not hasattr(os, "O_NOFOLLOW"), reason="O_NOFOLLOW unavailable"
)
@pytest.mark.parametrize("mode", ["w", "wb", "a", "ab", "r+", "r+b"])
def test_write_refuses_symlink_pointing_outside(tmp_path, mode):
    outside = tmp_path / "outside"
    outside.mkdir()
    canary = outside / "canary.txt"
    canary.write_text("DO NOT OVERWRITE", encoding="utf-8")

    workdir = tmp_path / "work"
    workdir.mkdir()
    link = workdir / "report.md"
    try:
        link.symlink_to(canary)
    except (OSError, NotImplementedError):
        pytest.skip("Cannot create symlinks in this environment")

    with pytest.raises(OSError):
        write_file_verified(
            link,
            b"CHANGED" if "b" in mode else "CHANGED",
            _GATE,
            mode=mode,
            context="t",
            settings_snapshot=_SNAP,
        )

    # The symlink's target must be untouched.
    assert canary.read_text(encoding="utf-8") == "DO NOT OVERWRITE"


@pytest.mark.skipif(
    not hasattr(os, "O_NOFOLLOW"), reason="O_NOFOLLOW unavailable"
)
def test_binary_write_refuses_symlink(tmp_path):
    outside = tmp_path / "outside"
    outside.mkdir()
    canary = outside / "canary.pdf"
    canary.write_bytes(b"ORIGINAL")

    link = tmp_path / "planted.pdf"
    try:
        link.symlink_to(canary)
    except (OSError, NotImplementedError):
        pytest.skip("Cannot create symlinks in this environment")

    with pytest.raises(OSError):
        write_file_verified(
            link,
            b"PWNED",
            _GATE,
            mode="wb",
            context="t",
            settings_snapshot=_SNAP,
        )

    assert canary.read_bytes() == b"ORIGINAL"


@pytest.mark.skipif(
    not hasattr(os, "O_NOFOLLOW"), reason="O_NOFOLLOW unavailable"
)
def test_write_refuses_symlink_pointing_inside_same_dir(tmp_path):
    """Even a link whose target is a sibling regular file is refused: the
    write side never dereferences a symlink leaf."""
    real = tmp_path / "real.md"
    real.write_text("REAL", encoding="utf-8")
    link = tmp_path / "link.md"
    try:
        link.symlink_to(real)
    except (OSError, NotImplementedError):
        pytest.skip("Cannot create symlinks in this environment")

    with pytest.raises(OSError):
        write_file_verified(
            link, "PWNED", _GATE, context="t", settings_snapshot=_SNAP
        )

    assert real.read_text(encoding="utf-8") == "REAL"


@pytest.mark.parametrize(
    ("mode", "expected"),
    [
        ("r+", b"NEEP TAIL"),
        ("r+b", b"NEEP TAIL"),
        ("w+", b"N"),
        ("a+", b"KEEP TAILN"),
        ("ab", b"KEEP TAILN"),
    ],
)
def test_write_preserves_update_and_append_semantics(tmp_path, mode, expected):
    target = tmp_path / "existing.txt"
    target.write_bytes(b"KEEP TAIL")
    write_file_verified(
        target,
        b"N" if "b" in mode else "N",
        _GATE,
        mode=mode,
        settings_snapshot=_SNAP,
    )
    assert target.read_bytes() == expected


@pytest.mark.parametrize("mode", ["x", "xb", "x+"])
def test_exclusive_write_refuses_existing_file(tmp_path, mode):
    target = tmp_path / "existing.txt"
    target.write_bytes(b"KEEP")
    with pytest.raises(FileExistsError):
        write_file_verified(
            target,
            b"N" if "b" in mode else "N",
            _GATE,
            mode=mode,
            settings_snapshot=_SNAP,
        )
    assert target.read_bytes() == b"KEEP"


@pytest.mark.parametrize("mode", ["wbb", "wa", "qq"])
@pytest.mark.parametrize("exists", [False, True])
def test_invalid_mode_fails_before_opening_file(tmp_path, mode, exists):
    target = tmp_path / "report.txt"
    if exists:
        target.write_bytes(b"KEEP")
    with patch(
        "local_deep_research.security.file_write_verifier.os.open",
        side_effect=AssertionError("invalid mode must not open a descriptor"),
    ) as open_fd:
        with pytest.raises(ValueError):
            write_file_verified(
                target, "N", _GATE, mode=mode, settings_snapshot=_SNAP
            )
        open_fd.assert_not_called()
    assert target.exists() is exists
    if exists:
        assert target.read_bytes() == b"KEEP"


@pytest.mark.skipif(os.name != "posix", reason="POSIX permission bits")
def test_new_file_respects_group_writable_umask(tmp_path):
    target = tmp_path / "shared.txt"
    previous_umask = os.umask(0o002)
    try:
        write_file_verified(target, "N", _GATE, settings_snapshot=_SNAP)
    finally:
        os.umask(previous_umask)
    assert target.stat().st_mode & 0o777 == 0o664


def test_text_write_uses_normal_newline_translation(tmp_path):
    target = tmp_path / "lines.txt"
    write_file_verified(target, "one\ntwo\n", _GATE, settings_snapshot=_SNAP)
    assert target.read_bytes() == f"one{os.linesep}two{os.linesep}".encode()


@pytest.mark.skipif(
    not hasattr(os, "O_NOFOLLOW"), reason="O_NOFOLLOW unavailable"
)
def test_write_refuses_dangling_symlink(tmp_path):
    destination = tmp_path / "not-created.txt"
    link = tmp_path / "link.txt"
    try:
        link.symlink_to(destination)
    except (OSError, NotImplementedError):
        pytest.skip("Cannot create symlinks in this environment")
    with pytest.raises(OSError):
        write_file_verified(link, "N", _GATE, settings_snapshot=_SNAP)
    assert not destination.exists()
    assert link.is_symlink()