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.

29.9 KB
Raw Blame
"""Coverage for the scope-aware variant of
``GET /settings/api/available-search-engines`` (issue #5204).

Without ``?egress_scope=`` (or under ``unprotected``), only options
blocked by the DNS-free private-URL advisory receive an ``egress`` field.
When the caller opts into scope filtering, every option gets an
``egress: {allowed, reason}`` field from the request-boundary policy,
then the same URL advisory can add a denial.

The tests below stub the engine-class flag lookups inside
``policy`` (``_get_engine_class`` and ``_engine_flags``) so they
exercise the helper's wiring (scope mapping,
fail-closed under bad input) without depending on third-party
engines like ``arxiv`` that require the ``arxiv`` SDK to import. The
*real* ``evaluate_engine`` runs the rest of its usual logic on top
of the mocked flags, so this still exercises the full helper path.
"""

import pytest

from local_deep_research.web.routers.settings import (
    _apply_guarded_url_overlay,
    _classify_options_for_egress,
)


def _option(value, label=None, **extra):
    """Build a minimal engine option the helper accepts."""
    return {
        "value": value,
        "label": label or value,
        "category": "Web Search",
        "icon": "🌐",
        "requires_api_key": False,
        "is_favorite": False,
        "group": "web",
        "group_label": "Web",
        "group_order": 1,
        "base_group": "web",
        "base_group_label": "Web",
        "base_group_order": 1,
        **extra,
    }


def _snap(scope="adaptive", primary="arxiv"):
    return {
        "policy.egress_scope": scope,
        "search.tool": primary,
    }


# Static engine class flags keyed by engine id. We mock the
# policy module's _get_engine_class to return a fake class with
# the matching (is_public, is_local) tuple so the real evaluate_engine
# flow runs on top of deterministic per-engine classification without
# needing the engines' third-party deps installed (the ``arxiv`` SDK
# for arxiv, etc.). ``library`` and ``collection_x`` are intentionally
# NOT in this map so they keep their real per-collection resolution.
_FLAGS = {
    "arxiv": (True, False),  # public web engine
    "github": (True, False),  # public code search
    "searxng": (True, False),  # public metasearch (guarded URL)
    "library": None,  # real path: collection/library branch
    "collection_x": None,  # real path: collection branch
}


def _fake_engine_class(engine_name):
    flags = _FLAGS.get(engine_name)
    if flags is None:
        return None  # not in the static map → real path handles it
    is_public, is_local = flags

    class _Fake:
        pass

    _Fake.is_public = is_public
    _Fake.is_local = is_local
    _Fake.url_setting = None
    return _Fake


@pytest.fixture(autouse=True)
def _fresh_guarded_descriptor_cache():
    """The guarded-URL overlay sweeps the engine registry through the
    (monkeypatched) ``_get_engine_class`` and caches the result — clear it
    on both sides so a sweep taken under the stub can never leak real-
    registry expectations into other test files (or vice versa)."""
    from local_deep_research.security.egress import validators

    validators.guarded_engine_url_descriptors.cache_clear()
    validators._public_engine_url_settings.cache_clear()
    yield
    validators.guarded_engine_url_descriptors.cache_clear()
    validators._public_engine_url_settings.cache_clear()


@pytest.fixture
def stub_engine_flags(monkeypatch):
    """Make _get_engine_class return a deterministic fake for the
    public/private engines we test against, without importing them.

    Also short-circuit ``_classify_engine_url`` so the URL-override
    path (which would do a DNS lookup in real life) never fires for
    the fake classes.
    """
    from local_deep_research.security.egress import policy as policy_mod

    monkeypatch.setattr(policy_mod, "_get_engine_class", _fake_engine_class)
    monkeypatch.setattr(
        policy_mod,
        "_classify_engine_url",
        lambda *_a, **_k: None,  # never override the static flags
    )


class TestClassifyOptionsForEgress:
    """Per-option egress classification (issue #5204 acceptance criteria)."""

    def test_private_only_denies_public_engines_strictly(
        self, stub_engine_flags
    ):
        # Under private_only, public engines (e.g. arxiv, github) are denied
        # regardless of primary engine setting.
        options = [
            _option("arxiv"),
            _option("github"),
            _option("library"),
        ]
        snap = _snap(scope="private_only", primary="arxiv")

        _classify_options_for_egress(
            options,
            egress_scope="private_only",
            primary_engine="arxiv",
            settings_snapshot=snap,
            username="user",
        )

        # Every option has the egress field.
        assert all("egress" in opt for opt in options)
        # Public engines under private_only are denied.
        arxiv = next(o for o in options if o["value"] == "arxiv")
        github = next(o for o in options if o["value"] == "github")
        lib = next(o for o in options if o["value"] == "library")

        assert arxiv["egress"]["allowed"] is False
        assert arxiv["egress"]["reason"] == "scope_mismatch_private_only"
        assert github["egress"]["allowed"] is False
        assert github["egress"]["reason"] == "scope_mismatch_private_only"
        assert lib["egress"]["allowed"] is True

    def test_public_only_denies_local_engines(self, stub_engine_flags):
        options = [_option("arxiv"), _option("library")]
        snap = _snap(scope="public_only", primary="arxiv")

        _classify_options_for_egress(
            options,
            egress_scope="public_only",
            primary_engine="arxiv",
            settings_snapshot=snap,
            username="user",
        )

        arxiv = next(o for o in options if o["value"] == "arxiv")
        lib = next(o for o in options if o["value"] == "library")
        assert arxiv["egress"]["allowed"] is True
        assert lib["egress"]["allowed"] is False
        assert lib["egress"]["reason"] == "scope_mismatch_public_only"

    def test_private_only_allows_local_engines(self, stub_engine_flags):
        options = [_option("arxiv"), _option("library")]
        snap = _snap(scope="private_only", primary="library")

        _classify_options_for_egress(
            options,
            egress_scope="private_only",
            primary_engine="library",
            settings_snapshot=snap,
            username="user",
        )

        arxiv = next(o for o in options if o["value"] == "arxiv")
        lib = next(o for o in options if o["value"] == "library")
        assert arxiv["egress"]["allowed"] is False
        assert arxiv["egress"]["reason"] == "scope_mismatch_private_only"
        assert lib["egress"]["allowed"] is True
        # ...and the local primary stays allowed (carve-out).

    def test_strict_mode_evaluates_primary_engine(self, stub_engine_flags):
        options = [_option("arxiv"), _option("library"), _option("github")]
        snap = _snap(scope="strict", primary="arxiv")

        _classify_options_for_egress(
            options,
            egress_scope="strict",
            primary_engine="arxiv",
            settings_snapshot=snap,
            username="user",
        )

        primary = next(o for o in options if o["value"] == "arxiv")
        assert primary["egress"]["allowed"] is True
        for opt in options:
            if opt["value"] == "arxiv":
                continue
            assert opt["egress"]["allowed"] is False
            assert opt["egress"]["reason"] == "strict_not_primary"

    def test_missing_snapshot_is_noop(self):
        # A non-dict snapshot (the test-double path the precheck also
        # has) is treated as "no filter": the helper must not raise,
        # and options come through unchanged so the frontend falls back
        # to today's behavior.
        options = [_option("arxiv"), _option("library")]

        _classify_options_for_egress(
            options,
            egress_scope="private_only",
            primary_engine="arxiv",
            settings_snapshot=None,  # not a dict
            username="user",
        )

        assert all("egress" not in opt for opt in options)

    def test_corrupt_scope_emits_policy_unavailable(self):
        # An unrecognised scope (PolicyDeniedError from
        # context_from_snapshot) must NOT crash the dropdown — the
        # helper falls back to permissive so the user can still pick
        # and the precheck's 400 surfaces the real error.
        options = [_option("arxiv"), _option("library")]
        snap = _snap(scope="garbage", primary="arxiv")

        _classify_options_for_egress(
            options,
            egress_scope="garbage",  # caller asked for filtering under corrupt scope...
            primary_engine="arxiv",
            settings_snapshot=snap,
            username="user",
        )

        # No crashing; all options got a policy_unavailable marker.
        for opt in options:
            assert opt["egress"]["allowed"] is True
            assert opt["egress"]["reason"] == "policy_unavailable"

    def test_preserves_unrelated_fields(self, stub_engine_flags):
        # The helper must mutate ONLY by adding ``egress``; every other
        # field the frontend relies on (label, group_label, etc.) is
        # untouched.
        options = [_option("arxiv", label="📁 ArXiv (Web Search)")]
        snap = _snap(scope="private_only", primary="arxiv")

        _classify_options_for_egress(
            options,
            egress_scope="private_only",
            primary_engine="arxiv",
            settings_snapshot=snap,
            username="user",
        )

        opt = options[0]
        assert opt["label"] == "📁 ArXiv (Web Search)"
        assert opt["category"] == "Web Search"
        assert opt["group_label"] == "Web"
        # arxiv is public -> denied under private_only
        assert opt["egress"]["allowed"] is False
        assert opt["egress"]["reason"] == "scope_mismatch_private_only"

    def test_handles_empty_option_list(self, stub_engine_flags):
        # An empty option list must not crash — the helper is called
        # even when no engines are configured.
        snap = _snap(scope="private_only", primary="arxiv")
        _classify_options_for_egress(
            [],
            egress_scope="private_only",
            primary_engine="arxiv",
            settings_snapshot=snap,
            username="user",
        )
        # No assertion needed — the test is "no exception".

    def test_request_omitting_egress_scope_is_unfiltered(
        self, stub_engine_flags, monkeypatch
    ):
        # When egress_scope is omitted or empty, options are classified under
        # the saved snapshot's unprotected scope and all remain allowed.
        # Since PR #5148 the unprotected escape hatch is operator-gated:
        # without the gate the scope is coerced to adaptive, so enable it
        # here to keep exercising the genuine unprotected path.
        from local_deep_research.security.egress import policy as policy_mod

        monkeypatch.setattr(
            policy_mod, "unprotected_egress_allowed", lambda: True
        )
        options = [_option("arxiv"), _option("github"), _option("library")]
        snap = _snap(scope="unprotected", primary="github")

        _classify_options_for_egress(
            options,
            egress_scope="",
            primary_engine="",
            settings_snapshot=snap,
            username="user",
        )

        for opt in options:
            assert opt["egress"]["allowed"] is True

    def test_override_egress_scope_differs_from_settings_snapshot(
        self, stub_engine_flags
    ):
        # When the user changes scope on the form, egress_scope passed to the helper
        # must override the saved DB snapshot's policy.egress_scope (issue #5204).
        options = [_option("arxiv"), _option("github"), _option("library")]
        snap = _snap(scope="adaptive", primary="github")

        _classify_options_for_egress(
            options,
            egress_scope="private_only",
            primary_engine="github",
            settings_snapshot=snap,
            username="user",
        )

        arxiv = next(o for o in options if o["value"] == "arxiv")
        github = next(o for o in options if o["value"] == "github")
        lib = next(o for o in options if o["value"] == "library")

        # Under private_only, public engines (github, arxiv) are denied
        assert github["egress"]["allowed"] is False
        assert github["egress"]["reason"] == "scope_mismatch_private_only"
        assert arxiv["egress"]["allowed"] is False
        assert arxiv["egress"]["reason"] == "scope_mismatch_private_only"
        # Local engines (library) are allowed
        assert lib["egress"]["allowed"] is True


class TestApiAvailableSearchEnginesEndpoint:
    """Integration coverage for GET /settings/api/available-search-engines dropdown filtering."""

    def test_private_only_scope_filters_public_options(
        self, authenticated_client
    ):
        res = authenticated_client.get(
            "/settings/api/available-search-engines?egress_scope=private_only"
        )
        assert res.status_code == 200
        data = res.get_json()
        assert "engine_options" in data
        options = data["engine_options"]

        # Guard against a vacuous pass. ``any`` is not enough: the guarded
        # engine-URL overlay stamps ``egress`` on SearXNG on EVERY response,
        # scope opt-in or not, so ``any`` holds even when the PDP pass never
        # ran. The PDP pass stamps every option unconditionally, so ``all``
        # is what actually proves it ran.
        assert all("egress" in o for o in options), (
            "not every option carried an egress decision — the scope opt-in "
            "did not run, so the per-engine assertions below are vacuous"
        )

        # Local library option is allowed
        lib = next((o for o in options if o.get("value") == "library"), None)
        assert lib is not None, "library option missing from the dropdown"
        assert lib["egress"]["allowed"] is True

        # Public engines are marked as denied under private_only
        public_engine = next(
            (
                o
                for o in options
                if o.get("value") in ("searxng", "arxiv", "github")
            ),
            None,
        )
        assert public_engine is not None, (
            "no public engine in the dropdown — the denial assertion below "
            "would not be exercised"
        )
        assert public_engine["egress"]["allowed"] is False
        assert (
            public_engine["egress"]["reason"] == "scope_mismatch_private_only"
        )

    def test_public_only_scope_filters_local_options(
        self, authenticated_client
    ):
        res = authenticated_client.get(
            "/settings/api/available-search-engines?egress_scope=public_only"
        )
        assert res.status_code == 200
        data = res.get_json()
        assert "engine_options" in data
        options = data["engine_options"]

        # ``all``, not ``any``: the guarded engine-URL overlay stamps
        # ``egress`` on SearXNG unconditionally, so ``any`` would hold even
        # if the scope opt-in never ran (see the private_only test above).
        assert all("egress" in o for o in options), (
            "not every option carried an egress decision — the scope opt-in "
            "did not run, so the per-engine assertion below is vacuous"
        )
        lib = next((o for o in options if o.get("value") == "library"), None)
        assert lib is not None, "library option missing from the dropdown"
        assert lib["egress"]["allowed"] is False
        assert lib["egress"]["reason"] == "scope_mismatch_public_only"

    def test_adaptive_and_strict_scopes_skip_scope_filtering_but_keep_guard(
        self, authenticated_client, monkeypatch
    ):
        """Scope filtering is still skipped outside private_only /
        public_only — but the guarded engine-URL overlay is NOT, because
        the guard it mirrors ignores ``policy.egress_scope`` entirely.

        This previously asserted ``len(denied) == 0``, which pinned the
        very hole that made the feature dead on a default install: the
        shipped default scope is ``adaptive``, so the overlay never ran
        where it mattered. The corrected contract is "no SCOPE denials,
        guard denials allowed".
        """
        for var in (
            "LDR_SEARCH_ALLOW_PRIVATE_ENGINE_URLS",
            "LDR_SEARCH_PRIVATE_ENGINE_URL_ALLOWLIST",
            "LDR_SEARCH_ENGINE_WEB_SEARXNG_DEFAULT_PARAMS_INSTANCE_URL",
        ):
            monkeypatch.delenv(var, raising=False)

        for scope in ("adaptive", "strict", "unprotected"):
            res = authenticated_client.get(
                f"/settings/api/available-search-engines?egress_scope={scope}"
            )
            assert res.status_code == 200
            data = res.get_json()
            options = data.get("engine_options", [])
            denied = [
                o
                for o in options
                if o.get("egress", {}).get("allowed") is False
            ]
            # No engine is denied for SCOPE reasons: the PDP pass is
            # skipped for these scopes, so the only denial the response
            # may carry is the scope-independent URL guard.
            assert all(
                o["egress"]["reason"] == "private_url_unapproved"
                for o in denied
            ), [o["egress"] for o in denied]

            # ...and the guard IS applied: the shipped SearXNG default is
            # http://localhost:8080, which no approval env covers here, so
            # it must read as disabled rather than silently self-disabling
            # at run time. Skipped only if this build has no SearXNG.
            searxng = next(
                (o for o in options if o.get("value") == "searxng"), None
            )
            if searxng is not None:
                assert "egress" in searxng, (
                    "the guarded-URL overlay did not run under "
                    f"egress_scope={scope}"
                )
                assert searxng["egress"] == {
                    "allowed": False,
                    "reason": "private_url_unapproved",
                }

    def test_unfiltered_response_still_carries_the_url_guard(
        self, authenticated_client, monkeypatch
    ):
        """No ``?egress_scope=`` at all — the request a default install
        actually makes, since ``getCurrentEgressScopeForDropdown`` returns
        null for every scope but private_only / public_only. Would FAIL
        before the fix: the overlay was inside the opt-in branch."""
        for var in (
            "LDR_SEARCH_ALLOW_PRIVATE_ENGINE_URLS",
            "LDR_SEARCH_PRIVATE_ENGINE_URL_ALLOWLIST",
            "LDR_SEARCH_ENGINE_WEB_SEARXNG_DEFAULT_PARAMS_INSTANCE_URL",
        ):
            monkeypatch.delenv(var, raising=False)

        res = authenticated_client.get("/settings/api/available-search-engines")
        assert res.status_code == 200
        options = res.get_json().get("engine_options", [])
        searxng = next(
            (o for o in options if o.get("value") == "searxng"), None
        )
        if searxng is None:
            pytest.skip("SearXNG not present in this build's engine config")
        assert searxng["egress"] == {
            "allowed": False,
            "reason": "private_url_unapproved",
        }
        # Engines the guard does not cover keep the historical shape.
        others = [
            o for o in options if o.get("value") != "searxng" and "egress" in o
        ]
        assert others == []


class TestGuardedUrlOverlay:
    """The private-URL guard grays out a blocked engine in the dropdown.

    The overlay reuses the warning-banner check verbatim, so the dropdown
    and the banner can never disagree: an unapproved private instance URL
    means the engine would silently self-disable at runtime, and the
    option must say so instead of being selectable.

    It is ADVISORY, not authoritative. The banner check is DNS-free by
    design, so it is a strict SUBSET of what the runtime's ``validate_url``
    blocks — the overlay can fail open (an engine stays selectable and is
    refused later) but never fails closed. The two "advisory gap" tests
    below pin that, and the scope-independence tests pin that it fires
    under every egress scope rather than only ``public_only``.
    """

    APPROVAL_ENVS = (
        "LDR_SEARCH_ALLOW_PRIVATE_ENGINE_URLS",
        "LDR_SEARCH_PRIVATE_ENGINE_URL_ALLOWLIST",
        "LDR_SEARCH_ENGINE_WEB_SEARXNG_DEFAULT_PARAMS_INSTANCE_URL",
    )
    URL_KEY = "search.engine.web.searxng.default_params.instance_url"

    def _clear(self, monkeypatch):
        for var in self.APPROVAL_ENVS:
            monkeypatch.delenv(var, raising=False)

    def _classify(self, snap, scope="public_only", primary="searxng"):
        options = [_option("searxng")]
        _classify_options_for_egress(
            options,
            egress_scope=scope,
            primary_engine=primary,
            settings_snapshot=snap,
            username="user",
        )
        return options[0]

    def test_unapproved_private_url_disables_option(
        self, stub_engine_flags, monkeypatch
    ):
        self._clear(monkeypatch)
        snap = _snap(scope="public_only", primary="searxng")
        snap[self.URL_KEY] = "http://localhost:8080"
        opt = self._classify(snap)
        assert opt["egress"]["allowed"] is False
        assert opt["egress"]["reason"] == "private_url_unapproved"

    def test_allowlisted_private_url_stays_enabled(
        self, stub_engine_flags, monkeypatch
    ):
        self._clear(monkeypatch)
        monkeypatch.setenv(
            "LDR_SEARCH_PRIVATE_ENGINE_URL_ALLOWLIST",
            "http://localhost:8080",
        )
        snap = _snap(scope="public_only", primary="searxng")
        snap[self.URL_KEY] = "http://localhost:8080"
        opt = self._classify(snap)
        assert opt["egress"]["allowed"] is True

    def test_public_url_is_untouched(self, stub_engine_flags, monkeypatch):
        self._clear(monkeypatch)
        snap = _snap(scope="public_only", primary="searxng")
        snap[self.URL_KEY] = "https://searx.example.com"
        opt = self._classify(snap)
        assert opt["egress"]["allowed"] is True

    # ------------------------------------------------------------------
    # Scope independence (PR #5648 follow-up, P0).
    #
    # ``resolve_engine_allow_private_ips`` deliberately ignores
    # ``policy.egress_scope`` (a user-editable dropdown would otherwise be
    # a self-service SSRF bypass), so the runtime refuses the URL under
    # EVERY scope. The overlay must therefore fire under every scope too.
    # Before the fix it lived inside the ``apply_egress_filter`` branch:
    # under ``private_only`` a public engine is already denied for scope
    # reasons and the overlay skipped it, and every other scope skipped
    # the branch entirely — so it could only ever fire under
    # ``public_only``, i.e. never on the shipped ``adaptive`` default.
    # ------------------------------------------------------------------

    @pytest.mark.parametrize("scope", ["adaptive", "strict", "unprotected"])
    def test_overlay_fires_under_every_scope(
        self, stub_engine_flags, monkeypatch, scope
    ):
        """Would FAIL before the fix for adaptive/strict/unprotected: the
        overlay only ran when the caller opted into scope filtering."""
        self._clear(monkeypatch)
        snap = _snap(scope=scope, primary="searxng")
        snap[self.URL_KEY] = "http://localhost:8080"
        opt = self._classify(snap, scope=scope)
        assert opt["egress"]["allowed"] is False
        assert opt["egress"]["reason"] == "private_url_unapproved"

    def test_overlay_runs_without_any_scope_classification(
        self, stub_engine_flags, monkeypatch
    ):
        """The unfiltered response path (no ``?egress_scope=``) still gets
        the overlay — this is the code path a default install takes, since
        ``getCurrentEgressScopeForDropdown`` sends no scope for adaptive.
        Would FAIL before the fix: nothing stamped ``egress`` at all."""
        self._clear(monkeypatch)
        options = [_option("searxng"), _option("arxiv")]
        _apply_guarded_url_overlay(
            options, {self.URL_KEY: "http://localhost:8080"}
        )
        searxng = next(o for o in options if o["value"] == "searxng")
        arxiv = next(o for o in options if o["value"] == "arxiv")
        assert searxng["egress"] == {
            "allowed": False,
            "reason": "private_url_unapproved",
        }
        # Non-guarded engines keep the historical shape (no egress key).
        assert "egress" not in arxiv

    def test_overlay_still_runs_when_the_policy_context_fails_to_build(
        self, stub_engine_flags, monkeypatch
    ):
        """The degraded path stamps ``policy_unavailable`` and returns early.

        ``context_from_snapshot`` raises on a malformed snapshot (and on an
        unknown ``policy.egress_scope``). The route then fails OPEN on scope
        deliberately, so the frontend defers to the request-boundary
        precheck instead of showing a half-blanked dropdown. The URL guard
        is not part of that context, though, and the runtime refuses the URL
        regardless — so a guard-denied engine must not read as selectable
        here either. Would FAIL before this change: the early ``return``
        came before the overlay.
        """
        self._clear(monkeypatch)
        import local_deep_research.security.egress.policy as policy_mod

        def _boom(*args, **kwargs):
            raise ValueError("malformed snapshot")

        monkeypatch.setattr(policy_mod, "context_from_snapshot", _boom)

        snap = _snap(scope="public_only", primary="searxng")
        snap[self.URL_KEY] = "http://localhost:8080"
        options = [_option("searxng"), _option("arxiv")]
        _classify_options_for_egress(
            options,
            egress_scope="public_only",
            primary_engine="searxng",
            settings_snapshot=snap,
            username="user",
        )
        searxng = next(o for o in options if o["value"] == "searxng")
        arxiv = next(o for o in options if o["value"] == "arxiv")
        # Scope decision degraded permissively...
        assert arxiv["egress"] == {
            "allowed": True,
            "reason": "policy_unavailable",
        }
        # ...but the scope-independent URL guard still denied SearXNG.
        assert searxng["egress"] == {
            "allowed": False,
            "reason": "private_url_unapproved",
        }

    def test_scope_denial_keeps_its_more_specific_reason(
        self, stub_engine_flags, monkeypatch
    ):
        """Under private_only a public engine is denied for scope reasons;
        the overlay must not overwrite that with the vaguer URL reason."""
        self._clear(monkeypatch)
        snap = _snap(scope="private_only", primary="library")
        snap[self.URL_KEY] = "http://localhost:8080"
        opt = self._classify(snap, scope="private_only", primary="library")
        assert opt["egress"]["allowed"] is False
        assert opt["egress"]["reason"] == "scope_mismatch_private_only"

    # ------------------------------------------------------------------
    # Advisory, not authoritative (PR #5648 follow-up, P1-1).
    #
    # ``check_private_engine_url_blocked`` is DNS-free on purpose — a
    # settings-page render must not become a resolver round-trip, and must
    # not let a hostile instance URL make the render path probe an
    # internal name. It is therefore a strict SUBSET of what the runtime
    # blocks: a short-form literal and a private-DNS hostname sail past
    # the overlay and are refused later by ``validate_url``. The overlay
    # fails OPEN (never closed), and these tests pin that contract so a
    # future change that silently adds a DNS lookup has to justify itself.
    # ------------------------------------------------------------------

    def test_short_form_literal_is_not_caught_advisory_gap(
        self, stub_engine_flags, monkeypatch
    ):
        self._clear(monkeypatch)
        snap = _snap(scope="adaptive", primary="searxng")
        snap[self.URL_KEY] = "http://127.1:8080"
        opt = self._classify(snap, scope="adaptive")
        # Documented gap: the runtime WILL block this. The overlay does
        # not, because 127.1 is not a parseable IP literal and the no-DNS
        # hostname check does not recognise it.
        assert opt["egress"]["allowed"] is True

    def test_private_dns_hostname_is_not_caught_advisory_gap(
        self, stub_engine_flags, monkeypatch
    ):
        self._clear(monkeypatch)
        snap = _snap(scope="adaptive", primary="searxng")
        snap[self.URL_KEY] = "http://searx.internal:8080"
        opt = self._classify(snap, scope="adaptive")
        # Documented gap: resolving searx.internal (-> 10.x) would block
        # at runtime; the overlay does no DNS so it stays selectable.
        assert opt["egress"]["allowed"] is True

    def test_lan_literal_is_caught(self, stub_engine_flags, monkeypatch):
        """The dominant misconfiguration (a LAN literal) IS covered, so
        the advisory gap above is a subset, not a no-op."""
        self._clear(monkeypatch)
        snap = _snap(scope="adaptive", primary="searxng")
        snap[self.URL_KEY] = "http://192.168.1.50:8080"
        opt = self._classify(snap, scope="adaptive")
        assert opt["egress"]["allowed"] is False
        assert opt["egress"]["reason"] == "private_url_unapproved"

    def test_env_locked_url_stays_enabled_under_adaptive(
        self, stub_engine_flags, monkeypatch
    ):
        """Operator-provisioned (env-locked) URLs are approved, and that
        approval is scope-independent too."""
        self._clear(monkeypatch)
        monkeypatch.setenv(
            "LDR_SEARCH_ENGINE_WEB_SEARXNG_DEFAULT_PARAMS_INSTANCE_URL",
            "http://localhost:8080",
        )
        snap = _snap(scope="adaptive", primary="searxng")
        snap[self.URL_KEY] = "http://localhost:8080"
        opt = self._classify(snap, scope="adaptive")
        assert opt["egress"]["allowed"] is True