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.

13.9 KB
Raw Blame
from __future__ import annotations

import importlib
import os
import subprocess
from pathlib import Path
from unittest.mock import patch

import pytest
from loguru import logger

from tests.test_utils import restored_loguru_state


HOSTILE_QUERY = "../../etc/passwd; $(rm -rf /); `cat /etc/shadow` \u202eevil"


def _outside_canaries(tmp_path: Path) -> tuple[Path, Path]:
    outside = tmp_path / "outside"
    outside.mkdir()
    file_canary = outside / "canary.txt"
    pkl_canary = outside / "canary.pkl"
    file_canary.write_text("outside data")
    pkl_canary.write_bytes(b"outside legacy plaintext")
    return file_canary, pkl_canary


def _outside_files(tmp_path: Path, data_dir: Path) -> set[Path]:
    data_root = data_dir.resolve()
    return {
        path.relative_to(tmp_path)
        for path in tmp_path.rglob("*")
        if path.is_file() and not path.resolve().is_relative_to(data_root)
    }


def _forbid_subprocess(
    monkeypatch: pytest.MonkeyPatch,
) -> list[tuple[str, tuple[object, ...], dict[str, object]]]:
    calls: list[tuple[str, tuple[object, ...], dict[str, object]]] = []

    def forbidden(name: str):
        def _record(*args, **kwargs):
            calls.append((name, args, kwargs))
            raise AssertionError(f"subprocess invocation reached {name}")

        return _record

    for name in ("Popen", "run", "call", "check_call", "check_output"):
        monkeypatch.setattr(subprocess, name, forbidden(name))
    monkeypatch.setattr(os, "system", forbidden("os.system"))
    return calls


def _configure_file_sink(
    monkeypatch: pytest.MonkeyPatch,
    tmp_path: Path,
    name: str,
    *,
    debug: bool = False,
) -> tuple[Path, object]:
    import local_deep_research.utilities.log_utils as log_utils

    log_dir = tmp_path / "data" / "logs"
    monkeypatch.setattr(log_utils, "_LOG_DIR", log_dir)
    monkeypatch.setenv("LDR_ENABLE_FILE_LOGGING", "true")
    log_utils.config_logger(name, debug=debug)
    return log_dir / f"{name}.log", log_utils


@pytest.fixture(autouse=True)
def _isolated_loguru_state():
    """Keep each test's logging configuration out of the rest of the session.

    ``config_logger()`` and ``run_server()`` (through
    ``configure_mcp_logging()``) replace loguru's process-wide handler table,
    install the credential-redaction patcher and enable the
    ``local_deep_research`` namespace, and monkeypatch undoes none of it. A
    leaked patcher rewrote the ``***REDACTED***`` marker that
    ``test_settings_logger_non_dict_redaction.py`` asserts. Autouse, so tests
    that reach those calls cannot forget to opt in.
    ``restored_loguru_state()`` detaches the session's handlers for the test,
    then removes the sinks the test added and puts the original patcher,
    activation and handlers back.
    """
    with restored_loguru_state():
        yield


@pytest.mark.parametrize(
    ("tool_name", "backend_name"),
    [
        ("quick_research", "ldr_quick_summary"),
        ("detailed_research", "ldr_detailed_research"),
        ("generate_report", "ldr_generate_report"),
    ],
)
def test_mcp_research_tools_keep_hostile_query_outside_data_dir(
    tmp_path, monkeypatch, tool_name, backend_name
):
    import local_deep_research.mcp.server as server

    data_dir = tmp_path / "data"
    monkeypatch.setenv("LDR_DATA_DIR", str(data_dir))
    file_canary, pkl_canary = _outside_canaries(tmp_path)
    before = _outside_files(tmp_path, data_dir)
    calls = _forbid_subprocess(monkeypatch)

    with patch.object(server, backend_name, return_value={}) as backend:
        result = getattr(server, tool_name)(HOSTILE_QUERY)

    assert result["status"] == "success"
    assert backend.call_args.args[0] == HOSTILE_QUERY
    assert calls == []
    assert _outside_files(tmp_path, data_dir) == before
    assert file_canary.read_text() == "outside data"
    assert pkl_canary.read_bytes() == b"outside legacy plaintext"


def test_mcp_analyze_documents_rejects_hostile_collection_before_backend(
    tmp_path, monkeypatch
):
    import local_deep_research.mcp.server as server

    data_dir = tmp_path / "data"
    monkeypatch.setenv("LDR_DATA_DIR", str(data_dir))
    file_canary, pkl_canary = _outside_canaries(tmp_path)
    before = _outside_files(tmp_path, data_dir)
    calls = _forbid_subprocess(monkeypatch)

    with patch.object(server, "ldr_analyze_documents") as backend:
        result = server.analyze_documents(HOSTILE_QUERY, "../../outside")

    assert result["status"] == "error"
    assert result["error_type"] == "validation_error"
    backend.assert_not_called()
    assert calls == []
    assert _outside_files(tmp_path, data_dir) == before
    assert file_canary.read_text() == "outside data"
    assert pkl_canary.read_bytes() == b"outside legacy plaintext"


def test_mcp_search_rejects_hostile_engine_without_subprocess_or_engine_creation(
    tmp_path, monkeypatch
):
    import local_deep_research.mcp.server as server
    import local_deep_research.web_search_engines.search_engine_factory as factory

    data_dir = tmp_path / "data"
    monkeypatch.setenv("LDR_DATA_DIR", str(data_dir))
    file_canary, pkl_canary = _outside_canaries(tmp_path)
    before = _outside_files(tmp_path, data_dir)
    calls = _forbid_subprocess(monkeypatch)
    engine_calls: list[tuple[tuple[object, ...], dict[str, object]]] = []

    def no_engine(*args, **kwargs):
        engine_calls.append((args, kwargs))
        raise AssertionError("hostile engine reached engine construction")

    monkeypatch.setattr(factory, "create_search_engine", no_engine)

    result = server.search(HOSTILE_QUERY, "wikipedia; $(id)")

    assert result["status"] == "error"
    assert result["error_type"] == "validation_error"
    assert engine_calls == []
    assert calls == []
    assert _outside_files(tmp_path, data_dir) == before
    assert file_canary.read_text() == "outside data"
    assert pkl_canary.read_bytes() == b"outside legacy plaintext"


@pytest.mark.parametrize(
    ("tool_name", "kwargs"),
    [
        ("quick_research", {"iterations": "1; touch outside"}),
        ("detailed_research", {"questions_per_iteration": "$(id)"}),
        ("generate_report", {"searches_per_section": "2 && id"}),
        (
            "analyze_documents",
            {"collection_name": "safe", "max_results": "1; id"},
        ),
    ],
)
def test_mcp_hostile_numeric_metadata_is_rejected_before_settings_snapshot(
    tmp_path, monkeypatch, tool_name, kwargs
):
    import local_deep_research.mcp.server as server

    data_dir = tmp_path / "data"
    monkeypatch.setenv("LDR_DATA_DIR", str(data_dir))
    file_canary, pkl_canary = _outside_canaries(tmp_path)
    before = _outside_files(tmp_path, data_dir)
    calls = _forbid_subprocess(monkeypatch)

    with patch.object(server, "create_settings_snapshot") as snapshot:
        result = getattr(server, tool_name)("safe query", **kwargs)

    assert result["status"] == "error"
    assert result["error_type"] == "validation_error"
    snapshot.assert_not_called()
    assert calls == []
    assert _outside_files(tmp_path, data_dir) == before
    assert file_canary.read_text() == "outside data"
    assert pkl_canary.read_bytes() == b"outside legacy plaintext"


def test_mcp_run_server_migrates_only_its_data_dir_cache(tmp_path, monkeypatch):
    import local_deep_research.mcp.server as server

    data_dir = tmp_path / "data"
    monkeypatch.setenv("LDR_DATA_DIR", str(data_dir))
    cache_root = data_dir / "cache" / "rag_indices"
    cache_root.mkdir(parents=True)
    legacy_pkl = cache_root / "legacy.pkl"
    legacy_pkl.write_bytes(b"unreadable legacy pickle")
    _, outside_pkl = _outside_canaries(tmp_path)
    transports: list[str] = []
    monkeypatch.setattr(
        server.mcp, "run", lambda *, transport: transports.append(transport)
    )

    server.run_server()

    assert not legacy_pkl.exists()
    assert outside_pkl.read_bytes() == b"outside legacy plaintext"
    assert transports == ["stdio"]


def test_mcp_run_server_refuses_symlinked_cache_root(tmp_path, monkeypatch):
    import local_deep_research.mcp.server as server

    data_dir = tmp_path / "data"
    cache_parent = data_dir / "cache"
    cache_parent.mkdir(parents=True)
    outside = tmp_path / "outside"
    outside.mkdir()
    foreign_pkl = outside / "foreign.pkl"
    foreign_pkl.write_bytes(b"outside legacy plaintext")
    rag_root = cache_parent / "rag_indices"
    try:
        rag_root.symlink_to(outside, target_is_directory=True)
    except (OSError, NotImplementedError):
        pytest.skip("Cannot create symlinks in this environment")
    monkeypatch.setenv("LDR_DATA_DIR", str(data_dir))
    transports: list[str] = []
    monkeypatch.setattr(
        server.mcp, "run", lambda *, transport: transports.append(transport)
    )

    server.run_server()

    assert rag_root.is_symlink()
    assert foreign_pkl.read_bytes() == b"outside legacy plaintext"
    assert transports == ["stdio"]


@pytest.mark.parametrize("value", [None, "", "false", "0"])
def test_file_logging_gate_off_creates_no_log_file(
    tmp_path, monkeypatch, value
):
    import local_deep_research.utilities.log_utils as log_utils

    monkeypatch.setattr(log_utils, "_LOG_DIR", tmp_path / "data" / "logs")
    if value is None:
        monkeypatch.delenv("LDR_ENABLE_FILE_LOGGING", raising=False)
    else:
        monkeypatch.setenv("LDR_ENABLE_FILE_LOGGING", value)

    log_utils.config_logger("gate-off")
    logger.info("file sink must remain disabled")
    logger.complete()

    assert list(tmp_path.rglob("*.log")) == []


def test_file_logging_gate_on_writes_only_under_configured_log_dir(
    tmp_path, monkeypatch
):
    canary, _ = _outside_canaries(tmp_path)
    log_file, _ = _configure_file_sink(monkeypatch, tmp_path, "contained")

    logger.info("persistent logging enabled")
    logger.complete()

    assert log_file.is_file()
    assert log_file.resolve().is_relative_to(
        (tmp_path / "data" / "logs").resolve()
    )
    assert list(tmp_path.rglob("*.log")) == [log_file]
    assert canary.read_text() == "outside data"


def test_file_sink_strips_unsafe_control_characters_before_disk(
    tmp_path, monkeypatch
):
    log_file, _ = _configure_file_sink(monkeypatch, tmp_path, "sanitized")
    unsafe = "\x00\x1b[31m\u202e\u200b"

    logger.info(f"before{unsafe}visible-payload")
    logger.complete()

    written = log_file.read_text()
    assert "visible-payload" in written
    assert "\x00" not in written
    assert "\x1b" not in written
    assert "\u202e" not in written
    assert "\u200b" not in written


@pytest.mark.parametrize("diagnose", ["", "true"])
def test_file_sink_never_dumps_frame_local_secret_even_with_diagnose_optin(
    tmp_path, monkeypatch, diagnose
):
    monkeypatch.setenv("LDR_LOGURU_DIAGNOSE", diagnose)
    log_file, _ = _configure_file_sink(
        monkeypatch, tmp_path, "no-frame-locals", debug=True
    )

    def emit_exception():
        frame_local_secret = "frame-local-secret-value-9876543210"
        try:
            # Loguru diagnoses variables referenced on the failing line.
            # The exception value contains only the length, so a plaintext
            # secret on disk would come from frame-local rendering.
            raise RuntimeError("engine failure", len(frame_local_secret))
        except RuntimeError:
            logger.opt(exception=True).error("request failed (scrubbed)")

    emit_exception()
    logger.complete()

    written = log_file.read_text()
    assert "request failed (scrubbed)" in written
    assert "frame-local-secret-value-9876543210" not in written


def test_secure_logger_exception_keeps_traceback_cause_off_disk(
    tmp_path, monkeypatch
):
    from local_deep_research.security.secure_logging import (
        logger as secure_logger,
    )

    log_file, _ = _configure_file_sink(monkeypatch, tmp_path, "secure-wrapper")
    monkeypatch.delenv("LDR_APP_DEBUG", raising=False)
    monkeypatch.delenv("LDR_LOGURU_DIAGNOSE", raising=False)

    try:
        try:
            raise ValueError("cause-chain-secret-13579")
        except ValueError as inner:
            raise RuntimeError("wrapped failure") from inner
    except RuntimeError:
        secure_logger.exception("provider call failed (scrubbed)")
    logger.complete()

    written = log_file.read_text()
    assert "provider call failed (scrubbed)" in written
    assert "cause-chain-secret-13579" not in written
    assert "Traceback" not in written


def test_log_utils_import_mkdir_stays_under_data_dir_despite_redirect_env(
    tmp_path, monkeypatch
):
    import local_deep_research.utilities.log_utils as log_utils
    from local_deep_research.config.paths import get_logs_directory

    data_dir = tmp_path / "data"
    outside = tmp_path / "outside"
    monkeypatch.setenv("LDR_DATA_DIR", str(data_dir))
    for variable in ("LDR_LOG_DIR", "LDR_LOGS_DIR", "LOG_DIR", "LOGDIR"):
        monkeypatch.setenv(variable, str(outside))

    try:
        importlib.reload(log_utils)
        log_dir = get_logs_directory()

        assert log_utils._LOG_DIR.resolve() == (data_dir / "logs").resolve()
        assert log_dir.resolve().is_relative_to(data_dir.resolve())
        assert not outside.exists()
        assert {path.relative_to(tmp_path) for path in tmp_path.rglob("*")} <= {
            Path("data"),
            Path("data/logs"),
        }
    finally:
        # ``importlib.reload`` mutates the shared module globals in place, so
        # ``log_utils._LOG_DIR`` would keep pointing at this test's
        # soon-deleted ``tmp_path`` for the remainder of the session and make
        # every later ``log_utils`` consumer order-dependent. Drop the
        # redirect env first, then reload once more so the module ends up
        # with the same state it had on first import.
        monkeypatch.undo()
        importlib.reload(log_utils)


@pytest.mark.parametrize(
    "hostile_data_dir", ["relative/ldr", "/tmp/ldr\npath", "/tmp/ldr\rpath"]
)
def test_hostile_data_dir_values_are_rejected(hostile_data_dir, monkeypatch):
    from local_deep_research.config.paths import get_data_directory

    monkeypatch.setenv("LDR_DATA_DIR", hostile_data_dir)

    with pytest.raises(ValueError):
        get_data_directory()