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.

27.0 KB
Raw Blame
"""Effective notification destination and send-time guard regressions."""

from contextlib import nullcontext
import ipaddress
from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.notifications.service import NotificationService
from local_deep_research.security import dns_pinning
from local_deep_research.security.notification_validator import (
    NotificationURLValidator,
)


@pytest.mark.parametrize(
    "url",
    [
        "json://127.0.0.1:8000/hook",
        "signal://10.0.0.5:8739/+15551234567/+15557654321",
        "gotify://10.0.0.5:8080/token",
        "ntfy://127.0.0.1/topic?mode=private",
        "matrix://user:[email protected]/%23room?mode=matrix",
    ],
)
def test_authority_plugin_needs_operator_private_opt_in(url):
    denied, reason = NotificationURLValidator.validate_service_url(url)
    allowed, _ = NotificationURLValidator.validate_service_url(
        url, allow_private_ips=True
    )
    assert denied is False
    assert "private/internal" in reason.lower()
    assert allowed is True


def test_authority_plugin_link_local_remains_blocked_with_opt_in():
    allowed, reason = NotificationURLValidator.validate_service_url(
        "json://169.254.10.5/hook", allow_private_ips=True
    )
    assert allowed is False
    assert "link-local" in reason.lower()


def test_dispatch_separates_authority_and_vendor_dns_guards(monkeypatch):
    service = NotificationService(allow_private_ips=True, outbound_allowed=True)
    service._new_apprise = MagicMock()
    service._new_apprise.return_value.add.return_value = True
    guards = []

    def fake_guard(urls, **policy):
        guards.append((urls, policy))
        return nullcontext()

    def fake_send(_title, _body, _apprise, guard_factory, _tag, _attach):
        with guard_factory():
            pass
        return True

    monkeypatch.setattr(dns_pinning, "pinned_notification_send", fake_guard)
    monkeypatch.setattr(service, "_send_with_retry", fake_send)
    assert service._dispatch(
        "title",
        "body",
        [],
        ["json://10.0.0.5/hook", "discord://id/token"],
        None,
        None,
    )
    assert len(guards) == 2
    assert guards[0][0] == ["json://10.0.0.5/hook"]
    assert guards[0][1]["allow_private_ips"] is True
    assert guards[0][1]["block_link_local"] is True
    assert guards[1][0] == ["discord://id/token"]
    # Fixed vendor endpoints follow the operator opt-in too (a private
    # forward proxy is the only private lookup they make).
    assert guards[1][1]["allow_private_ips"] is True
    assert guards[1][1]["block_link_local"] is True


def test_dispatch_vendor_guard_is_public_only_by_default(monkeypatch):
    service = NotificationService(outbound_allowed=True)
    service._new_apprise = MagicMock()
    service._new_apprise.return_value.add.return_value = True
    guards = []

    def fake_guard(urls, **policy):
        guards.append((urls, policy))
        return nullcontext()

    def fake_send(_title, _body, _apprise, guard_factory, _tag, _attach):
        with guard_factory():
            pass
        return True

    monkeypatch.setattr(dns_pinning, "pinned_notification_send", fake_guard)
    monkeypatch.setattr(service, "_send_with_retry", fake_send)
    assert service._dispatch(
        "title", "body", [], ["discord://id/token"], None, None
    )
    assert guards == [
        (
            ["discord://id/token"],
            {
                "allow_localhost": False,
                "allow_private_ips": False,
                "block_link_local": True,
            },
        )
    ]


@pytest.mark.parametrize(
    "url",
    [
        "signal://evil.com%2F.local/+15551234567/+15557654321",
        "signal://evil.com%23.local/+15551234567/+15557654321",
        "signal://evil.com%3F.local/+15551234567/+15557654321",
        "signal://10.0.0.5%2Fevil.com/+15551234567/+15557654321",
        "json://evil.com%2F.local/hook",
        "gotify://evil.com%252F.local/token",
        "ntfy://evil.com%2F.local/topic",
        "mailto://u:[email protected]%2F.local",
        "matrix://u:[email protected]%2F.local/%23room",
    ],
)
def test_percent_encoded_plugin_host_is_refused(url):
    from local_deep_research.security.notification_destination import (
        NotificationDestinationError,
        parse_notification_destination,
    )

    with pytest.raises(NotificationDestinationError):
        parse_notification_destination(url)
    with patch.object(
        NotificationURLValidator,
        "_resolve_hostname_ips",
        side_effect=AssertionError("ambiguous plugin host must not resolve"),
    ):
        allowed, _reason = NotificationURLValidator.validate_service_url(
            url, allow_private_ips=True
        )
    assert allowed is False


def test_destination_error_survives_contextmanager_traceback():
    from contextlib import contextmanager

    from local_deep_research.security.notification_destination import (
        NotificationDestinationError,
    )

    @contextmanager
    def scope():
        yield

    with pytest.raises(NotificationDestinationError) as caught:
        with scope():
            raise NotificationDestinationError("reason")
    assert str(caught.value) == "reason"
    assert caught.value.reason == "reason"


def test_admin_test_url_explains_private_plugin_opt_in():
    service = NotificationService(outbound_allowed=True)
    result = service.test_service("json://127.0.0.1:8000/hook")
    assert result["success"] is False
    assert "LDR_NOTIFICATIONS_ALLOW_PRIVATE_IPS=true" in result["error"]


def test_admin_test_url_does_not_suggest_opt_in_for_link_local():
    service = NotificationService(allow_private_ips=True, outbound_allowed=True)
    result = service.test_service("json://169.254.10.5/hook")
    assert result["success"] is False
    assert "LDR_NOTIFICATIONS_ALLOW_PRIVATE_IPS" not in result["error"]


def test_direct_plugin_validation_uses_one_dns_answer_for_decision():
    with patch.object(
        NotificationURLValidator,
        "_resolve_hostname_ips",
        return_value=[ipaddress.ip_address("10.0.0.5")],
    ) as resolver:
        allowed, reason = NotificationURLValidator.validate_service_url(
            "json://hook.example/hook"
        )
    assert allowed is False
    assert "private/internal" in reason.lower()
    resolver.assert_called_once_with("hook.example")


@pytest.mark.parametrize(
    "url",
    [
        "ntfy://169.254.169.254?to=x",
        "ntfys://169.254.169.254/?to=alerts",
        "ntfy://169.254.169.254?x=1;To=x",
    ],
)
def test_ntfy_to_query_topic_still_blocks_metadata_host(url):
    allowed, reason = NotificationURLValidator.validate_service_url(
        url, allow_private_ips=True
    )
    assert allowed is False
    assert "metadata" in reason.lower()


def test_ntfy_to_query_topic_needs_private_opt_in_like_path_topic():
    for url in ("ntfy://10.0.0.5/topic", "ntfy://10.0.0.5?to=topic"):
        denied, reason = NotificationURLValidator.validate_service_url(url)
        allowed, _ = NotificationURLValidator.validate_service_url(
            url, allow_private_ips=True
        )
        assert denied is False
        assert "private/internal" in reason.lower()
        assert allowed is True


# The classification must match where Apprise really sends, in BOTH
# directions: VENDOR skips host checks, so Apprise must not send to the URL
# host; AUTHORITY(host) passes the egress scope on that host, so Apprise must
# not send anywhere else (for example ntfy cloud mode posting to ntfy.sh).
# ntfy ground truth is the URL Apprise actually POSTs to with requests
# mocked; Matrix ground truth is the mode and host of the plugin Apprise
# builds.
_TOKEN = "a" * 64
_VENDOR_HOSTS = {"ntfy.sh", "webhooks.t2bot.io"}
_APPRISE_HOST_CASES = [
    f"{scheme}://{host}{path}{query}"
    for scheme, hosts in (
        (
            "ntfy",
            (
                "evil.example.com",
                "10.0.0.5",
                "localhost:8080",
                "topic",
                "homelab_",
                "a.1.lan",
                "foo..lan",
                "Ntfy.Sh.",
            ),
        ),
        ("ntfys", ("evil.example.com", "[fd00::1]")),
        ("matrix", ("evil.example.com", _TOKEN, "[email protected]")),
    )
    for host in hosts
    for path in ("", "/", "/t", "/%20", "#t", "/#/t", "/#x/t", "#x?to=t")
    for query in (
        "",
        "?to=t",
        "?To=t",
        "?%74o=t",
        "?x=1;to=t",
        "?to=",
        "?to=,",
        "?to=%20",
        "?mode=private",
        "?mode=cloud&to=t",
        "?mode=private&to=t",
        "?mode=hookshot",
    )
] + [
    "ntfy://localhost?to=",
    "ntfy://ntfy?to=,",
    "ntfy://homelab_/alerts",
    "ntfy://nas.lan/?to=%20",
    "ntfys://ntfy-server?to=",
    "ntfy://10.0.0.5:8080/#/alerts",
]


def _apprise_send_hosts(url):
    from urllib.parse import urlsplit

    import apprise
    from apprise.plugins.matrix import NotifyMatrix
    from apprise.plugins.ntfy import NotifyNtfy

    plugin = apprise.Apprise.instantiate(
        url, asset=apprise.AppriseAsset(async_mode=False)
    )
    if url.startswith("matrix"):
        # The plugin Apprise.add builds (url_to_dict rewrites ``/#`` to
        # ``/%23`` before parsing, so NotifyMatrix.parse_url alone differs).
        if not isinstance(plugin, NotifyMatrix):
            return set()
        if str(plugin.mode or "").lower() == "t2bot":
            return {"webhooks.t2bot.io"}
        return {plugin.host.strip("[]").rstrip(".").lower()}
    if not isinstance(plugin, NotifyNtfy):
        return set()
    plugin.request_rate_per_sec = 0
    hosts = set()

    def record(target, *args, **kwargs):
        hosts.add((urlsplit(target).hostname or "").rstrip(".").lower())
        response = MagicMock(status_code=200, content=b"{}", text="{}")
        response.json.return_value = {}
        return response

    with patch("apprise.plugins.ntfy.requests.post", side_effect=record):
        plugin.send(body="body", title="title")
    return hosts


@pytest.mark.parametrize(
    "url", _APPRISE_HOST_CASES, ids=range(len(_APPRISE_HOST_CASES))
)
def test_classification_matches_apprise_send_destination(url):
    from local_deep_research.security.notification_destination import (
        NotificationDestinationError,
        NotificationDestinationKind,
        parse_notification_destination,
    )

    try:
        destination = parse_notification_destination(url)
    except NotificationDestinationError:
        return
    sent_to = _apprise_send_hosts(url)
    if destination.kind is NotificationDestinationKind.VENDOR_PLUGIN:
        assert sent_to <= _VENDOR_HOSTS, (url, sent_to)
    else:
        assert destination.kind is NotificationDestinationKind.AUTHORITY_PLUGIN
        assert sent_to <= {destination.effective_host}, (url, sent_to)


@pytest.mark.parametrize(
    ("url", "kind", "host"),
    [
        # Apprise cloud mode: an empty/blank ``to=`` is not a topic and an
        # invalid host becomes a topic; both POST to ntfy.sh.
        ("ntfy://localhost?to=", "vendor_plugin", None),
        ("ntfy://ntfy?to=,", "vendor_plugin", None),
        ("ntfy://nas.lan/?to=%20", "vendor_plugin", None),
        ("ntfy://homelab_/alerts", "vendor_plugin", None),
        ("ntfy://a.1.lan/topic", "vendor_plugin", None),
        # Apprise private mode: a topic in ``to=`` or after ``/#/`` sends
        # to the URL host.
        (
            "ntfy://evil.example.com?to=topic",
            "authority_plugin",
            "evil.example.com",
        ),
        (
            "ntfy://evil.example.com/#/topic",
            "authority_plugin",
            "evil.example.com",
        ),
        ("ntfy://10.0.0.5:8080/#/alerts", "authority_plugin", "10.0.0.5"),
        ("ntfy://nas.lan/topic", "authority_plugin", "nas.lan"),
        ("ntfy://topic", "vendor_plugin", None),
    ],
)
def test_ntfy_classification_follows_apprise_mode(url, kind, host):
    from local_deep_research.security.notification_destination import (
        parse_notification_destination,
    )

    destination = parse_notification_destination(url)
    assert (destination.kind.value, destination.effective_host) == (kind, host)


@pytest.mark.parametrize(
    ("url", "host"),
    [
        ("json://Hook.Example.com/hook", "hook.example.com"),
        ("gotify://NAS.Example.com:8080/token", "nas.example.com"),
        ("mailto://me:[email protected]", "mail.example.com"),
        ("ntfy://Ntfy.Example.com/topic", "ntfy.example.com"),
    ],
)
def test_mixed_case_plugin_host_is_not_ambiguous(url, host):
    with patch.object(
        NotificationURLValidator,
        "_resolve_hostname_ips",
        return_value=[ipaddress.ip_address("93.184.216.34")],
    ) as resolver:
        assert NotificationURLValidator.validate_service_url(url) == (
            True,
            None,
        )
    resolver.assert_called_once_with(host)


def test_mixed_case_ipv6_plugin_literal_follows_private_policy():
    # Uncompressed: Apprise 1.13 rewrites a compressed literal such as
    # ``[FD00::1]`` (see test_ipv6_literal_rewritten_by_apprise_is_refused).
    url = "json://[FD00:0:0:0:0:0:0:1]/hook"
    denied, reason = NotificationURLValidator.validate_service_url(url)
    assert denied is False
    assert "private/internal" in reason.lower()
    assert NotificationURLValidator.validate_service_url(
        url, allow_private_ips=True
    ) == (True, None)


def _zone_id_metadata_hosts():
    from local_deep_research.security.ssrf_validator import (
        ALWAYS_BLOCKED_METADATA_IPS,
    )

    hosts = []
    for ip in sorted(ALWAYS_BLOCKED_METADATA_IPS):
        literal = ip if ":" in ip else f"::ffff:{ip}"
        hosts += [f"[{literal}%25eth0]", f"[{literal}%eth0]"]
    return hosts


@pytest.mark.parametrize("allow_private_ips", [False, True])
@pytest.mark.parametrize("scheme", ["http", "https", "json", "gotify"])
@pytest.mark.parametrize("host", _zone_id_metadata_hosts())
def test_zone_id_metadata_literal_is_refused(host, scheme, allow_private_ips):
    url = f"{scheme}://{host}/path"
    with patch.object(
        NotificationURLValidator,
        "_resolve_hostname_ips",
        side_effect=AssertionError("an IP literal must not resolve"),
    ):
        allowed, _reason = NotificationURLValidator.validate_service_url(
            url, allow_private_ips=allow_private_ips
        )
    assert allowed is False


@pytest.mark.parametrize("allow_private_ips", [False, True])
def test_scoped_metadata_address_is_blocked_at_send_time(allow_private_ips):
    from local_deep_research.security.ssrf_validator import is_ip_blocked

    assert is_ip_blocked(
        "fd00:ec2::254%eth0", allow_private_ips=allow_private_ips
    )


# Apprise's email parser discards an authority it does not accept as a
# hostname (an empty port, an underscore) and takes the SMTP host from the
# domain of an address in the userinfo or ``?user=``. LDR must not classify
# such a URL by its authority.
_MAIL_REDIRECT_URLS = [
    "mailto://public.example.com:[email protected]",
    "mailto://public.example.com:[email protected]",
    "mailto://u%40intranet.test:[email protected]:",
    "mailto://u:pw@[email protected]",
]


@pytest.mark.parametrize("url", _MAIL_REDIRECT_URLS)
def test_mailto_smtp_host_moved_by_apprise_is_refused(url):
    from local_deep_research.security.notification_destination import (
        NotificationDestinationError,
        parse_notification_destination,
    )

    with pytest.raises(NotificationDestinationError):
        parse_notification_destination(url)
    with patch.object(
        NotificationURLValidator,
        "_resolve_hostname_ips",
        return_value=[ipaddress.ip_address("93.184.216.34")],
    ):
        allowed, _reason = NotificationURLValidator.validate_service_url(
            url, allow_private_ips=True
        )
    assert allowed is False


@pytest.mark.parametrize(
    ("url", "host"),
    [
        ("mailto://u:[email protected]", "mail.example.com"),
        ("mailto://[email protected]", "example.com"),
        # Built-in provider: Apprise connects to its fixed smtp.gmail.com.
        ("mailto://u:[email protected]", "gmail.com"),
        ("mailto://[email protected]", "gmail.com"),
    ],
)
def test_mailto_authority_or_provider_mapping_is_accepted(url, host):
    from local_deep_research.security.notification_destination import (
        parse_notification_destination,
    )

    destination = parse_notification_destination(url)
    assert (destination.kind.value, destination.effective_host) == (
        "authority_plugin",
        host,
    )


_MAIL_DIFFERENTIAL_CASES = [
    f"mailto://{userinfo}{host}{port}{query}"
    for userinfo in ("", "u:p@", "u%40intranet.test:p@", "x@")
    for host in ("public.example.com", "lo_cal.lan", "10.0.0.5", "gmail.com")
    for port in ("", ":", ":25")
    for query in ("", "[email protected]", "[email protected]")
]


@pytest.mark.parametrize(
    "url", _MAIL_DIFFERENTIAL_CASES, ids=range(len(_MAIL_DIFFERENTIAL_CASES))
)
def test_mailto_classification_matches_apprise_smtp_host(url):
    import apprise
    from apprise.plugins.email.templates import EMAIL_TEMPLATES

    from local_deep_research.security.notification_destination import (
        NotificationDestinationError,
        parse_notification_destination,
    )

    try:
        destination = parse_notification_destination(url)
    except NotificationDestinationError:
        return
    plugin = apprise.Apprise.instantiate(
        url, asset=apprise.AppriseAsset(async_mode=False)
    )
    if plugin is None:
        return
    provider_hosts = {
        template[2]["smtp_host"]
        for template in EMAIL_TEMPLATES
        if template[2].get("smtp_host")
    }
    smtp_host = plugin.smtp_host.strip("[]").rstrip(".").lower()
    assert smtp_host in {destination.effective_host} | provider_hosts, (
        url,
        smtp_host,
    )


_MATRIX_TOKEN = "a" * 64


@pytest.mark.parametrize(
    ("url", "kind", "host"),
    [
        # Apprise's documented room-alias forms reach the URL host.
        (
            "matrix://user:[email protected]/#room",
            "authority_plugin",
            "matrix.example.com",
        ),
        (
            "matrix://user:[email protected]/?to=%23room",
            "authority_plugin",
            "matrix.example.com",
        ),
        (
            "matrix://user:[email protected]",
            "authority_plugin",
            "matrix.example.com",
        ),
        # Apprise's t2bot forms post to webhooks.t2bot.io.
        (f"matrix://user@{_MATRIX_TOKEN}", "vendor_plugin", None),
        (f"matrix://{_MATRIX_TOKEN}/", "vendor_plugin", None),
        (f"matrix://{_MATRIX_TOKEN}", "vendor_plugin", None),
    ],
)
def test_matrix_forms_accepted_by_apprise_are_classified(url, kind, host):
    from apprise.plugins import url_to_dict

    from local_deep_research.security.notification_destination import (
        parse_notification_destination,
    )

    apprise_mode = str(url_to_dict(url).get("mode") or "").lower()
    assert (apprise_mode == "t2bot") is (kind == "vendor_plugin")
    destination = parse_notification_destination(url)
    assert (destination.kind.value, destination.effective_host) == (
        kind,
        host,
    )
    with patch.object(
        NotificationURLValidator,
        "_resolve_hostname_ips",
        return_value=[ipaddress.ip_address("93.184.216.34")],
    ):
        assert NotificationURLValidator.validate_service_url(url) == (
            True,
            None,
        )


@pytest.mark.parametrize(
    "url",
    ["http://169.254.10.5/notify/abc", "https://[fe80::1]/x"],
)
def test_http_link_local_is_blocked_even_with_opt_in(url):
    # Apprise turns http://<host>/notify/<token> into its Apprise API
    # plugin, which posts to the URL host.
    allowed, reason = NotificationURLValidator.validate_service_url(
        url, allow_private_ips=True
    )
    assert allowed is False
    assert "link-local" in reason.lower()


def test_dispatch_http_guard_blocks_link_local(monkeypatch):
    service = NotificationService(allow_private_ips=True, outbound_allowed=True)
    service._new_apprise = MagicMock()
    service._new_apprise.return_value.add.return_value = True
    guards = []

    def fake_guard(urls, **policy):
        guards.append((urls, policy))
        return nullcontext()

    def fake_send(_title, _body, _apprise, guard_factory, _tag, _attach):
        with guard_factory():
            pass
        return True

    monkeypatch.setattr(dns_pinning, "pinned_notification_send", fake_guard)
    monkeypatch.setattr(service, "_send_with_retry", fake_send)
    assert service._dispatch(
        "title", "body", ["http://hook.example/notify/abc"], [], None, None
    )
    assert guards == [
        (
            ["http://hook.example/notify/abc"],
            {
                "allow_localhost": False,
                "allow_private_ips": True,
                "block_link_local": True,
            },
        )
    ]


def test_test_service_http_guard_blocks_link_local(monkeypatch):
    service = NotificationService(allow_private_ips=True, outbound_allowed=True)
    guards = []

    def fake_guard(urls, **policy):
        guards.append((urls, policy))
        return nullcontext()

    monkeypatch.setattr(dns_pinning, "pinned_notification_send", fake_guard)
    monkeypatch.setattr(
        NotificationURLValidator,
        "validate_service_url_with_hint",
        staticmethod(lambda *_args, **_kwargs: (True, None, False)),
    )

    def fake_notify(_title, _body, _apprise, guard_factory, *_args, **_kw):
        with guard_factory():
            return True

    monkeypatch.setattr(service, "_guarded_notify", fake_notify)
    service.test_service("http://hook.example/notify/abc")
    assert guards, "test_service never built its send guard"
    assert guards[0][1]["block_link_local"] is True


@pytest.mark.parametrize(
    "url", ["http://[fd00::1%25eth0]/x", "https://[fe80::1%25eth0]/x"]
)
def test_zone_identifier_is_refused_by_the_classifier(url):
    from local_deep_research.security.notification_destination import (
        NotificationDestinationError,
        parse_notification_destination,
    )

    with pytest.raises(NotificationDestinationError):
        parse_notification_destination(url)


# Apprise 1.13's generic URL parser rewrites compressed IPv6 literals:
# ``json://[fd00::1]/x`` builds a plugin whose host is ``[fd00::]`` and
# ``[::ffff:10.0.0.5]`` becomes ``[::ffff:10]``. Policy would vet one
# address while Apprise connects to another, so the classifier refuses it.
_APPRISE_REWRITTEN_IPV6_URLS = [
    "json://[fd00::1]/hook",
    "json://[fd00::1]:8080/hook",
    "xml://[2001:db8::1]/hook",
    "form://[::ffff:10.0.0.5]/hook",
    "gotify://[FD00::1]/token",
    "json://[fe80::1]/hook",
    "signal://[::ffff:10.0.0.5]:8080/+15551234567/+15557654321",
    "signal://[FD00::1]:8080/+15551234567/+15557654321",
]


@pytest.mark.parametrize("url", _APPRISE_REWRITTEN_IPV6_URLS)
def test_ipv6_literal_rewritten_by_apprise_is_refused(url):
    from urllib.parse import urlsplit

    from apprise import Apprise

    from local_deep_research.security.notification_destination import (
        NotificationDestinationError,
        parse_notification_destination,
    )

    # Premise: Apprise builds this URL with a host that is not the URL's
    # address (another address, or for Signal with a port a mixed-case or
    # mapped literal that keeps ``:port`` inside the host).
    plugin = Apprise.instantiate(url)
    assert plugin is not None
    literal = ipaddress.ip_address(urlsplit(url).hostname)
    try:
        assert ipaddress.ip_address(plugin.host.strip("[]")) != literal
    except ValueError:
        pass

    with pytest.raises(NotificationDestinationError):
        parse_notification_destination(url)
    for allow_private_ips in (False, True):
        assert NotificationURLValidator.validate_service_url(
            url, allow_private_ips=allow_private_ips
        ) == (False, "Notification destination is unsupported or ambiguous")


@pytest.mark.parametrize(
    ("url", "host"),
    [
        ("json://[fd00:0:0:0:0:0:0:1]/hook", "fd00:0:0:0:0:0:0:1"),
        ("gotify://[fd00::]:8080/token", "fd00::"),
        ("xml://[::1]/hook", "::1"),
        (
            "signal://[fd00::1]/+15551234567/+15557654321",
            "fd00::1",
        ),
        # Apprise does not build these, so it sends nothing to any host.
        ("mattermost://[fd00::1]/token", "fd00::1"),
        ("http://[fd00::1]/notify/key", "fd00::1"),
    ],
)
def test_ipv6_literal_kept_by_apprise_is_classified(url, host):
    from apprise import Apprise

    from local_deep_research.security.notification_destination import (
        parse_notification_destination,
    )

    destination = parse_notification_destination(url)
    assert destination.effective_host == host
    plugin = Apprise.instantiate(url)
    if plugin is not None:
        assert ipaddress.ip_address(
            plugin.host.strip("[]")
        ) == ipaddress.ip_address(host)


# Apprise's port regex for a bracketed literal only matches lowercase hex, so
# an uppercase literal keeps its host but loses its port: the plugin built
# from ``json://[FD00:0:0:0:0:0:0:1]:8080/hook`` posts to port 80. The
# classifier compares the port as well as the host and refuses it.
_APPRISE_PORT_DROPPED_IPV6_URLS = [
    "json://[FD00:0:0:0:0:0:0:1]:8080/hook",
    "xml://[FD00:0:0:0:0:0:0:1]:8080/hook",
    "form://[Fd00:0:0:0:0:0:0:1]:8443/hook",
    "gotify://[FD00::]:8080/token",
    "json://[fd00:0:0:0:0:0:0:A]:8080/hook",
]


@pytest.mark.parametrize("url", _APPRISE_PORT_DROPPED_IPV6_URLS)
def test_ipv6_literal_whose_port_apprise_drops_is_refused(url):
    from urllib.parse import urlsplit

    from apprise import Apprise

    from local_deep_research.security.notification_destination import (
        NotificationDestinationError,
        parse_notification_destination,
    )

    # Premise: Apprise keeps the address but drops the URL's port.
    plugin = Apprise.instantiate(url)
    assert plugin is not None
    parts = urlsplit(url)
    assert ipaddress.ip_address(
        plugin.host.strip("[]")
    ) == ipaddress.ip_address(parts.hostname)
    assert parts.port is not None
    assert plugin.port != parts.port

    with pytest.raises(NotificationDestinationError):
        parse_notification_destination(url)
    for allow_private_ips in (False, True):
        assert NotificationURLValidator.validate_service_url(
            url, allow_private_ips=allow_private_ips
        ) == (False, "Notification destination is unsupported or ambiguous")


@pytest.mark.parametrize(
    "url",
    [
        "json://[fd00:0:0:0:0:0:0:1]:8080/hook",
        "gotify://[fd00::]:8443/token",
        "signal://[fd00::1]:8080/+15551234567/+15557654321",
        "json://[FD00:0:0:0:0:0:0:1]/hook",
    ],
)
def test_ipv6_literal_whose_port_apprise_keeps_is_classified(url):
    from urllib.parse import urlsplit

    from apprise import Apprise

    from local_deep_research.security.notification_destination import (
        parse_notification_destination,
    )

    parts = urlsplit(url)
    destination = parse_notification_destination(url)
    assert ipaddress.ip_address(
        destination.effective_host
    ) == ipaddress.ip_address(parts.hostname)
    plugin = Apprise.instantiate(url)
    assert plugin is not None
    assert plugin.port == parts.port