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.2 KB
Raw Blame
"""Tests for the SearXNG private-URL warning banner check.

``check_searxng_private_url_blocked`` surfaces, on the research form, the
otherwise-quiet failure where SearXNG is the selected engine but its
private/localhost instance URL has no operator approval, so the engine
self-disables at run time.
"""

from local_deep_research.security.egress.warnings import (
    check_searxng_private_url_blocked,
)

GATE_ENV = "LDR_SEARCH_ALLOW_PRIVATE_ENGINE_URLS"
ALLOWLIST_ENV = "LDR_SEARCH_PRIVATE_ENGINE_URL_ALLOWLIST"
INSTANCE_URL_ENV = "LDR_SEARCH_ENGINE_WEB_SEARXNG_DEFAULT_PARAMS_INSTANCE_URL"

APPROVAL_ENVS = (GATE_ENV, ALLOWLIST_ENV, INSTANCE_URL_ENV)


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


class TestFires:
    def test_fires_for_unapproved_localhost_url(self, monkeypatch):
        """The dominant broken case: searxng selected, default localhost URL,
        no operator approval — the banner fires and names all three remedies."""
        _clear_approvals(monkeypatch)
        w = check_searxng_private_url_blocked(
            "searxng", "http://localhost:8080", False
        )
        assert w is not None
        assert w["type"] == "searxng_private_url_blocked"
        assert "http://localhost:8080" in w["message"]
        assert ALLOWLIST_ENV in w["message"]
        assert GATE_ENV in w["message"]
        assert INSTANCE_URL_ENV in w["message"]
        assert w["dismissKey"] == "app.warnings.dismiss_searxng_private_url"

    def test_fires_for_lan_ip(self, monkeypatch):
        _clear_approvals(monkeypatch)
        w = check_searxng_private_url_blocked(
            "searxng", "http://10.0.0.5:8888", False
        )
        assert w is not None

    def test_message_shows_origin_never_userinfo_or_path(self, monkeypatch):
        """The copy-paste remedy is the ORIGIN — credentials, path, and query
        from the raw URL must never appear in the banner."""
        _clear_approvals(monkeypatch)
        w = check_searxng_private_url_blocked(
            "searxng", "http://user:[email protected]:8888/searx?x=1", False
        )
        assert w is not None
        assert "http://192.168.1.5:8888" in w["message"]
        assert "secretpw" not in w["message"]
        assert "user:" not in w["message"]
        assert "/searx" not in w["message"]


class TestStaysSilent:
    def test_silent_when_acknowledged(self, monkeypatch):
        _clear_approvals(monkeypatch)
        assert (
            check_searxng_private_url_blocked(
                "searxng", "http://localhost:8080", True
            )
            is None
        )

    def test_silent_for_other_primary_engine(self, monkeypatch):
        _clear_approvals(monkeypatch)
        assert (
            check_searxng_private_url_blocked(
                "wikipedia", "http://localhost:8080", False
            )
            is None
        )

    def test_silent_for_public_url(self, monkeypatch):
        """A public hostname is fine — and must not trigger DNS lookups
        (is_private_ip is a literal/known-name check)."""
        _clear_approvals(monkeypatch)
        assert (
            check_searxng_private_url_blocked(
                "searxng", "https://searx.example.com", False
            )
            is None
        )

    def test_silent_for_empty_url(self, monkeypatch):
        _clear_approvals(monkeypatch)
        assert check_searxng_private_url_blocked("searxng", "", False) is None
        assert check_searxng_private_url_blocked("searxng", None, False) is None

    def test_silent_when_blanket_gate_set(self, monkeypatch):
        _clear_approvals(monkeypatch)
        monkeypatch.setenv(GATE_ENV, "true")
        assert (
            check_searxng_private_url_blocked(
                "searxng", "http://localhost:8080", False
            )
            is None
        )

    def test_silent_when_origin_allowlisted(self, monkeypatch):
        _clear_approvals(monkeypatch)
        monkeypatch.setenv(ALLOWLIST_ENV, "http://localhost:8080")
        assert (
            check_searxng_private_url_blocked(
                "searxng", "http://localhost:8080", False
            )
            is None
        )

    def test_fires_when_allowlist_does_not_match(self, monkeypatch):
        """An allowlist for a DIFFERENT origin does not silence the banner."""
        _clear_approvals(monkeypatch)
        monkeypatch.setenv(ALLOWLIST_ENV, "http://localhost:9090")
        assert (
            check_searxng_private_url_blocked(
                "searxng", "http://localhost:8080", False
            )
            is not None
        )

    def test_silent_when_url_env_locked(self, monkeypatch):
        _clear_approvals(monkeypatch)
        monkeypatch.setenv(INSTANCE_URL_ENV, "http://localhost:8080")
        assert (
            check_searxng_private_url_blocked(
                "searxng", "http://localhost:8080", False
            )
            is None
        )

    def test_silent_for_metadata_ip(self, monkeypatch):
        """A cloud-metadata address is blocked unconditionally — the env
        remedies this banner prescribes would not help, so it stays silent."""
        _clear_approvals(monkeypatch)
        assert (
            check_searxng_private_url_blocked(
                "searxng", "http://169.254.169.254", False
            )
            is None
        )

    def test_silent_for_malformed_url(self, monkeypatch):
        """A URL the allowlist parser refuses (unbracketed IPv6 with port,
        unparseable port) yields no banner — better silence than an
        unusable prescribed remedy. The runtime ERROR still covers it."""
        _clear_approvals(monkeypatch)
        for url in ("http://fd00::5:8080", "http://localhost:8080."):
            assert (
                check_searxng_private_url_blocked("searxng", url, False) is None
            ), f"expected no banner for malformed {url}"


class TestBannerRemedyRoundTrip:
    """The origin the banner prescribes must, fed back into the allowlist,
    silence the banner AND open the runtime gate — the three-layer contract
    in one test."""

    def _prescribed_origin(self, message):
        marker = "LDR_SEARCH_PRIVATE_ENGINE_URL_ALLOWLIST="
        start = message.index(marker) + len(marker)
        end = message.index(" ", start)
        return message[start:end]

    def _assert_round_trip(self, monkeypatch, url, expected_origin):
        from local_deep_research.security.egress.validators import (
            resolve_searxng_allow_private_ips,
        )

        _clear_approvals(monkeypatch)
        w = check_searxng_private_url_blocked("searxng", url, False)
        assert w is not None, f"expected banner for {url}"
        origin = self._prescribed_origin(w["message"])
        assert origin == expected_origin
        monkeypatch.setenv(ALLOWLIST_ENV, origin)
        assert resolve_searxng_allow_private_ips(url) is True, (
            f"prescribed origin {origin} did not open the gate for {url}"
        )
        assert (
            check_searxng_private_url_blocked("searxng", url, False) is None
        ), f"prescribed origin {origin} did not silence the banner for {url}"

    def test_round_trip_localhost(self, monkeypatch):
        self._assert_round_trip(
            monkeypatch, "http://localhost:8080", "http://localhost:8080"
        )

    def test_round_trip_ipv6_literal(self, monkeypatch):
        """Would have caught the bracket-stripping bug: the prescribed
        origin must be re-bracketed to stay parseable."""
        self._assert_round_trip(
            monkeypatch, "http://[fd00::5]:8080", "http://[fd00::5]:8080"
        )

    def test_round_trip_default_port(self, monkeypatch):
        self._assert_round_trip(
            monkeypatch, "http://localhost", "http://localhost"
        )

    def test_round_trip_with_userinfo_and_path(self, monkeypatch):
        self._assert_round_trip(
            monkeypatch,
            "http://user:[email protected]:8888/searx",
            "http://192.168.1.5:8888",
        )


class TestGateMirroring:
    def test_fires_for_cgnat_literal(self, monkeypatch):
        """CGNAT (100.64.0.0/10 — Podman/rootless containers) is blocked by
        the gate as private, so the banner must fire for it too."""
        _clear_approvals(monkeypatch)
        assert (
            check_searxng_private_url_blocked(
                "searxng", "http://100.64.0.1:8080", False
            )
            is not None
        )

    def test_fires_for_non_metadata_link_local(self, monkeypatch):
        """Link-local that is NOT in the always-blocked metadata set is
        liftable by the env remedies, so the banner fires."""
        _clear_approvals(monkeypatch)
        assert (
            check_searxng_private_url_blocked(
                "searxng", "http://169.254.10.5:8080", False
            )
            is not None
        )

    def test_action_url_is_renderable(self, monkeypatch):
        """research_form.js only renders action links that start with '/'
        and not '//' — pin the shape so the link never silently vanishes."""
        _clear_approvals(monkeypatch)
        w = check_searxng_private_url_blocked(
            "searxng", "http://localhost:8080", False
        )
        assert w is not None
        assert w["actionUrl"].startswith("/")
        assert not w["actionUrl"].startswith("//")