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.

9.3 KB
Raw Blame
"""Tests for PolicyDeniedError handling in the public MCP tools.

The egress policy PEPs (search-system factory, BaseSearchEngine self-check,
audit-hook net) raise ``PolicyDeniedError`` when a search/research operation
is hard-stopped by the user's declared egress scope. The public MCP tools
must:

* convert that denial into a structured, machine-readable response with the
  stable ``error_type=policy_denied`` and the PDP's short reason code;
* never leak the ``target`` attribute (engine names / URLs may carry user
  content or internal hostnames) or the exception chain;
* preserve fail-closed behaviour -- the underlying engine ``run()`` does NOT
  fire when policy denies;
* preserve resource cleanup (settings context, engine handles) when a denial
  fires mid-run;
* leave non-policy failures on their existing classification path.

The factory PEP is the primary enforcement point (see
``web_search_engines/search_engine_factory.py``); these tests verify the MCP
boundary converts its denials correctly.
"""

from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.security.egress.policy import (
    Decision,
    PolicyDeniedError,
)

# MCP is an optional dependency. Skip the full module when it is absent so
# the suite still collects in minimal environments; the import-only checks
# below still run unconditionally to catch syntax/import regressions.
from tests.mcp_sdk import is_mcp_installed

MCP_AVAILABLE = is_mcp_installed()

pytestmark = pytest.mark.skipif(
    not MCP_AVAILABLE, reason="MCP package not installed"
)


def _denied(
    reason: str = "scope_mismatch_private_only",
    target: str = "wikipedia",
) -> PolicyDeniedError:
    """Build a denial carrying a sensitive target the response must NOT leak."""
    return PolicyDeniedError(Decision(False, reason), target=target)


# -----------------------------------------------------------------------------
# Research tools (quick / detailed / report / documents)
# These all delegate to an ``ldr_*`` function which internally instantiates
# engines through the factory PEP. Patching the ``ldr_*`` boundary exercises
# the public tool's exception handler without depending on the factory's
# runtime environment.
# -----------------------------------------------------------------------------


def test_quick_research_policy_denied():
    """quick_research surfaces PolicyDeniedError as error_type=policy_denied."""
    from local_deep_research.mcp.server import quick_research

    with patch(
        "local_deep_research.mcp.server.ldr_quick_summary",
        side_effect=_denied(),
    ) as mock_call:
        result = quick_research(query="test query")

    mock_call.assert_called_once()
    assert result["status"] == "error"
    assert result["error_type"] == "policy_denied"
    assert result["reason"] == "scope_mismatch_private_only"
    # The sensitive target must NOT appear anywhere in the client response.
    assert "wikipedia" not in str(result)


def test_detailed_research_policy_denied():
    from local_deep_research.mcp.server import detailed_research

    with patch(
        "local_deep_research.mcp.server.ldr_detailed_research",
        side_effect=_denied(
            reason="unprotected_egress_disabled",
            target="https://internal.example.com/search?q=secret",
        ),
    ) as mock_call:
        result = detailed_research(query="test query")

    mock_call.assert_called_once()
    assert result["status"] == "error"
    assert result["error_type"] == "policy_denied"
    assert result["reason"] == "unprotected_egress_disabled"
    # Neither the URL target nor its query content may leak.
    assert "internal.example.com" not in str(result)
    assert "secret" not in str(result)


def test_generate_report_policy_denied():
    from local_deep_research.mcp.server import generate_report

    with patch(
        "local_deep_research.mcp.server.ldr_generate_report",
        side_effect=_denied(),
    ) as mock_call:
        result = generate_report(query="test query")

    mock_call.assert_called_once()
    assert result["status"] == "error"
    assert result["error_type"] == "policy_denied"
    assert result["reason"] == "scope_mismatch_private_only"


def test_analyze_documents_policy_denied():
    from local_deep_research.mcp.server import analyze_documents

    with patch(
        "local_deep_research.mcp.server.ldr_analyze_documents",
        side_effect=_denied(target="collection_secret"),
    ) as mock_call:
        result = analyze_documents(query="test", collection_name="mycoll")

    mock_call.assert_called_once()
    assert result["status"] == "error"
    assert result["error_type"] == "policy_denied"
    assert "collection_secret" not in str(result)


# -----------------------------------------------------------------------------
# search() -- the direct-search path. It calls the factory PEP directly via
# _execute_search, then arms the audit-hook net around engine.run(). These
# tests verify both denial points preserve fail-closed + cleanup.
# -----------------------------------------------------------------------------


def test_search_policy_denied_no_engine_run():
    """When the factory PEP denies engine creation, the public search tool
    returns a structured policy_denied response and the engine's run() is
    never invoked -- fail-closed is preserved at the MCP boundary."""
    from local_deep_research.mcp.server import search

    with (
        patch(
            "local_deep_research.mcp.server.create_settings_snapshot",
            return_value={"search.tool": {"value": "wikipedia"}},
        ),
        patch(
            "local_deep_research.web_search_engines.search_engines_config.search_config",
            return_value={"wikipedia": {"requires_api_key": False}},
        ),
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.create_search_engine",
            side_effect=_denied(target="wikipedia"),
        ) as mock_factory,
        patch(
            "local_deep_research.mcp.server._egress_audit_net",
        ) as mock_net,
    ):
        result = search(query="test query", engine="wikipedia")

    # The factory PEP was reached and denied.
    mock_factory.assert_called_once()
    # The audit-hook net is armed inside _execute_search immediately before
    # engine.run(); since the factory denied, _execute_search never got that
    # far -- proving no engine run occurred.
    mock_net.assert_not_called()
    # Structured, leak-safe response.
    assert result["status"] == "error"
    assert result["error_type"] == "policy_denied"
    assert result["reason"] == "scope_mismatch_private_only"
    assert "wikipedia" not in str(result)


def test_search_policy_denied_mid_run_preserves_cleanup():
    """When PolicyDeniedError fires from engine.run() (e.g. the
    BaseSearchEngine self-check denies), resource cleanup still runs and the
    public tool surfaces a structured policy_denied response."""
    from local_deep_research.mcp.server import search

    fake_engine = MagicMock()
    fake_engine.run.side_effect = _denied(target="wikipedia")

    close_calls: list = []

    def track_close(engine, label=""):
        close_calls.append((engine, label))

    with (
        patch(
            "local_deep_research.mcp.server.create_settings_snapshot",
            return_value={"search.tool": {"value": "wikipedia"}},
        ),
        patch(
            "local_deep_research.web_search_engines.search_engines_config.search_config",
            return_value={"wikipedia": {"requires_api_key": False}},
        ),
        patch(
            "local_deep_research.web_search_engines.search_engine_factory.create_search_engine",
            return_value=fake_engine,
        ),
        patch(
            "local_deep_research.utilities.resource_utils.safe_close",
            side_effect=track_close,
        ),
        patch(
            "local_deep_research.mcp.server._egress_audit_net",
        ),
    ):
        result = search(query="test query", engine="wikipedia")

    assert result["status"] == "error"
    assert result["error_type"] == "policy_denied"
    assert result["reason"] == "scope_mismatch_private_only"
    # Cleanup preserved: safe_close ran even though policy denied mid-run.
    assert len(close_calls) == 1
    assert close_calls[0][0] is fake_engine


# -----------------------------------------------------------------------------
# Non-policy failures must keep their existing classification -- the new
# PolicyDeniedError branch must not swallow them.
# -----------------------------------------------------------------------------


def test_non_policy_exception_keeps_existing_classification():
    """A connection failure is still classified as connection_error -- the
    new PolicyDeniedError handler does not broaden to non-policy failures.

    The input 'connection refused' is pinned by
    test_classify_error_contract.test_classify_connection_refused, which
    verifies the classifier mapping directly (without MCP) so this
    assertion is not built on a message that silently maps to 'unknown'.
    """
    from local_deep_research.mcp.server import quick_research

    with patch(
        "local_deep_research.mcp.server.ldr_quick_summary",
        side_effect=ConnectionError("connection refused"),
    ):
        result = quick_research(query="test query")

    assert result["status"] == "error"
    assert result["error_type"] == "connection_error"