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.

5.4 KB
Raw Blame
"""Tests for the local-engine public-URL warning banner check.

``check_local_engine_public_url`` is the mirror image of the SearXNG
private-URL banner: for a LOCAL document engine (Paperless, Elasticsearch)
a PUBLIC-looking host is the anomaly — document queries would leave the
machine, and a private-only egress scope silently excludes the engine.
"""

from local_deep_research.security.egress.warnings import (
    _extract_engine_hosts,
    check_local_engine_public_url,
)


class TestExtractEngineHosts:
    def test_single_url_string(self):
        assert _extract_engine_hosts("http://localhost:8000") == ["localhost"]

    def test_list_of_urls(self):
        assert _extract_engine_hosts(
            ["http://localhost:9200", "https://es.example.com:9200"]
        ) == ["localhost", "es.example.com"]

    def test_json_string_list(self):
        assert _extract_engine_hosts('["http://localhost:9200"]') == [
            "localhost"
        ]

    def test_comma_string(self):
        assert _extract_engine_hosts(
            "http://a.example.com:9200, http://b.example.com:9200"
        ) == ["a.example.com", "b.example.com"]

    def test_bare_host_port_form(self):
        """Elasticsearch accepts bare host:port entries."""
        assert _extract_engine_hosts(["es.example.com:9200"]) == [
            "es.example.com"
        ]

    def test_garbage_is_skipped(self):
        assert _extract_engine_hosts([42, "", None, "   "]) == []
        assert _extract_engine_hosts(None) == []
        assert _extract_engine_hosts("") == []


class TestFires:
    def test_fires_for_public_paperless_url(self):
        w = check_local_engine_public_url(
            "Paperless-ngx",
            "paperless",
            "https://paperless.example.com",
            True,
            False,
        )
        assert w is not None
        assert w["type"] == "paperless_public_url"
        assert w["dismissKey"] == "app.warnings.dismiss_paperless_public_url"
        assert "paperless.example.com" in w["message"]
        assert w["actionUrl"].startswith("/")
        assert not w["actionUrl"].startswith("//")

    def test_fires_for_one_public_host_among_private(self):
        w = check_local_engine_public_url(
            "Elasticsearch",
            "elasticsearch",
            ["http://localhost:9200", "https://es.example.com:9200"],
            True,
            False,
        )
        assert w is not None
        assert w["type"] == "elasticsearch_public_url"
        assert "es.example.com" in w["message"]
        # Only the flagged (non-private) hosts are named, never the private ones.
        assert "localhost" not in w["message"]

    def test_fires_for_public_ipv6_literal(self):
        """IPv6 literals contain no dot — they must not slip through the
        dotless service-name shortcut (regression pinned by review)."""
        w = check_local_engine_public_url(
            "Elasticsearch",
            "elasticsearch",
            ["http://[2600::1]:9200"],
            True,
            False,
        )
        assert w is not None
        assert "2600::1" in w["message"]

    def test_fires_for_cloud_id_even_with_private_hosts(self):
        w = check_local_engine_public_url(
            "Elasticsearch",
            "elasticsearch",
            ["http://localhost:9200"],
            True,
            False,
            extra="Elastic Cloud (cloud_id)",
        )
        assert w is not None
        assert "Elastic Cloud" in w["message"]


class TestStaysSilent:
    def test_silent_for_private_urls(self):
        for urls in (
            "http://localhost:8000",
            ["http://localhost:9200"],
            ["http://127.0.0.1:9200", "http://10.0.0.5:9200"],
            "http://paperless.local:8000",
            # Dotless single labels: Docker/compose service names can never
            # be public DNS — the standard compose setup must not warn.
            "http://paperless:8000",
            ["http://elasticsearch:9200"],
            # CGNAT (Tailscale / rootless Podman): the egress gate treats
            # 100.64.0.0/10 as private, so the banner must agree.
            "http://100.64.0.5:8000",
            # Private IPv6 ULA — also dotless, must classify as an IP
            # literal, not fall into the service-name shortcut.
            "http://[fd00::1]:9200",
        ):
            assert (
                check_local_engine_public_url(
                    "Paperless-ngx", "paperless", urls, True, False
                )
                is None
            ), f"expected silence for {urls}"

    def test_silent_when_inactive(self):
        assert (
            check_local_engine_public_url(
                "Paperless-ngx",
                "paperless",
                "https://paperless.example.com",
                False,
                False,
            )
            is None
        )

    def test_silent_when_acknowledged(self):
        assert (
            check_local_engine_public_url(
                "Paperless-ngx",
                "paperless",
                "https://paperless.example.com",
                True,
                True,
            )
            is None
        )

    def test_silent_for_empty_config(self):
        assert (
            check_local_engine_public_url(
                "Elasticsearch", "elasticsearch", [], True, False
            )
            is None
        )
        assert (
            check_local_engine_public_url(
                "Paperless-ngx", "paperless", "", True, False
            )
            is None
        )