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.

7.6 KB
Raw Blame
import ipaddress
import socket
from unittest.mock import patch

import pytest

from local_deep_research.security.notification_validator import (
    NotificationURLValidator,
)
from local_deep_research.security.legacy_ipv4 import (
    is_ambiguous_numeric_ipv4_host,
)
from local_deep_research.security.ssrf_validator import validate_url


_PUBLIC_ADDRINFO = [
    (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 0)),
]
_PUBLIC_RESOLVED_IPS = [ipaddress.IPv4Address("93.184.216.34")]
_AMBIGUOUS_NUMERIC_IPV4_ERROR = "Ambiguous numeric IPv4 host"
_LEGACY_NUMERIC_IPV4_HOSTS = (
    "2130706433",
    "0x7f000001",
    "0X7F000001",
    "0177.0.0.1",
    "127.1",
    "127.0.1",
    "0x7f.1",
    "0x7f.0.0.1",
    "2852039166",
    "0xa9fea9fe",
    "0XA9FEA9FE",
    "0251.0376.0251.0376",
    "0xa9.0xfe.0251.0376",
)


def test_validators_preserve_canonical_and_nonlegacy_hosts():
    # Given: public DNS answers for inputs that use hostname resolution.
    with (
        patch(
            "local_deep_research.security.ssrf_validator.socket.getaddrinfo",
            return_value=_PUBLIC_ADDRINFO,
        ),
        patch.object(
            NotificationURLValidator,
            "_resolve_hostname_ips",
            return_value=_PUBLIC_RESOLVED_IPS,
        ),
    ):
        # When: canonical addresses, DNS names, and malformed lookalikes validate.
        for url in (
            "https://8.8.8.8/",
            "https://[2001:4860:4860::8888]/",
            "https://example.com/",
            "https://0xZZ/",
        ):
            # Then: the existing public-destination outcome is preserved.
            assert validate_url(url) is True
            assert NotificationURLValidator.validate_service_url(url) == (
                True,
                None,
            )

        # When: a token-style plugin uses an opaque numeric-looking token.
        is_valid, error = NotificationURLValidator.validate_service_url(
            "discord://2130706433/webhook-token"
        )

    # Then: it remains a valid plugin URL.
    assert (is_valid, error) == (True, None)


@pytest.mark.parametrize("host", _LEGACY_NUMERIC_IPV4_HOSTS)
def test_validate_url_rejects_legacy_numeric_ipv4_before_dns(host):
    # Given: a resolver that would otherwise classify the host as public.
    with patch(
        "local_deep_research.security.ssrf_validator.socket.getaddrinfo",
        return_value=_PUBLIC_ADDRINFO,
    ) as resolver:
        # When: general SSRF validation receives a legacy numeric IPv4 host.
        is_valid = validate_url(f"https://{host}/")

    # Then: it is rejected without invoking the resolver.
    assert is_valid is False
    assert resolver.call_count == 0


@pytest.mark.parametrize("host", _LEGACY_NUMERIC_IPV4_HOSTS)
def test_service_url_rejects_legacy_numeric_ipv4_before_dns(host):
    # Given: a resolver that would otherwise classify the host as public.
    with patch.object(
        NotificationURLValidator,
        "_resolve_hostname_ips",
        return_value=_PUBLIC_RESOLVED_IPS,
    ) as resolver:
        # When: notification validation receives a legacy numeric IPv4 host.
        result = NotificationURLValidator.validate_service_url(
            f"https://{host}/"
        )

    # Then: it returns the stable rejection reason without resolution.
    assert result == (False, _AMBIGUOUS_NUMERIC_IPV4_ERROR)
    assert resolver.call_count == 0


@pytest.mark.parametrize("host", _LEGACY_NUMERIC_IPV4_HOSTS)
def test_service_url_hint_rejects_legacy_numeric_ipv4_before_dns(host):
    # Given: a resolver that would otherwise classify the host as public.
    with patch.object(
        NotificationURLValidator,
        "_resolve_hostname_ips",
        return_value=_PUBLIC_RESOLVED_IPS,
    ) as resolver:
        # When: hint-aware notification validation receives a legacy host.
        result = NotificationURLValidator.validate_service_url_with_hint(
            f"https://{host}/"
        )

    # Then: the structural rejection has no DNS or operator-setting hint.
    assert result == (False, _AMBIGUOUS_NUMERIC_IPV4_ERROR, False)
    assert resolver.call_count == 0


@pytest.mark.parametrize("host", _LEGACY_NUMERIC_IPV4_HOSTS)
def test_host_plugin_rejects_legacy_numeric_ipv4_before_dns(host):
    # Given: a resolver that would otherwise classify the host as public.
    with patch.object(
        NotificationURLValidator,
        "_resolve_hostname_ips",
        return_value=_PUBLIC_RESOLVED_IPS,
    ) as resolver:
        # When: a host-bearing plugin receives a legacy numeric IPv4 host.
        result = NotificationURLValidator.validate_service_url(
            f"signal://{host}/+15551234567/+15557654321"
        )

    # Then: it is rejected without changing plugin LAN reachability policy.
    assert result == (False, _AMBIGUOUS_NUMERIC_IPV4_ERROR)
    assert resolver.call_count == 0


@pytest.mark.parametrize(
    "scheme", sorted(NotificationURLValidator.HOST_BEARING_PLUGIN_SCHEMES)
)
def test_each_host_plugin_rejects_legacy_numeric_ipv4_before_dns(scheme):
    # Given: every plugin scheme that identifies a network host.
    host = "0xa9fea9fe"
    url = (
        f"mailto://user@{host}/recipient"
        if scheme == "mailto"
        else f"{scheme}://{host}/path"
    )
    with patch.object(
        NotificationURLValidator,
        "_resolve_hostname_ips",
        return_value=_PUBLIC_RESOLVED_IPS,
    ) as resolver:
        # When: the plugin receives an ambiguous metadata-address form.
        result = NotificationURLValidator.validate_service_url(url)

    # Then: the shared address grammar rejects it before resolution.
    assert result == (False, _AMBIGUOUS_NUMERIC_IPV4_ERROR)
    assert resolver.call_count == 0


@pytest.mark.parametrize(
    "url",
    (
        "discord://2130706433/webhook-token",
        "slack://2130706433/token-a/token-b",
        # tgram authority is a bot CREDENTIAL (<bot_id>:<token>), and a
        # bare numeric bot id is indistinguishable from a legacy numeric
        # IPv4 host, so it must stay exempt from the address grammar.
        "tgram://2130706433:AAexample_token/123456789",
        "pushover://2130706433/application-token",
        "teams://2130706433/token-a/token-b/token-c",
    ),
)
def test_token_plugins_numeric_hosts_skip_address_checks(url):
    # Given: a token-style plugin with an opaque numeric-looking token.
    with patch.object(
        NotificationURLValidator,
        "_resolve_hostname_ips",
        return_value=_PUBLIC_RESOLVED_IPS,
    ) as resolver:
        # When: notification validation receives the token-style URL.
        result = NotificationURLValidator.validate_service_url(url)

    # Then: no host-address grammar or resolution is applied.
    assert result == (True, None)
    assert resolver.call_count == 0


@pytest.mark.parametrize(
    ("host", "expected"),
    (
        ("0xffffffff", True),
        ("0xff.0xffffff", True),
        ("0xff.0xff.0xffff", True),
        ("0xff.0xff.0xff.0xff", True),
        ("4294967296", False),
        ("0x100000000", False),
        ("256.0", False),
        ("255.16777216", False),
        ("255.256.0", False),
        ("255.255.65536", False),
        ("255.255.255.256", False),
        ("0x", False),
        ("0xGG", False),
        ("08", False),
        ("0x7g.1", False),
        ("", False),
        (".", False),
        ("1..1", False),
        ("1.2.3.4.5", False),
        ("123.example.com", False),
    ),
)
def test_legacy_numeric_ipv4_parser_honors_grammar_bounds(host, expected):
    # Given: a possible legacy address form or numeric-looking DNS name.
    # When: the pure historical IPv4 parser evaluates it.
    actual = is_ambiguous_numeric_ipv4_host(host)

    # Then: only bounded one-to-four-part grammar is recognized.
    assert actual is expected