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.

19.9 KB
Raw Blame
"""Coverage for the request-boundary egress precheck in research.py
(flagged untested by the PR #4300 review).

Targets:
- _apply_policy_overrides: overlays per-research form overrides onto the
  snapshot (pure dict logic).
- _precheck_engine_policy: rejects a forbidden engine / corrupt scope at
  /api/start_research with a 400, or returns None to continue.

Ported from the Flask research_routes to the FastAPI router: the precheck now
returns a Starlette ``JSONResponse`` (read ``.status_code``) instead of a
``(jsonify, status)`` tuple, and no Flask app context is required.
"""

import json
from unittest.mock import MagicMock, Mock, patch

import pytest

from local_deep_research.security.egress.policy import PolicyDeniedError
from local_deep_research.web.routers.research import (
    _apply_policy_overrides,
    _precheck_engine_policy,
)


# ---------------------------------------------------------------------------
# _apply_policy_overrides (pure)
# ---------------------------------------------------------------------------


class TestApplyPolicyOverrides:
    def test_scope_override_applied(self):
        snap = {"policy.egress_scope": "both"}
        _apply_policy_overrides(snap, {"policy_egress_scope": "private_only"})
        assert snap["policy.egress_scope"] == "private_only"

    def test_unprotected_override_disabled_by_default(self):
        with pytest.raises(PolicyDeniedError) as exc_info:
            _apply_policy_overrides(
                {"policy.egress_scope": "adaptive"},
                {"policy_egress_scope": "unprotected"},
            )
        assert exc_info.value.decision.reason == "unprotected_egress_disabled"

    def test_unprotected_override_requires_operator_opt_in(self, monkeypatch):
        monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
        snap = {"policy.egress_scope": "adaptive"}
        _apply_policy_overrides(snap, {"policy_egress_scope": "unprotected"})
        assert snap["policy.egress_scope"] == "unprotected"

    def test_environment_scope_cannot_be_overridden(self, monkeypatch):
        monkeypatch.setenv("LDR_POLICY_EGRESS_SCOPE", "private_only")
        snap = {"policy.egress_scope": "private_only"}
        _apply_policy_overrides(snap, {"policy_egress_scope": "public_only"})
        assert snap["policy.egress_scope"] == "private_only"

    def test_bool_overrides_coerced(self):
        snap = {}
        _apply_policy_overrides(
            snap,
            {
                "llm_require_local_endpoint": "1",
                "embeddings_require_local": "",
            },
        )
        assert snap["llm.require_local_endpoint"] is True
        # Empty string is falsy => coerced to False.
        assert snap["embeddings.require_local"] is False

    @pytest.mark.parametrize(
        ("env_key", "snapshot_key", "param_key"),
        [
            (
                "LDR_LLM_REQUIRE_LOCAL_ENDPOINT",
                "llm.require_local_endpoint",
                "llm_require_local_endpoint",
            ),
            (
                "LDR_EMBEDDINGS_REQUIRE_LOCAL",
                "embeddings.require_local",
                "embeddings_require_local",
            ),
        ],
    )
    def test_environment_locality_flags_cannot_be_weakened(
        self, monkeypatch, env_key, snapshot_key, param_key
    ):
        monkeypatch.setenv(env_key, "true")
        snap = {snapshot_key: True}
        _apply_policy_overrides(snap, {param_key: False})
        assert snap[snapshot_key] is True

    def test_string_false_boolean_override_is_false(self):
        snap = {}
        _apply_policy_overrides(
            snap,
            {
                "llm_require_local_endpoint": "false",
                "embeddings_require_local": "0",
            },
        )
        assert snap["llm.require_local_endpoint"] is False
        assert snap["embeddings.require_local"] is False

    def test_absent_params_leave_snapshot_untouched(self):
        snap = {"policy.egress_scope": "both"}
        _apply_policy_overrides(snap, {})
        assert snap == {"policy.egress_scope": "both"}

    def test_non_dict_snapshot_is_noop(self):
        # Must not raise on a non-dict snapshot.
        assert (
            _apply_policy_overrides(None, {"policy_egress_scope": "x"}) is None
        )


# ---------------------------------------------------------------------------
# _precheck_engine_policy (returns a JSONResponse on reject, None to continue)
# ---------------------------------------------------------------------------


def _mgr(snapshot, primary="arxiv"):
    m = Mock()
    # A real settings snapshot always carries the primary under "search.tool".
    # The precheck now resolves the primary FROM the snapshot (matching the
    # worker via resolve_run_primary_engine), so reflect that here.
    if isinstance(snapshot, dict) and "search.tool" not in snapshot:
        snapshot = {**snapshot, "search.tool": primary}
    m.get_settings_snapshot.return_value = snapshot
    m.get_setting.side_effect = lambda key, default=None: (
        primary if key == "search.tool" else default
    )
    return m


class TestPrecheckEnginePolicy:
    def test_allowed_engine_returns_none(self):
        # PUBLIC_ONLY + a public engine (arxiv) => allowed => continue.
        mgr = _mgr({"policy.egress_scope": "public_only"})
        assert _precheck_engine_policy(mgr, {}, "arxiv", "user") is None

    def test_disabled_unprotected_override_returns_400(self):
        # _apply_policy_overrides raises PolicyDeniedError for an
        # unprotected override without operator opt-in; the precheck
        # catches it and surfaces a 400 with the curated reason code.
        mgr = _mgr({"policy.egress_scope": "private_only"})
        result = _precheck_engine_policy(
            mgr, {"policy_egress_scope": "unprotected"}, "pubmed", "user"
        )
        assert result is not None
        assert result.status_code == 400
        body = json.loads(result.body)
        assert body["message"].endswith("unprotected_egress_disabled")

    def test_forbidden_engine_response_flags_egress_scope_field(self):
        # The 400 must tell the frontend WHICH field to highlight so the error
        # can be shown inline on the Egress Scope dropdown (not just as a
        # top-of-form alert the user may have scrolled past).
        mgr = _mgr({"policy.egress_scope": "public_only"})
        result = _precheck_engine_policy(mgr, {}, "library", "user")
        assert result is not None
        assert result.status_code == 400
        body = json.loads(result.body)
        assert body["field"] == "policy_egress_scope"
        # And the human-readable message is still present.
        assert body["message"] and "Egress Scope" in body["message"]
        assert body["reason"] == "scope_mismatch_public_only"

    def test_strict_non_primary_response_flags_egress_scope_field(self):
        # STRICT + a non-primary engine => strict_not_primary. Same fix as
        # the scope_mismatch cases: change Egress Scope away from Strict.
        mgr = _mgr(
            {"policy.egress_scope": "strict"},
            primary="arxiv",
        )
        result = _precheck_engine_policy(mgr, {}, "library", "user")
        assert result is not None
        body = json.loads(result.body)
        assert result.status_code == 400
        assert body["field"] == "policy_egress_scope"
        assert body["reason"] == "strict_not_primary"

    def test_internal_error_response_omits_field_attribution(self, monkeypatch):
        # internal_error is a server-side issue — no form field fixes it, so
        # ``field`` must be ``null`` (NOT the egress-scope dropdown, which
        # would mislead the user into thinking the scope is the problem).
        # ``evaluate_engine`` is imported lazily from the policy module inside
        # ``_precheck_engine_policy`` (see the local-import note in the
        # implementation), so patching the source module is enough — every
        # call into it resolves through the same module attribute.
        from local_deep_research.security.egress import policy as policy_mod

        def _boom(*_args, **_kwargs):
            return policy_mod.Decision(False, "internal_error")

        monkeypatch.setattr(policy_mod, "evaluate_engine", _boom)
        mgr = _mgr({"policy.egress_scope": "public_only"})
        result = _precheck_engine_policy(mgr, {}, "arxiv", "user")
        assert result is not None
        body = json.loads(result.body)
        assert result.status_code == 400
        assert body["field"] is None
        assert body["reason"] == "internal_error"

    def test_engine_unknown_response_omits_field_attribution(self, monkeypatch):
        # engine_unknown: the engine isn't in the registry and isn't a
        # collection. Changing the egress scope won't help, so the field
        # hint must NOT point at the egress dropdown — the user has to pick
        # a different engine, which is shown via the alert text.
        from local_deep_research.security.egress import policy as policy_mod

        monkeypatch.setattr(
            policy_mod,
            "evaluate_engine",
            lambda *_a, **_k: policy_mod.Decision(False, "engine_unknown"),
        )
        mgr = _mgr({"policy.egress_scope": "public_only"})
        result = _precheck_engine_policy(mgr, {}, "arxiv", "user")
        assert result is not None
        body = json.loads(result.body)
        assert result.status_code == 400
        assert body["field"] is None
        assert body["reason"] == "engine_unknown"

    def test_adaptive_requested_engine_overrides_saved_primary(self):
        # Saved primary is private; this run requests a public engine.
        # ADAPTIVE must follow the requested engine, not the DB default.
        mgr = _mgr({"policy.egress_scope": "adaptive"}, primary="library")
        assert _precheck_engine_policy(mgr, {}, "arxiv", "user") is None

    def test_scope_mismatch_message_omits_adaptive_when_target_isnt_primary(
        self,
    ):
        # Adaptive (default) follows the saved primary engine. If the
        # blocked engine is NOT the primary (library blocked under
        # public_only while primary=arxiv), Adaptive would resolve back to
        # public_only and library would still be denied. So the message
        # must point only at the explicit compatible scope (Private only)
        # — not suggest Adaptive.
        mgr = _mgr({"policy.egress_scope": "public_only"}, primary="arxiv")
        result = _precheck_engine_policy(mgr, {}, "library", "user")
        assert result is not None
        body = json.loads(result.body)
        assert body["message"].startswith(
            "Search engine 'library' was blocked because your Egress Scope "
            "is set to Public only"
        )
        # Explicit remedy named, Adaptive NOT mentioned.
        assert "Private only" in body["message"]
        assert "Adaptive" not in body["message"]

    def test_scope_mismatch_message_mentions_adaptive_when_target_is_primary(
        self,
    ):
        # When the blocked engine IS the primary, Adaptive would follow the
        # primary into the compatible bucket, so mentioning it is reliable.
        mgr = _mgr({"policy.egress_scope": "public_only"}, primary="library")
        result = _precheck_engine_policy(mgr, {}, "library", "user")
        assert result is not None
        body = json.loads(result.body)
        assert "Adaptive" in body["message"]
        assert "Private only" in body["message"]

    def test_corrupt_scope_returns_400(self):
        mgr = _mgr({"policy.egress_scope": "garbage"})
        result = _precheck_engine_policy(mgr, {}, "arxiv", "user")
        assert result is not None
        assert result.status_code == 400
        # This hits the PolicyDeniedError branch, which surfaces the curated
        # decision.reason code (safe), not raw exception text.
        assert "garbage" not in json.loads(result.body)["message"]

    def test_non_dict_snapshot_returns_none(self):
        # No real snapshot => skip precheck (factory PEP backstops).
        mgr = _mgr(None)
        assert _precheck_engine_policy(mgr, {}, "library", "user") is None

    def test_missing_saved_primary_uses_requested_engine(self):
        # Empty saved search.tool is filled by the requested engine overlay
        # before resolve, matching the worker's kwargs primary.
        mgr = _mgr({"policy.egress_scope": "public_only", "search.tool": ""})
        assert _precheck_engine_policy(mgr, {}, "arxiv", "user") is None

    def test_missing_primary_and_requested_engine_returns_400(self):
        mgr = _mgr({"policy.egress_scope": "public_only", "search.tool": ""})
        result = _precheck_engine_policy(mgr, {}, "", "user")
        assert result is not None
        assert result.status_code == 400
        body = json.loads(result.body)
        assert (
            body["message"]
            == "Egress policy refused this run due to an invalid policy configuration."
        )
        assert "search engine configured" not in body["message"]

    def test_per_research_override_tightens_scope(self):
        # Saved scope is permissive (both) but the form overrides to
        # public_only for THIS run => a local engine must be refused.
        mgr = _mgr({"policy.egress_scope": "both"})
        params = {"policy_egress_scope": "public_only"}
        result = _precheck_engine_policy(mgr, params, "library", "user")
        assert result is not None
        assert result.status_code == 400

    # --- two-axis enforcement (ADR-0007 stage C) ---
    # Ported to the FastAPI router: the precheck returns a Starlette
    # ``JSONResponse`` (read ``.status_code`` / ``json.loads(result.body)``)
    # and needs no Flask app context.

    def test_two_axis_denies_sensitive_source_plus_cloud_llm(self):
        # STRICT lets a private-collection primary + cloud LLM pass the SCOPE
        # check (strict is orthogonal to inference), but the two-axis rule
        # refuses it: a sensitive source would reach an exposing inference sink.
        mgr = _mgr({"policy.egress_scope": "strict"}, primary="collection_x")
        result = _precheck_engine_policy(
            mgr, {"model_provider": "anthropic"}, "collection_x", "user"
        )
        assert result is not None
        assert result.status_code == 400
        assert (
            json.loads(result.body)["reason"]
            == "sensitive_to_exposing_inference"
        )

    def test_two_axis_allows_sensitive_source_plus_local_llm(self):
        # Same private primary but a LOCAL LLM => two-axis allows => continue.
        mgr = _mgr({"policy.egress_scope": "strict"}, primary="collection_x")
        result = _precheck_engine_policy(
            mgr, {"model_provider": "ollama"}, "collection_x", "user"
        )
        assert result is None

    def test_unprotected_scope_bypasses_two_axis(self, monkeypatch):
        # Without the operator opt-in, context_from_snapshot's
        # parse_user_egress_scope(disabled_unprotected="adaptive") would
        # silently coerce "unprotected" to "adaptive" (policy.py), which
        # would defeat the point of this test.
        monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")
        # The escape hatch: the otherwise-denied combo is permitted under
        # UNPROTECTED (audit_run evaluates permissive), so no 400.
        mgr = _mgr(
            {"policy.egress_scope": "unprotected"}, primary="collection_x"
        )
        result = _precheck_engine_policy(
            mgr, {"model_provider": "anthropic"}, "collection_x", "user"
        )
        assert result is None

    def test_lexical_primary_with_cloud_embeddings_not_denied(self):
        # A lexical store (paperless) never embeds, so a configured cloud
        # embedder must NOT falsely trip the two-axis check: STRICT + paperless
        # primary + local LLM + cloud embeddings -> allowed (no embeddings sink).
        mgr = _mgr(
            {"policy.egress_scope": "strict", "embeddings.provider": "openai"},
            primary="paperless",
        )
        result = _precheck_engine_policy(
            mgr, {"model_provider": "ollama"}, "paperless", "user"
        )
        assert result is None

    def test_env_override_egress_scope_takes_precedence_at_precheck(
        self, monkeypatch
    ):
        from local_deep_research.api.settings_utils import (
            InMemorySettingsManager,
        )

        mgr = InMemorySettingsManager()
        mgr.set_setting("policy.egress_scope", "adaptive")
        mgr.set_setting("search.tool", "library")
        monkeypatch.setenv("LDR_POLICY_EGRESS_SCOPE", "private_only")

        # Requesting a public engine (searxng) under env-locked private_only is rejected
        result = _precheck_engine_policy(mgr, {}, "searxng", "user")
        assert result is not None
        assert result.status_code == 400
        body = json.loads(result.body)
        assert body["reason"] == "scope_mismatch_private_only"

    def test_precheck_overlays_max_results_and_time_period(self):
        with patch(
            "local_deep_research.web.routers.research.overlay_runtime_settings"
        ) as mock_overlay:
            mgr = _mgr({"policy.egress_scope": "adaptive"})
            params = {
                "max_results": 42,
                "time_period": "w",
                "model_provider": "openai",
                "model": "gpt-4",
            }
            _precheck_engine_policy(mgr, params, "arxiv", "user")
            mock_overlay.assert_called_once()
            assert mock_overlay.call_args[1]["max_results"] == 42
            assert mock_overlay.call_args[1]["time_period"] == "w"

    def test_precheck_strict_scope_keeps_saved_primary(self):
        # STRICT + a saved ``library`` primary + a request for ``searxng``
        # must evaluate the saved primary. The precheck passes the
        # saved primary into context_from_snapshot, so even though the
        # overlay writes the requested engine onto search.tool, the
        # context still classifies against the saved primary and the
        # factory PEP refuses the run with strict_not_primary.
        from local_deep_research.security.egress import policy as policy_mod

        mgr = _mgr({"policy.egress_scope": "strict"}, primary="library")
        with patch.object(policy_mod, "context_from_snapshot") as mock_ctx:
            mock_ctx.return_value = MagicMock(
                scope="strict", primary_engine="library"
            )
            with patch.object(policy_mod, "evaluate_engine") as mock_eval:
                mock_eval.return_value = MagicMock(
                    allowed=False, reason="strict_not_primary"
                )
                result = _precheck_engine_policy(mgr, {}, "searxng", "user")

        assert result is not None
        assert result.status_code == 400
        assert mock_ctx.call_args.args[1] == "library"

    @pytest.mark.parametrize(
        "snapshot",
        [
            {"policy.egress_scope": "strict", "search.tool": ""},
            {"policy.egress_scope": "strict", "search.tool": "   "},
            {"policy.egress_scope": "strict", "search.tool": {"value": ""}},
            {"policy.egress_scope": "strict", "search.tool": {"value": "  "}},
            {"policy.egress_scope": "strict"},
        ],
        ids=[
            "flat-blank",
            "flat-whitespace",
            "envelope-blank",
            "envelope-ws",
            "missing",
        ],
    )
    def test_precheck_strict_blank_saved_primary_fails_closed(self, snapshot):
        # STRICT + a blank/missing saved primary + an explicit requested
        # engine must fail closed (400 invalid config), not let the request
        # choose its own primary. Non-STRICT scopes deliberately fall back
        # to the requested engine (see
        # test_missing_saved_primary_uses_requested_engine).
        m = Mock()
        m.get_settings_snapshot.return_value = dict(snapshot)
        result = _precheck_engine_policy(m, {}, "arxiv", "user")
        assert result is not None
        assert result.status_code == 400
        body = json.loads(result.body)
        assert (
            body["message"]
            == "Egress policy refused this run due to an invalid policy configuration."
        )