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.

72.3 KB
Raw Blame
"""SSRF hardening for the notification (Apprise) delivery path.

These tests prove that the DNS pin + block-private send window
(``security.dns_pinning`` + ``notifications.service``) close the
resolve-vs-connect / DNS-rebinding TOCTOU that Apprise's own send-time
re-resolution would otherwise reopen:

* the ``getaddrinfo`` shim actually intercepts *Apprise's* resolution
  (Apprise is forced synchronous/in-thread so the thread-local guard
  applies) — an end-to-end ``json://`` webhook is delivered, rebound, and
  redirected through the real Apprise + ``requests`` stack;
* a host that validated public but rebinds to a private/metadata IP is
  refused before any connection;
* a webhook that redirects to an internal IP is refused at the socket
  layer during the window;
* plugin schemes with a token "host" (``discord://``) are never resolved
  as a hostname (not broken by pinning); and
* TLS SNI + certificate verification still use the original hostname while
  the socket connects to the pinned IP.
"""

import contextlib
import datetime
import ipaddress
import socket
import ssl
import threading
import time
from http.server import BaseHTTPRequestHandler, HTTPServer
from unittest.mock import patch

import apprise
import pytest
import requests
from cryptography import x509
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.x509.oid import NameOID

from local_deep_research.notifications.exceptions import (
    SecurityBlockError,
    SendError,
    ServiceError,
)
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,
)


# --------------------------------------------------------------------------
# Test doubles
# --------------------------------------------------------------------------
class _Resolver:
    """Drive ``dns_pinning._real_getaddrinfo`` with scripted answers.

    ``answers`` maps host -> list of successive answers (each a list of IP
    strings). Each lookup consumes the next answer; the final answer sticks
    for every later call, so a two-element list simulates a rebind that then
    stays put (validation sees answer #1, the pin/send sees answer #2).
    """

    def __init__(self, answers):
        self._answers = {
            h.lower().rstrip("."): list(v) for h, v in answers.items()
        }
        self.calls = []

    def __call__(self, host, port, family=0, type=0, proto=0, flags=0):
        self.calls.append((host, port))
        norm = (host or "").lower().rstrip(".")
        # A numeric IP literal resolves to ITSELF — exactly what the real
        # getaddrinfo does. Without this, a redirect/rebind whose target is a
        # bare IP literal (e.g. 169.254.169.254) would raise "no answer" here
        # BEFORE any block check could run, making such a test pass for the
        # wrong reason (vacuously green even if the block were deleted).
        try:
            ipaddress.ip_address(norm)
            is_literal = True
        except ValueError:
            is_literal = False
        if is_literal:
            ips = [norm]
        else:
            seq = self._answers.get(norm)
            if not seq:
                raise socket.gaierror(
                    socket.EAI_NONAME, f"no answer for {host}"
                )
            ips = seq[0] if len(seq) == 1 else seq.pop(0)
        results = []
        for ip in ips:
            fam = socket.AF_INET6 if ":" in ip else socket.AF_INET
            sockaddr = (
                (ip, port or 0, 0, 0)
                if fam == socket.AF_INET6
                else (ip, port or 0)
            )
            results.append(
                (fam, socket.SOCK_STREAM, socket.IPPROTO_TCP, "", sockaddr)
            )
        return results


class _RecordingHandler(BaseHTTPRequestHandler):
    """200 OK that records every request path it served."""

    hits = None  # set on the subclass
    redirect_to = None  # optional Location target

    def _handle(self):
        type(self).hits.append(self.path)
        if type(self).redirect_to:
            self.send_response(302)
            self.send_header("Location", type(self).redirect_to)
            self.end_headers()
            return
        body = b"OK"
        self.send_response(200)
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)

    def do_GET(self):
        self._handle()

    def do_POST(self):
        length = int(self.headers.get("Content-Length", 0) or 0)
        if length:
            self.rfile.read(length)
        self._handle()

    def log_message(self, *args):
        pass


def _start_server(redirect_to=None, tls_cert=None):
    """Start a loopback HTTP(S) server; return (server, port, hits)."""
    hits = []
    handler = type(
        "BoundHandler",
        (_RecordingHandler,),
        {"hits": hits, "redirect_to": redirect_to},
    )
    server = HTTPServer(("127.0.0.1", 0), handler)
    if tls_cert is not None:
        cert_file, key_file = tls_cert
        ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
        ctx.load_cert_chain(certfile=cert_file, keyfile=key_file)
        server.socket = ctx.wrap_socket(server.socket, server_side=True)
    threading.Thread(target=server.serve_forever, daemon=True).start()
    return server, server.server_address[1], hits


def _make_self_signed_cert(hostname, tmp_path):
    """Mint a self-signed cert (SAN=DNS:hostname); return (cert, key) paths."""
    key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
    name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostname)])
    now = datetime.datetime.now(datetime.timezone.utc)
    cert = (
        x509.CertificateBuilder()
        .subject_name(name)
        .issuer_name(name)
        .public_key(key.public_key())
        .serial_number(x509.random_serial_number())
        .not_valid_before(now - datetime.timedelta(minutes=5))
        .not_valid_after(now + datetime.timedelta(days=1))
        .add_extension(
            x509.SubjectAlternativeName([x509.DNSName(hostname)]),
            critical=False,
        )
        .add_extension(
            x509.BasicConstraints(ca=True, path_length=None), critical=True
        )
        .sign(key, hashes.SHA256())
    )
    cert_file = tmp_path / "cert.pem"
    key_file = tmp_path / "key.pem"
    cert_file.write_bytes(cert.public_bytes(serialization.Encoding.PEM))
    key_file.write_bytes(
        key.private_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PrivateFormat.TraditionalOpenSSL,
            encryption_algorithm=serialization.NoEncryption(),
        )
    )
    return str(cert_file), str(key_file)


@contextlib.contextmanager
def _connect_spy():
    """Record every ``socket.connect`` target address for the duration."""
    targets = []
    real_connect = socket.socket.connect

    def spy(self, address):
        targets.append(address)
        return real_connect(self, address)

    with patch.object(socket.socket, "connect", spy):
        yield targets


def _service(*, allow_private_ips=False):
    return NotificationService(
        allow_private_ips=allow_private_ips, outbound_allowed=True
    )


# ==========================================================================
# dns_pinning primitives: block_private_resolution
# ==========================================================================
def test_block_private_refuses_metadata_at_socket_layer():
    """An unpinned lookup that resolves to cloud-metadata is refused with a
    gaierror while the block-private window is active (even with
    allow_private_ips=True, metadata is always blocked)."""
    resolver = _Resolver({"redir.example": [["169.254.169.254"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.block_private_resolution(allow_private_ips=True):
            with pytest.raises(socket.gaierror):
                socket.getaddrinfo("redir.example", 80)
        # Outside the window the same lookup resolves normally.
        assert socket.getaddrinfo("redir.example", 80)[0][4][0] == (
            "169.254.169.254"
        )


def test_block_private_refuses_rfc1918_when_not_opted_in():
    resolver = _Resolver({"redir.example": [["10.1.2.3"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.block_private_resolution(allow_private_ips=False):
            with pytest.raises(socket.gaierror):
                socket.getaddrinfo("redir.example", 80)


def test_block_private_allows_rfc1918_when_opted_in_but_still_blocks_metadata():
    resolver = _Resolver(
        {"lan.example": [["10.1.2.3"]], "imds.example": [["169.254.169.254"]]}
    )
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.block_private_resolution(allow_private_ips=True):
            # Private allowed under the opt-in...
            assert socket.getaddrinfo("lan.example", 80)[0][4][0] == "10.1.2.3"
            # ...but metadata is still refused.
            with pytest.raises(socket.gaierror):
                socket.getaddrinfo("imds.example", 80)


def test_block_private_passes_public_through():
    resolver = _Resolver({"ok.example": [["93.184.216.34"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.block_private_resolution(allow_private_ips=False):
            assert (
                socket.getaddrinfo("ok.example", 80)[0][4][0] == "93.184.216.34"
            )


def test_block_private_is_thread_scoped():
    """The window on one thread must not affect another thread's lookup."""
    resolver = _Resolver({"lan.example": [["10.1.2.3"]]})
    other_result = {}

    def worker():
        # No window active on this thread -> resolves normally.
        other_result["ip"] = socket.getaddrinfo("lan.example", 80)[0][4][0]

    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.block_private_resolution(allow_private_ips=False):
            t = threading.Thread(target=worker)
            t.start()
            t.join()
    assert other_result["ip"] == "10.1.2.3"


# ==========================================================================
# dns_pinning primitives: pin_hosts
# ==========================================================================
def test_pin_hosts_pins_only_pinnable_schemes():
    """json:// (raw webhook) is resolved+pinned; discord:// (token host) is
    left untouched — pinning must never try to resolve an opaque token."""
    resolver = _Resolver({"hook.example": [["93.184.216.34"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.pin_hosts(
            ["json://hook.example/x", "discord://webhook_id/token"],
            allow_private_ips=True,
        ):
            # hook.example is pinned; a lookup returns the pinned address
            # without consulting the resolver again.
            calls_before = len(resolver.calls)
            got = socket.getaddrinfo("hook.example", 443)
            assert got[0][4][0] == "93.184.216.34"
            assert len(resolver.calls) == calls_before  # served from pin
    # discord's token was never resolved.
    assert not any(h == "webhook_id" for h, _ in resolver.calls)


def test_pin_hosts_rejects_rebind_to_metadata():
    """A pinnable host that validates public but resolves to metadata at pin
    time is refused (fail closed) before the window is active."""
    resolver = _Resolver({"hook.example": [["169.254.169.254"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with pytest.raises(ValueError, match="SSRF"):
            with dns_pinning.pin_hosts(
                ["json://hook.example/x"], allow_private_ips=True
            ):
                pass


def test_pin_hosts_skips_unresolvable_host_without_aborting():
    """A host that doesn't resolve right now is skipped (left unpinned), not
    fatal — so a sibling URL in the same batch is unaffected."""
    resolver = _Resolver({"good.example": [["93.184.216.34"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.pin_hosts(
            ["json://dead.example/x", "json://good.example/y"],
            allow_private_ips=True,
        ):
            assert (
                socket.getaddrinfo("good.example", 443)[0][4][0]
                == "93.184.216.34"
            )


# ==========================================================================
# End-to-end through the real Apprise + requests stack (proves the shim
# intercepts Apprise's own resolution)
# ==========================================================================
def test_public_json_webhook_delivers_through_apprise():
    """A legitimate raw webhook is delivered end-to-end: Apprise resolves the
    host through the shim, connects to the pinned address, and the loopback
    server receives the POST."""
    server, port, hits = _start_server()
    resolver = _Resolver({"good.example": [["127.0.0.1"]]})
    try:
        with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
            result = _service(allow_private_ips=True).send(
                title="t",
                body="b",
                service_urls=f"json://good.example:{port}/notify",
            )
        assert result is True
        assert hits, "loopback webhook server never received the request"
    finally:
        server.shutdown()


def test_rebind_to_metadata_blocked_through_apprise():
    """Validation sees a public IP; the pin's connect-time re-validation
    catches the rebind to cloud-metadata and refuses the send before any
    connection is made. The pin's rebind catch raises a non-retryable
    ValueError, which ``send()`` surfaces as ``SecurityBlockError`` — a
    ``ServiceError`` subclass reserved for a CONFIRMED send-time block (as
    opposed to plain ``ServiceError``, raised for pre-dispatch validation
    rejects) — see ``NotificationService.send``'s docstring. Asserting the
    subclass here (not the broader ``ServiceError``) is what pins this test
    to the send-time rebind path rather than any pre-dispatch reject."""
    resolver = _Resolver(
        {"rebind.example": [["93.184.216.34"], ["169.254.169.254"]]}
    )
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with _connect_spy() as targets:
            with pytest.raises(SecurityBlockError):
                _service().send(
                    title="t",
                    body="b",
                    service_urls="json://rebind.example/notify",
                )
    assert not any(addr[0] == "169.254.169.254" for addr in targets), (
        f"connected to metadata IP: {targets}"
    )


def test_raw_plugin_private_rebind_refused_without_operator_opt_in():
    """A public validation answer cannot become a loopback send target."""
    resolver = _Resolver({"rebind.example": [["93.184.216.34"], ["127.0.0.1"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with _connect_spy() as targets:
            with pytest.raises(SecurityBlockError):
                _service().send(
                    title="t",
                    body="b",
                    service_urls="json://rebind.example/notify",
                )
    assert not any(addr[0] == "127.0.0.1" for addr in targets)


def test_multi_url_batch_guards_every_host():
    """A single notify() fans out to several raw-webhook URLs. EVERY host in
    the batch is pinned/checked: one host that rebinds to metadata refuses
    the whole batch (fail closed) and no connection to the metadata IP is
    made — proving the guard is not applied to just the first URL. The
    confirmed rebind raises ``SecurityBlockError`` (non-retryable, a
    ``ServiceError`` subclass) — asserting the subclass pins this to the
    send-time block path, not any pre-dispatch reject."""
    server, port, _hits = _start_server()
    resolver = _Resolver(
        {
            "a.example": [["127.0.0.1"]],
            "b.example": [["93.184.216.34"], ["169.254.169.254"]],
        }
    )
    urls = f"json://a.example:{port}/x,json://b.example/y"
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with _connect_spy() as targets:
            with pytest.raises(SecurityBlockError):
                _service(allow_private_ips=True).send(
                    title="t", body="b", service_urls=urls
                )
    try:
        assert not any(addr[0] == "169.254.169.254" for addr in targets)
    finally:
        server.shutdown()


def test_redirect_to_metadata_blocked_at_socket_layer():
    """A public webhook host that 302-redirects to a cloud-metadata IP:
    defense-in-depth. Redirect-following is disabled for the send
    (``http_redirects=False`` + per-plugin re-force), so Apprise does NOT
    follow the 302 at all — the initial request to the (pinned) public host
    happens, the send fails on the unfollowed 302, and no connection to the
    metadata IP is ever made. Even if the redirect WERE followed, the
    block-private window would refuse the ``169.254.169.254`` resolution at
    the getaddrinfo layer (the mock resolver now resolves that literal to
    itself, so that block genuinely fires — see
    ``test_block_private_refuses_metadata_at_socket_layer`` for the isolated
    teeth). Two independent layers; this asserts the end-to-end outcome."""
    server, port, hits = _start_server(
        redirect_to="http://169.254.169.254/latest/meta-data/"
    )
    resolver = _Resolver({"public.example": [["127.0.0.1"]]})
    try:
        with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
            with _connect_spy() as targets:
                with pytest.raises(SendError):
                    _service(allow_private_ips=True).send(
                        title="t",
                        body="b",
                        service_urls=f"json://public.example:{port}/hook",
                    )
        assert hits, "initial request to the public host never happened"
        assert not any(addr[0] == "169.254.169.254" for addr in targets), (
            f"connected to metadata IP: {targets}"
        )
    finally:
        server.shutdown()


def test_redirect_following_disabled_by_default():
    """Fix B, isolated from the block-private backstop: a webhook that
    302-redirects to a target the send window does NOT independently block
    (``redir-target.example`` -> loopback, which the lenient plugin policy
    allows) is still never followed, because redirect-following is disabled
    for the send. So ONLY the redirect-disable stops the second hop — this
    gives fix B clean teeth (deleting the redirect-disable makes the target
    server receive the request and this test fail). Also closes
    redirect-to-arbitrary-public exfil, of which the loopback target is a
    stand-in."""
    target_server, target_port, target_hits = _start_server()
    hook_server, hook_port, hook_hits = _start_server(
        redirect_to=f"http://redir-target.example:{target_port}/pwn"
    )
    resolver = _Resolver(
        {
            "public.example": [["127.0.0.1"]],
            "redir-target.example": [["127.0.0.1"]],
        }
    )
    try:
        with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
            with _connect_spy() as targets:
                with pytest.raises(SendError):
                    _service(allow_private_ips=True).send(
                        title="t",
                        body="b",
                        service_urls=f"json://public.example:{hook_port}/hook",
                    )
        assert hook_hits, "initial request to the webhook host never happened"
        assert not target_hits, (
            "redirect was FOLLOWED to the target server "
            f"(hits={target_hits}) — redirect-following was not disabled"
        )
        assert not any(addr[1] == target_port for addr in targets), (
            f"connected to the redirect target port {target_port}: {targets}"
        )
    finally:
        hook_server.shutdown()
        target_server.shutdown()


def test_redirect_param_is_rejected_by_url_validation():
    """OUTER layer: a user-supplied ``?redirect=yes`` never reaches Apprise.

    ``NotificationURLValidator`` rejects the ``redirect`` query key outright
    (it is one of ``BLOCKED_APPRISE_QUERY_KEYS``), so ``send()`` raises before
    ``Apprise.add()`` and no request is made at all. The INNER backstop — the
    per-plugin redirect re-force that would neutralize the option had it got
    through — is exercised separately below.
    """
    hook_server, hook_port, hook_hits = _start_server(
        redirect_to="http://redir-target.example:1/pwn"
    )
    try:
        with pytest.raises(ServiceError, match="redirect"):
            _service().send(
                title="t",
                body="b",
                service_urls=(
                    f"json://public.example:{hook_port}/hook?redirect=yes"
                ),
            )
        assert not hook_hits, (
            "the rejected URL still reached the webhook host; validation "
            "must fail before Apprise.add()"
        )
    finally:
        hook_server.shutdown()


def test_redirect_param_yes_cannot_reenable_redirects():
    """INNER backstop: even if a ``?redirect=yes`` URL somehow reached
    Apprise, the per-plugin re-force (``NotificationService._disable_redirects``)
    keeps redirect-following off.

    Via Apprise's per-URL override, ``?redirect=yes`` would normally re-enable
    redirect-following even when the asset default is off. URL validation now
    rejects that key up-front (see the test above), so this test deliberately
    stubs out the outer validation layer to isolate the inner one — deleting
    the re-force makes the redirect follow through to the target server and
    this test fail. Both layers are kept: the validator is the primary
    defence, and the re-force still covers any future path that constructs an
    Apprise client from an unvalidated URL.
    """
    target_server, target_port, target_hits = _start_server()
    hook_server, hook_port, hook_hits = _start_server(
        redirect_to=f"http://redir-target.example:{target_port}/pwn"
    )
    resolver = _Resolver(
        {
            "public.example": [["127.0.0.1"]],
            "redir-target.example": [["127.0.0.1"]],
        }
    )
    try:
        with patch.object(
            NotificationURLValidator,
            "validate_multiple_urls",
            staticmethod(lambda *args, **kwargs: (True, None)),
        ):
            with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
                with _connect_spy() as targets:
                    with pytest.raises(SendError):
                        _service(allow_private_ips=True).send(
                            title="t",
                            body="b",
                            service_urls=(
                                f"json://public.example:{hook_port}"
                                "/hook?redirect=yes"
                            ),
                        )
        assert hook_hits, "initial request to the webhook host never happened"
        assert not target_hits, (
            "?redirect=yes re-enabled redirect-following "
            f"(target hits={target_hits}) — the per-plugin re-force failed"
        )
        assert not any(addr[1] == target_port for addr in targets), (
            f"connected to the redirect target port {target_port}: {targets}"
        )
    finally:
        hook_server.shutdown()
        target_server.shutdown()


def test_redirect_to_loopback_blocked_for_http_scheme_default_policy():
    """The strict (http/https) policy blocks a redirect to loopback even
    though the plugin policy would allow it. Exercised at the primitive level
    because Apprise does not accept raw http(s):// notification URLs."""
    resolver = _Resolver({"redir.example": [["127.0.0.1"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.pinned_notification_send(
            ["http://public.example/hook"], allow_private_ips=False
        ):
            with pytest.raises(socket.gaierror):
                socket.getaddrinfo("redir.example", 80)


def test_discord_plugin_scheme_not_pinned_and_unaffected():
    """A discord:// URL (hardcoded public endpoint, opaque token host) is a
    plugin scheme, not a raw webhook: pinning must not try to resolve its
    token as a hostname, and the send still reaches Apprise's discord plugin
    with the guard in place."""
    # Empty resolver: any hostname lookup (e.g. the validator's fail-open
    # probe of the token) returns gaierror rather than hitting the network.
    resolver = _Resolver({})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with patch(
            "apprise.plugins.discord.NotifyDiscord.send", return_value=True
        ) as mock_send:
            result = _service().send(
                title="t",
                body="b",
                service_urls="discord://123456789/abcdefg",
            )
    # Delivered (guard did not break the plugin) and the plugin actually ran.
    assert result is True
    assert mock_send.called


# ==========================================================================
# Fail-closed hardening invariants
# ==========================================================================
def test_pinned_notification_send_fails_closed_when_shim_missing():
    """If ``socket.getaddrinfo`` is no longer our shim (e.g. a late
    gevent/eventlet monkeypatch), the thread-local pin/block would silently
    not apply. pinned_notification_send must REFUSE the send (fail closed)
    rather than proceed unguarded.

    Asserts the SPECIFIC ``NotificationGuardUnavailableError`` type (not a
    bare ``RuntimeError``, which it subclasses) — see
    ``test_send_fails_closed_through_send_when_shim_missing`` below for why
    that distinction matters: a bare ``RuntimeError`` here would still pass
    if ``dns_pinning`` reverted to ``raise RuntimeError(...)``, silently
    losing the dedicated type ``notifications.service`` relies on to route
    this into ``SecurityBlockError`` instead of the generic
    ``except Exception`` catch-all. Teeth: drop the ``_shim_installed``
    guard and this raises nothing."""
    saved = socket.getaddrinfo
    resolver = _Resolver({})  # empty: any real lookup would gaierror
    try:
        with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
            # Displace our shim with a plain non-shim resolver.
            socket.getaddrinfo = resolver
            assert not dns_pinning._shim_installed()
            with pytest.raises(
                dns_pinning.NotificationGuardUnavailableError, match="shim"
            ):
                with dns_pinning.pinned_notification_send(
                    ["json://x.example/y"], allow_private_ips=True
                ):
                    pass
    finally:
        socket.getaddrinfo = saved
    # Shim restored for the rest of the suite.
    assert dns_pinning._shim_installed()


def test_send_fails_closed_through_send_when_shim_missing():
    """End-to-end: when the DNS-pin shim is not installed,
    ``NotificationService.send()`` must surface this as
    ``SecurityBlockError`` (a ``ServiceError`` subclass, mapped by
    ``NotificationManager`` to the non-retryable ``invalid_url`` reason) —
    NOT let it fall through to the generic ``except Exception`` branch
    (which ``NotificationManager`` maps to the ``exception`` reason,
    silently losing the fact that this was a deliberate fail-closed
    security refusal, not an incidental bug).

    Teeth: if ``dns_pinning.pinned_notification_send`` reverted to raising
    a bare ``RuntimeError`` instead of the dedicated
    ``NotificationGuardUnavailableError`` subclass, ``send()``'s
    ``except dns_pinning.NotificationGuardUnavailableError`` clause would
    no longer match, this test's ``pytest.raises(SecurityBlockError)``
    would fail, and the bare ``RuntimeError`` would propagate through the
    generic ``except Exception`` branch instead.

    ``guard-missing.example`` resolves to a public IP so PRE-DISPATCH URL
    validation (which itself calls ``socket.getaddrinfo``, and would
    otherwise fail with an unrelated "could not resolve host" ServiceError
    before the guard is even reached) succeeds; the guard-unavailable
    refusal then happens purely because the shim is displaced, not because
    the host is unresolvable."""
    saved = socket.getaddrinfo
    resolver = _Resolver({"guard-missing.example": [["93.184.216.34"]]})
    try:
        with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
            # Displace our shim with a plain non-shim resolver, exactly as
            # test_pinned_notification_send_fails_closed_when_shim_missing
            # does above.
            socket.getaddrinfo = resolver
            assert not dns_pinning._shim_installed()
            with pytest.raises(SecurityBlockError):
                _service().send(
                    title="t",
                    body="b",
                    service_urls="json://guard-missing.example/y",
                )
    finally:
        socket.getaddrinfo = saved
    assert dns_pinning._shim_installed()


def test_resolver_unicode_error_is_connection_error_not_security_block():
    """A resolver failure that is a ``UnicodeError`` — what
    ``socket.getaddrinfo`` raises for a host whose IDNA encoding fails
    (a label over 63 bytes or an empty label; ``UnicodeEncodeError`` is
    a ``ValueError`` subclass) — must leave ``_resolve_and_validate``
    as a ``requests.ConnectionError``, an ordinary transport failure,
    and never as a bare ``ValueError``:
    ``NotificationService.send()``'s ``except ValueError`` clause
    treats that as a CONFIRMED send-time SSRF block and re-raises it as
    ``SecurityBlockError``, which would mislabel a resolution failure
    as a security block (and map it to the non-retryable ``invalid_url``
    instead of a retryable transport failure).

    Teeth: revert ``except (socket.gaierror, UnicodeError)`` to
    ``except socket.gaierror`` in ``dns_pinning._resolve_and_validate``
    and the ``UnicodeEncodeError`` escapes this call uncaught, so
    ``pytest.raises(requests.ConnectionError)`` fails.
    """

    def _idna_broken_resolver(host, port, family=0, type=0, proto=0, flags=0):
        raise UnicodeEncodeError(
            "idna", host, 0, len(host), "label too long or empty"
        )

    with patch.object(dns_pinning, "_real_getaddrinfo", _idna_broken_resolver):
        with pytest.raises(requests.ConnectionError):
            dns_pinning._resolve_and_validate(
                "unicode-fail.example",
                allow_localhost=False,
                allow_private_ips=False,
            )


def test_send_time_unicode_error_is_gaierror_not_security_block():
    """The SAME invariant, one function away: an IDNA-unencodable host
    that reaches ``_resolve_maybe_block`` (i.e. resolution INSIDE an
    active block-private window) must be refused as ``socket.gaierror``,
    never as a bare ``ValueError``.

    ``socket.getaddrinfo`` raises ``UnicodeError`` — a ``ValueError``
    subclass — for a label over 63 bytes or an empty label. At this layer
    a ``ValueError`` reaching the sender means a CONFIRMED SSRF block, so
    letting it escape mislabels an ordinary resolution failure as
    ``SecurityBlockError`` — exactly the defect
    ``_resolve_and_validate``'s ``except (socket.gaierror, UnicodeError)``
    fixed at pin time.

    This path is genuinely reachable, not hypothetical: ``pin_hosts``
    SKIPS a host whose pin-time resolution raised
    ``requests.ConnectionError`` — which, since that fix, INCLUDES this
    UnicodeError case — and leaves it unpinned, so its send-time lookup
    lands here.

    Teeth: remove the ``except UnicodeError`` clause around
    ``_getaddrinfo_bounded`` in ``dns_pinning._resolve_maybe_block`` and
    the ``UnicodeEncodeError`` escapes uncaught, so
    ``pytest.raises(socket.gaierror)`` fails (it is a ``ValueError``, not
    an ``OSError``).
    """

    def _idna_broken_resolver(host, port, family=0, type=0, proto=0, flags=0):
        raise UnicodeEncodeError(
            "idna", str(host), 0, len(str(host)), "label too long or empty"
        )

    # Clear any marker a sibling test left on this thread so the
    # "not reported as a confirmed block" assertion below is meaningful.
    dns_pinning.reset_ssrf_block()

    with patch.object(dns_pinning, "_real_getaddrinfo", _idna_broken_resolver):
        with dns_pinning.block_private_resolution(
            allow_localhost=False,
            allow_private_ips=False,
            block_link_local=True,
        ):
            with pytest.raises(socket.gaierror) as excinfo:
                dns_pinning._resolve_maybe_block(
                    "unicode-fail.example",
                    None,
                    socket.AF_UNSPEC,
                    socket.SOCK_STREAM,
                    0,
                    0,
                )

    # A gaierror is an OSError, so it can never be caught by the
    # ``except ValueError`` clause that reports a confirmed SSRF block.
    assert not isinstance(excinfo.value, ValueError)
    # And this is NOT reported as a confirmed block, so the send is not
    # forced onto the fail-fast security path.
    assert dns_pinning.consume_ssrf_block() is False


def test_pin_hosts_skips_idna_unencodable_host_without_aborting():
    """``pin_hosts`` must not abort the batch for an IDNA-unencodable
    host: ``_resolve_and_validate`` routes it to
    ``requests.ConnectionError``, which the loop skips. Pins the handoff
    the test above depends on — the host is left UNPINNED, so the
    block-private window governs its send-time lookup.

    Teeth: revert this PR's delta — ``_resolve_and_validate``'s
    ``except (socket.gaierror, UnicodeError)`` back to ``except
    socket.gaierror`` — and the resolver's ``UnicodeEncodeError`` is never
    converted to ``requests.ConnectionError``, so ``pin_hosts``' handler
    does not catch it: the context manager raises instead of yielding and
    the body below never runs.
    """

    calls = []

    def _idna_broken_resolver(host, port, family=0, type=0, proto=0, flags=0):
        calls.append(host)
        raise UnicodeEncodeError(
            "idna", str(host), 0, len(str(host)), "label too long or empty"
        )

    with patch.object(dns_pinning, "_real_getaddrinfo", _idna_broken_resolver):
        # ``json`` is a PINNABLE scheme, so the loop really does try to
        # resolve this host — a non-pinnable scheme (``ntfy``) would be
        # skipped before resolution and make this test vacuous.
        with dns_pinning.pin_hosts(
            ["json://unicode-fail.example/x"],
            allow_localhost=False,
            allow_private_ips=False,
        ):
            assert calls, "pin_hosts never attempted to resolve the host"
            assert dns_pinning._get_pins().get("unicode-fail.example") is None


def _fixed_resolver(ip):
    """A resolver that answers EVERY host (str or bytes) with ``ip``."""

    def resolver(host, port, family=0, type=0, proto=0, flags=0):
        return [
            (
                socket.AF_INET,
                socket.SOCK_STREAM,
                socket.IPPROTO_TCP,
                "",
                (ip, port or 0),
            )
        ]

    return resolver


def test_block_window_refuses_bytes_host_resolving_to_metadata():
    """``getaddrinfo`` accepts a ``bytes`` host, so an attacker/plugin can
    hand the shim ``b"..."`` that resolves to cloud metadata. Under an active
    block-private window a bytes host must be block-checked and REFUSED on the
    resolved IP exactly like a ``str`` host — not returned unchecked (the one
    guard that used to fail OPEN).

    Teeth: restore the old ``if not isinstance(host, str): return results``
    early-return in ``_resolve_maybe_block`` and the bytes host resolves to
    the metadata IP and is returned unchecked — no gaierror — so this test
    fails.
    """
    dns_pinning.reset_ssrf_block()
    with patch.object(
        dns_pinning, "_real_getaddrinfo", _fixed_resolver("169.254.169.254")
    ):
        with dns_pinning.block_private_resolution(
            allow_private_ips=True, block_link_local=True
        ):
            with pytest.raises(socket.gaierror):
                dns_pinning._pinned_getaddrinfo(b"metadata.example", 80)
    # A CONFIRMED security block (not a transient error) so the sender fails
    # fast instead of retrying.
    assert dns_pinning.consume_ssrf_block()


def test_block_window_allows_bytes_host_resolving_to_public_ip():
    """The bytes-host fix fails CLOSED without over-blocking: a bytes host
    that resolves to a public address is still returned (the block check runs
    on the resolved IP, which is not blocked), so a legitimate bytes host is
    never refused just for being bytes."""
    dns_pinning.reset_ssrf_block()
    with patch.object(
        dns_pinning, "_real_getaddrinfo", _fixed_resolver("93.184.216.34")
    ):
        with dns_pinning.block_private_resolution(
            allow_private_ips=True, block_link_local=True
        ):
            got = dns_pinning._pinned_getaddrinfo(b"public.example", 443)
    assert got[0][4][0] == "93.184.216.34"
    assert not dns_pinning.consume_ssrf_block()


def test_block_window_exempts_none_host_passive_bind():
    """A ``None`` host is an AF_PASSIVE bind
    (``getaddrinfo(None, port, ..., AI_PASSIVE)``) — a local listening-socket
    setup, not a rebindable outbound name — so it is exempt from the block
    check even while a window is active (it must not raise)."""
    dns_pinning.reset_ssrf_block()
    with patch.object(
        dns_pinning, "_real_getaddrinfo", _fixed_resolver("169.254.169.254")
    ):
        with dns_pinning.block_private_resolution(
            allow_private_ips=True, block_link_local=True
        ):
            # No raise even though the resolver answers with a metadata IP:
            # a None (passive-bind) host is not an outbound destination.
            got = dns_pinning._pinned_getaddrinfo(
                None,
                8080,
                socket.AF_INET,
                socket.SOCK_STREAM,
                0,
                socket.AI_PASSIVE,
            )
    assert got  # returned unchecked
    assert not dns_pinning.consume_ssrf_block()


def test_send_time_resolution_timeout_fails_closed(monkeypatch):
    """A slow/hostile DNS authority must not hang the sending thread: the
    block-window resolution is bounded and fails closed (gaierror) well
    before the resolver would have returned. Teeth: remove the timeout
    wrapping and this call blocks for the full sleep instead."""
    monkeypatch.setattr(dns_pinning, "_RESOLVE_TIMEOUT_SECONDS", 0.2)

    def slow(host, *a, **k):
        # Intentional "hang" the 0.2s bound must interrupt; the test itself
        # returns in ~0.2s (the sleep only lingers in the abandoned worker).
        time.sleep(1.5)  # allow: unmarked-sleep
        return [
            (
                socket.AF_INET,
                socket.SOCK_STREAM,
                socket.IPPROTO_TCP,
                "",
                ("93.184.216.34", 0),
            )
        ]

    with patch.object(dns_pinning, "_real_getaddrinfo", slow):
        with dns_pinning.block_private_resolution(allow_private_ips=True):
            start = time.monotonic()
            with pytest.raises(socket.gaierror):
                socket.getaddrinfo("slow.example", 80)
            elapsed = time.monotonic() - start
    assert elapsed < 1.0, (
        f"resolution was not bounded (took {elapsed:.2f}s; timeout was 0.2s)"
    )


def test_guarded_send_refuses_async_mode_and_tag():
    """The guarded-send invariants fail closed: async_mode must be off and
    tag must be None, or the thread-local pin/block would be bypassed by
    Apprise's worker-thread fan-out. Teeth: drop the checks in
    ``_enforce_guarded_send_invariants`` and neither raises.

    Both violations raise ``SecurityBlockError`` directly (not a generic
    ``RuntimeError``) so ``send()`` cannot mislabel this deliberate
    security refusal as a retryable webhook failure — see the
    round-3 fix for #5113."""
    svc = _service()
    # Correct config: synchronous, no tag -> accepted.
    good = svc._new_apprise()
    svc._enforce_guarded_send_invariants(good, None)
    # async_mode=True -> refused.
    bad_async = apprise.Apprise(asset=apprise.AppriseAsset(async_mode=True))
    with pytest.raises(SecurityBlockError, match="async_mode"):
        svc._enforce_guarded_send_invariants(bad_async, None)
    # tag present -> refused.
    with pytest.raises(SecurityBlockError, match="tag"):
        svc._enforce_guarded_send_invariants(good, "urgent")


def test_ssrf_block_is_not_retried():
    """A confirmed SSRF block (pin's connect-time rebind-to-metadata) must
    fail fast, not be retried 3x by Tenacity. The rebind host resolves
    public at validation (call #1) and metadata at the guarded pin (call
    #2), which raises the SSRF ValueError; with that excluded from retries
    the guarded pin runs exactly ONCE. ``send()``'s ``except ValueError``
    maps that confirmed, non-retryable block to ``SecurityBlockError`` —
    asserted specifically here rather than as its ``ServiceError`` base,
    so a regression that reports the block as a plain ``ServiceError``
    (or lets ``SendError``'s retryable ``WEBHOOK_FAILED`` reason claim it)
    fails the test. Teeth: restore
    ``retry_if_exception_type((Exception,))`` and the pin is retried, so
    rebind.example is resolved 4 times (1 validation + 3 pin attempts)."""
    resolver = _Resolver(
        {"rebind.example": [["93.184.216.34"], ["169.254.169.254"]]}
    )
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with pytest.raises(SecurityBlockError):
            _service().send(
                title="t",
                body="b",
                service_urls="json://rebind.example/notify",
            )
    rebind_calls = [
        c
        for c in resolver.calls
        if (c[0] or "").lower().rstrip(".") == "rebind.example"
    ]
    assert len(rebind_calls) == 2, (
        f"expected 1 validation + 1 (non-retried) pin resolution, got "
        f"{len(rebind_calls)}: {rebind_calls}"
    )


# ==========================================================================
# TLS: SNI + cert verification use the hostname, not the pinned IP
# ==========================================================================
def test_tls_sni_and_cert_use_hostname_while_connecting_to_pinned_ip(
    tmp_path,
):
    """With the host pinned to the loopback server, an HTTPS request to the
    hostname still completes certificate verification against that
    hostname's cert. If the pin had leaked the IP into SNI / cert
    validation, verification would fail — so success proves only name
    resolution is redirected.

    Teeth: the hostname resolves to a NON-loopback, unreachable TEST-NET-3
    address (203.0.113.9) by DEFAULT; ONLY the pin redirects the connection
    to 127.0.0.1 where the cert is actually served. The pinned address is
    injected directly (what ``pin_hosts`` installs after validating) so it
    is independent of the resolver's default answer — that independence is
    what gives the test teeth: drop the pin and the request targets
    203.0.113.9 and never reaches the loopback server, so the assertions
    fail. The ``_connect_spy`` below pins that down: the socket connects to
    127.0.0.1 (the pin), never to 203.0.113.9 (the default resolution)."""
    hostname = "pinned-tls.example"
    cert_file, key_file = _make_self_signed_cert(hostname, tmp_path)
    server, port, _hits = _start_server(tls_cert=(cert_file, key_file))
    # Default resolution is a non-loopback, unreachable address.
    resolver = _Resolver({hostname: [["203.0.113.9"]]})
    # The pin points at the loopback server. Port 0 is rewritten to the
    # lookup's port by the shim (matching how _resolve_and_validate pins).
    loopback_pin = [
        (
            socket.AF_INET,
            socket.SOCK_STREAM,
            socket.IPPROTO_TCP,
            "",
            ("127.0.0.1", 0),
        )
    ]
    try:
        with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
            pins = dns_pinning._get_pins()
            pins[hostname] = loopback_pin
            try:
                with _connect_spy() as targets:
                    resp = requests.get(
                        f"https://{hostname}:{port}/",
                        verify=cert_file,
                        timeout=5,
                    )
            finally:
                pins.pop(hostname, None)
        assert resp.status_code == 200
        # Connected to the PINNED loopback IP, never the default non-loopback
        # resolution — proving the pin (not the resolver) chose the address.
        assert any(addr[0] == "127.0.0.1" for addr in targets), targets
        assert not any(addr[0] == "203.0.113.9" for addr in targets), targets
    finally:
        server.shutdown()


# ==========================================================================
# P1.3: a block-window block fails fast (not retried 3x)
# ==========================================================================
def test_block_window_block_is_not_retried():
    """A confirmed SSRF block from the BLOCK-PRIVATE WINDOW (an UNPINNED
    plugin-scheme host that rebinds to metadata at send time) must fail fast,
    not be retried 3x by Tenacity. ntfy:// is a plugin scheme (not pinned),
    so the pin's own connect-time ValueError does not apply — the block is a
    socket.gaierror that Apprise swallows into a generic failure. The service
    consults ``dns_pinning.consume_ssrf_block()`` after the failed send and
    re-raises it non-retryably. The rebind host resolves public at validation
    (call #1) and metadata at the send-time window resolution (call #2), so
    with the block excluded from retries the host is resolved exactly TWICE.
    ``send()``'s ``except ValueError`` maps the confirmed, non-retryable
    block to ``SecurityBlockError`` — asserted specifically, not as its
    ``ServiceError`` base, so a regression that reports it as a plain
    ``ServiceError`` (or as a retryable ``SendError``) fails the test.
    Teeth: drop the ``consume_ssrf_block`` short-circuit in
    ``_send_with_retry`` and the send is retried, resolving the host 4 times
    (1 validation + 3 send attempts)."""
    resolver = _Resolver(
        {"rebind.example": [["93.184.216.34"], ["169.254.169.254"]]}
    )
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with _connect_spy() as targets:
            with pytest.raises(SecurityBlockError):
                _service().send(
                    title="t",
                    body="b",
                    service_urls="ntfy://rebind.example/mytopic",
                )
    rebind_calls = [
        c
        for c in resolver.calls
        if (c[0] or "").lower().rstrip(".") == "rebind.example"
    ]
    assert len(rebind_calls) == 2, (
        f"expected 1 validation + 1 (non-retried) send resolution, got "
        f"{len(rebind_calls)}: {rebind_calls}"
    )
    assert not any(addr[0] == "169.254.169.254" for addr in targets), targets


# ==========================================================================
# P0.1: AWS IPv6 IMDS (fd00:ec2::254) blocked through the pin/block path
# ==========================================================================
def test_ipv6_imds_rejected_by_pin_at_connect_time():
    """A pinnable raw-webhook host that resolves to AWS's IPv6 IMDS
    (fd00:ec2::254) at pin time is refused (fail closed) even under
    allow_private_ips=True — which permits the fc00::/7 ULA range the address
    sits in. The cloud-metadata block is absolute."""
    resolver = _Resolver({"hook.example": [["fd00:ec2::254"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with pytest.raises(ValueError, match="SSRF"):
            with dns_pinning.pin_hosts(
                ["json://hook.example/x"], allow_private_ips=True
            ):
                pass


def test_ipv6_imds_refused_by_block_window():
    """An UNPINNED lookup that resolves to AWS's IPv6 IMDS is refused at the
    getaddrinfo layer while the block-private window is active, even with
    allow_private_ips=True."""
    resolver = _Resolver({"redir.example": [["fd00:ec2::254"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.block_private_resolution(allow_private_ips=True):
            with pytest.raises(socket.gaierror):
                socket.getaddrinfo("redir.example", 80)


# ==========================================================================
# P2.10: additional coverage
# ==========================================================================
def test_send_with_tag_is_refused_end_to_end():
    """send(..., tag=...) is refused end-to-end: a tag can fan delivery out
    to worker threads that bypass the thread-local pin/block, so the
    guarded-send invariant rejects it before any connection — no socket is
    opened to the target.

    The invariant guard raises ``SecurityBlockError`` directly (not a
    generic ``RuntimeError``), and ``send()`` must propagate it unchanged
    rather than wrap it as ``SendError``: this is a deliberate security
    refusal, not a transient webhook failure, and NotificationManager maps
    ``SendError`` to the RETRYABLE ``webhook_failed`` reason — retrying a
    security refusal would be a bug. Round-3 fix for #5113 (the prior fix
    only re-routed the ValueError SSRF-block path, leaving this
    RuntimeError invariant-violation path mislabeled as webhook_failed)."""
    resolver = _Resolver({"good.example": [["93.184.216.34"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with _connect_spy() as targets:
            with pytest.raises(SecurityBlockError):
                _service().send(
                    title="t",
                    body="b",
                    service_urls="json://good.example/notify",
                    tag="a,b",
                )
    assert not any(addr[0] == "93.184.216.34" for addr in targets), targets


def test_rfc1918_host_pinned_and_allowed_by_notification_guard():
    """A raw-webhook (json) host that resolves to an RFC1918 address is
    PINNED (not blocked) by the lenient notification guard — self-hosted LAN
    webhooks keep working. The pinned lookup returns the validated RFC1918
    address without re-resolving."""
    resolver = _Resolver({"lan.example": [["10.11.12.13"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.pinned_notification_send(
            ["json://lan.example/hook"], allow_private_ips=True
        ):
            got = socket.getaddrinfo("lan.example", 443)
            assert got[0][4][0] == "10.11.12.13"


def test_selfhosted_private_plugin_delivers_end_to_end():
    """A self-hosted plugin notifier (ntfy / gotify) on a private host
    delivers end-to-end: the lenient partition allows private/LAN targets
    (cloud-metadata still always blocked). The loopback server stands in for
    the private endpoint; the guard pins/permits it and the plugin POSTs
    successfully through the real Apprise + requests stack."""
    server, port, hits = _start_server()
    resolver = _Resolver({"lan.example": [["127.0.0.1"]]})
    svc = NotificationService(allow_private_ips=True, outbound_allowed=True)
    try:
        for url in (
            f"ntfy://lan.example:{port}/topic",
            f"gotify://lan.example:{port}/atoken",
        ):
            hits.clear()
            with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
                result = svc.send(title="t", body="b", service_urls=url)
            assert result is True, url
            assert hits, f"self-hosted plugin never received the POST: {url}"
    finally:
        server.shutdown()


def test_non_json_plugin_redirect_not_followed():
    """redirect-disable applies to a NON-json plugin too (ntfy honors the
    per-plugin ``redirects`` flag). An ntfy endpoint that 302-redirects to
    another host is NOT followed: the target server is never hit and the send
    fails on the unfollowed 302. The redirect target is a loopback stand-in
    the lenient block-window does NOT itself block, so ONLY the
    redirect-disable stops the second hop (teeth)."""
    target_server, target_port, target_hits = _start_server()
    hook_server, hook_port, hook_hits = _start_server(
        redirect_to=f"http://redir-target.example:{target_port}/pwn"
    )
    resolver = _Resolver(
        {
            "ntfyhost.example": [["127.0.0.1"]],
            "redir-target.example": [["127.0.0.1"]],
        }
    )
    svc = NotificationService(allow_private_ips=True, outbound_allowed=True)
    try:
        with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
            with pytest.raises(SendError):
                svc.send(
                    title="t",
                    body="b",
                    service_urls=f"ntfy://ntfyhost.example:{hook_port}/topic",
                )
        assert hook_hits, "initial request to the ntfy host never happened"
        assert not target_hits, (
            f"ntfy redirect was FOLLOWED to the target (hits={target_hits}) "
            "— redirect-disable did not apply to the plugin"
        )
    finally:
        hook_server.shutdown()
        target_server.shutdown()


# ==========================================================================
# test_service (admin "Send Test Notification") is guarded like send()
# ==========================================================================
def test_test_service_rebind_to_metadata_blocked_through_apprise():
    """The admin "Send Test Notification" path (``test_service``) runs through
    the SAME guarded core (:meth:`NotificationService._guarded_notify`) as the
    real send, so a plugin-scheme host that validates public but rebinds to
    cloud-metadata at send time is refused: the block-private window raises a
    gaierror on the send-time resolution (Apprise swallows it), NO connection
    to the metadata IP is made, and ``test_service`` reports failure. The
    consume_ssrf_block cosmetic then surfaces the SPECIFIC SSRF-block reason to
    the admin instead of the generic "Failed to send" message.

    Teeth: pass ``guard_factory=None`` to ``_guarded_notify`` in
    ``test_service`` (drop the pin/block window) and Apprise resolves
    ``rebind.example`` to ``169.254.169.254`` and connects to it — the connect
    spy sees the metadata IP and this test fails. Dropping only the
    consume_ssrf_block branch makes the error revert to the generic message
    and the reason assertion fails."""
    resolver = _Resolver(
        {"rebind.example": [["93.184.216.34"], ["169.254.169.254"]]}
    )
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with _connect_spy() as targets:
            result = _service().test_service("ntfy://rebind.example/mytopic")
    assert result["success"] is False
    assert not any(addr[0] == "169.254.169.254" for addr in targets), (
        f"connected to metadata IP: {targets}"
    )
    # Cosmetic: the specific SSRF-block reason is surfaced (consume_ssrf_block),
    # not the generic delivery-failure message.
    assert "SSRF" in result["error"], (
        f"expected the specific SSRF-block reason, got: {result['error']!r}"
    )


def test_test_service_selfhosted_loopback_delivers():
    """Counterpart to the block test: ``test_service`` against a self-hosted
    loopback plugin endpoint still delivers end-to-end through the real
    Apprise + requests stack (the lenient partition permits private/loopback;
    only metadata / link-local are refused)."""
    server, port, hits = _start_server()
    resolver = _Resolver({"lan.example": [["127.0.0.1"]]})
    svc = NotificationService(allow_private_ips=True, outbound_allowed=True)
    try:
        with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
            result = svc.test_service(f"ntfy://lan.example:{port}/topic")
        assert result["success"] is True, result
        assert hits, "self-hosted loopback endpoint never received the POST"
    finally:
        server.shutdown()


# ==========================================================================
# IPv6 IMDS (fd00:ec2::254) refused end-to-end through send() (sockaddr path)
# ==========================================================================
def test_ipv6_imds_rebind_refused_through_send():
    """End-to-end IPv6 sockaddr path: a raw-webhook (json) host that validates
    to a public IPv6 but rebinds to AWS's IPv6 IMDS (``fd00:ec2::254``) at the
    guarded pin is refused before any connection — the IPv6 analogue of
    ``test_rebind_to_metadata_blocked_through_apprise``. The pin's connect-time
    re-validation catches the rebind (the cloud-metadata block is absolute even
    under allow_private_ips=True, which permits the fc00::/7 ULA the address
    sits in) and raises the SSRF ValueError, which ``send()`` surfaces as
    ``SecurityBlockError`` (confirmed, non-retryable send-time security
    block — a ``ServiceError`` subclass, not SendError). Asserting the
    subclass pins this to the send-time rebind path, not a pre-dispatch
    reject.

    Teeth: drop the ``is_ip_blocked`` check in
    ``dns_pinning._resolve_and_validate`` (or remove ``fd00:ec2::254`` from
    ``ALWAYS_BLOCKED_METADATA_IPS``) and the send connects to the IPv6 IMDS —
    the connect spy sees it and this test fails."""
    resolver = _Resolver(
        {
            "rebind6.example": [
                ["2606:2800:220:1:248:1893:25c8:1946"],  # public IPv6
                ["fd00:ec2::254"],  # AWS IPv6 IMDS
            ]
        }
    )
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with _connect_spy() as targets:
            with pytest.raises(SecurityBlockError):
                _service().send(
                    title="t",
                    body="b",
                    service_urls="json://rebind6.example/notify",
                )
    assert not any(str(addr[0]) == "fd00:ec2::254" for addr in targets), (
        f"connected to IPv6 IMDS: {targets}"
    )


# ==========================================================================
# Fix 1: link-local blocked for the lenient notification partition even under
# allow_private_ips=True (metadata beyond the always-blocked literals), while
# RFC1918 self-hosted notifiers keep working.
# ==========================================================================
def test_link_local_refused_by_notification_block_window():
    """An UNPINNED lookup that resolves into the link-local range (``169.254.10.5``,
    a generic link-local address — NOT one of the always-blocked
    literals) is refused by the notification block-private
    window when ``block_link_local=True``, even though allow_private_ips=True
    permits RFC1918. Teeth: drop ``block_link_local`` and this lookup
    resolves normally (link-local is otherwise allowed under the private-IP
    opt-in)."""
    resolver = _Resolver({"link-local.example": [["169.254.10.5"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.block_private_resolution(
            allow_private_ips=True, block_link_local=True
        ):
            with pytest.raises(socket.gaierror):
                socket.getaddrinfo("link-local.example", 80)


def test_ipv6_link_local_refused_by_notification_block_window():
    """IPv6 link-local (fe80::/10) is likewise refused under
    ``block_link_local=True`` even with allow_private_ips=True."""
    resolver = _Resolver({"ll6.example": [["fe80::1"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.block_private_resolution(
            allow_private_ips=True, block_link_local=True
        ):
            with pytest.raises(socket.gaierror):
                socket.getaddrinfo("ll6.example", 80)


def test_rfc1918_still_allowed_under_block_link_local():
    """``block_link_local=True`` must NOT over-block: RFC1918 / loopback /
    non-link-local ULA self-hosted notifiers still resolve under the lenient
    partition. Only link-local is refused."""
    resolver = _Resolver(
        {
            "lan.example": [["10.11.12.13"]],
            "lan2.example": [["192.168.1.5"]],
            "loop.example": [["127.0.0.1"]],
            "ula.example": [["fd00::1"]],
        }
    )
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with dns_pinning.block_private_resolution(
            allow_private_ips=True, block_link_local=True
        ):
            assert (
                socket.getaddrinfo("lan.example", 80)[0][4][0] == "10.11.12.13"
            )
            assert (
                socket.getaddrinfo("lan2.example", 80)[0][4][0] == "192.168.1.5"
            )
            assert (
                socket.getaddrinfo("loop.example", 80)[0][4][0] == "127.0.0.1"
            )
            assert socket.getaddrinfo("ula.example", 80)[0][4][0] == "fd00::1"


def test_link_local_pin_rejects_rebind():
    """A pinnable raw-webhook host that rebinds INTO link-local at pin time is
    refused (fail closed) under ``block_link_local=True`` — the connect-time
    re-validation catches metadata that lives in link-local beyond the
    always-blocked literals."""
    resolver = _Resolver({"hook.example": [["169.254.10.5"]]})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with pytest.raises(ValueError, match="SSRF"):
            with dns_pinning.pin_hosts(
                ["json://hook.example/x"],
                allow_private_ips=True,
                block_link_local=True,
            ):
                pass


def test_link_local_refused_end_to_end_through_send():
    """End-to-end: ``send()`` refuses a json:// webhook whose IP-literal host
    is a link-local address (``169.254.10.5``). The
    validator's plugin-scheme IMDS guard rejects it pre-send, so ``send``
    raises ServiceError and no connection is made to the link-local IP."""
    resolver = _Resolver({})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with _connect_spy() as targets:
            with pytest.raises(ServiceError):
                _service().send(
                    title="t",
                    body="b",
                    service_urls="json://169.254.10.5/notify",
                )
    assert not any(addr[0] == "169.254.10.5" for addr in targets), targets


def test_rfc1918_json_webhook_still_delivers_end_to_end():
    """The link-local block does not regress self-hosted delivery: a json://
    webhook on an RFC1918 host still delivers end-to-end through the lenient
    partition (loopback stands in for the private endpoint)."""
    server, port, hits = _start_server()
    resolver = _Resolver({"lan.example": [["127.0.0.1"]]})
    svc = NotificationService(allow_private_ips=True, outbound_allowed=True)
    try:
        with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
            result = svc.send(
                title="t",
                body="b",
                service_urls=f"json://lan.example:{port}/notify",
            )
        assert result is True
        assert hits, "RFC1918 self-hosted webhook never received the POST"
    finally:
        server.shutdown()


# ==========================================================================
# WIRING locks: block_link_local=True is threaded through the two
# notification send call sites themselves (service.py's ``_dispatch``
# lenient partition and ``test_service``'s guard factory) — not just
# present somewhere in the shared ``is_ip_blocked`` / validator machinery.
#
# Every test above that exercises link-local either drives the primitives
# directly (``dns_pinning.block_private_resolution`` /
# ``dns_pinning.pin_hosts``) or uses an IP-LITERAL URL
# (``test_link_local_refused_end_to_end_through_send``), which the
# validator's plugin-scheme IMDS guard rejects PRE-SEND — ``_dispatch`` /
# ``pinned_notification_send`` is never reached. So none of them would
# notice if ``block_link_local=True`` were dropped at either call site while
# the shared validator/``is_ip_blocked`` stayed intact.
#
# These tests use a HOSTNAME that validates PUBLIC (the validator's own
# resolution, call #1 of the scripted resolver) but rebinds to a link-local
# address that is NOT one of the always-blocked metadata literals (call
# #2, at the guarded send call site itself) — ``169.254.10.5``
# / a bare IPv6 ``fe80::`` address, both ordinary link-local space that
# ``allow_private_ips=True`` would otherwise ADMIT. So a refusal here can
# only come from the ``block_link_local=True`` threaded through the
# call site under test. The two ``_dispatch`` tests build the service with
# ``allow_private_ips=True`` so they have real teeth against that one wire;
# the ``test_service`` test below uses the default service and locks the
# refusal only (its flag is locked in test_notification_destination_guard.py).
# ==========================================================================
def test_link_local_rebind_refused_by_dispatch_lenient_partition():
    """WIRING lock for ``_dispatch``'s lenient-partition
    ``block_link_local=True`` (``notifications/service.py`` ~line 535).

    ``ll-rebind.example`` validates PUBLIC at ``send()``'s pre-send
    validation (resolver call #1) but rebinds to the link-local
    address ``169.254.10.5`` at ``_dispatch``'s ``pin_hosts``
    resolution (call #2, "pin time" — json:// is a pinnable scheme).
    ``169.254.10.5`` is ordinary link-local space, which the
    ``allow_private_ips=True`` opt-in (set on the service here) would
    otherwise admit; it is refused ONLY because ``_dispatch`` also passes
    ``block_link_local=True`` into that same ``pinned_notification_send``
    call. ``send()`` must refuse it and no connection may reach the
    link-local IP.

    Teeth: the service uses ``allow_private_ips=True`` so the address is
    NOT refused as private. Change ``block_link_local=True`` to ``False``
    at _dispatch's authority-partition ``pinned_notification_send`` call
    (leaving ``is_ip_blocked`` / the validator untouched) and this test
    fails — the rebind pins successfully and the connect spy sees
    169.254.10.5.

    The confirmed rebind raises ``SecurityBlockError`` (non-retryable, a
    ``ServiceError`` subclass), not SendError. Asserting the subclass (not
    the broader ``ServiceError``) pins this to the send-time rebind path
    exercised by ``_dispatch``, not any pre-dispatch reject.
    """
    resolver = _Resolver(
        {"ll-rebind.example": [["93.184.216.34"], ["169.254.10.5"]]}
    )
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with _connect_spy() as targets:
            with pytest.raises(SecurityBlockError):
                _service(allow_private_ips=True).send(
                    title="t",
                    body="b",
                    service_urls="json://ll-rebind.example/notify",
                )
    assert not any(addr[0] == "169.254.10.5" for addr in targets), (
        f"connected to link-local IP: {targets}"
    )


def test_link_local_rebind_refused_by_dispatch_lenient_partition_ipv6():
    """IPv6 analogue of the test above: ``ll-rebind6.example`` validates
    public IPv6 (resolver call #1) but rebinds to a bare IPv6 link-local
    address ``fe80::1`` (fe80::/10, not an always-blocked literal) at
    ``_dispatch``'s pin resolution (call #2). Same wiring lock —
    refused only by ``_dispatch``'s ``block_link_local=True`` (the
    service sets ``allow_private_ips=True``, so it is not refused as
    private). The
    confirmed rebind raises ``SecurityBlockError`` (non-retryable, a
    ``ServiceError`` subclass), not SendError. Asserting the subclass pins
    this to the send-time rebind path, not a pre-dispatch reject."""
    resolver = _Resolver(
        {
            "ll-rebind6.example": [
                ["2606:2800:220:1:248:1893:25c8:1946"],  # public IPv6
                ["fe80::1"],  # IPv6 link-local
            ]
        }
    )
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with _connect_spy() as targets:
            with pytest.raises(SecurityBlockError):
                _service(allow_private_ips=True).send(
                    title="t",
                    body="b",
                    service_urls="json://ll-rebind6.example/notify",
                )
    assert not any(str(addr[0]) == "fe80::1" for addr in targets), (
        f"connected to IPv6 link-local IP: {targets}"
    )


def test_test_service_link_local_rebind_refused_by_guard_factory():
    """WIRING lock for ``test_service``'s guard-factory
    ``block_link_local=True`` (``notifications/service.py``).

    ``ntfy://`` is a plugin scheme (not in ``_PINNABLE_SCHEMES``), so
    ``pin_hosts`` never resolves/pins it; the send-time lookup instead runs
    under the block-private WINDOW (``block_private_resolution``), which
    consults the SAME ``block_link_local`` flag ``test_service`` threads
    into its guard factory. The host validates public at ``test_service``'s
    pre-send validation (resolver call #1) and rebinds to the
    link-local ``169.254.10.5`` at send time (call #2), and the window
    refuses it.

    Teeth: with ``allow_private_ips=False`` (the service used here) the
    address is refused as private even without ``block_link_local``, so
    this test locks the refusal rather than that one flag;
    ``test_test_service_http_guard_blocks_link_local`` in
    ``test_notification_destination_guard.py`` locks the flag itself.
    """
    resolver = _Resolver(
        {"ll-rebind-ts.example": [["93.184.216.34"], ["169.254.10.5"]]}
    )
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with _connect_spy() as targets:
            result = _service().test_service(
                "ntfy://ll-rebind-ts.example/mytopic"
            )
    assert result["success"] is False
    assert not any(addr[0] == "169.254.10.5" for addr in targets), (
        f"connected to link-local IP: {targets}"
    )
    # Cosmetic corroboration that the block-private WINDOW (not some other
    # failure) is what fired.
    assert "SSRF" in result["error"], (
        f"expected the specific SSRF-block reason, got: {result['error']!r}"
    )


# ==========================================================================
# Fix 2: empty-authority notification URLs (host smuggled into the path) are
# rejected at the validator before send.
# ==========================================================================
def test_empty_authority_metadata_in_path_rejected_before_send():
    """``json:///169.254.169.254/path`` has an empty ``//`` authority:
    urllib3/requests see no host (so the validator's IP check would be
    skipped) but Apprise dials the in-path host ``169.254.169.254``. The
    empty-authority guard rejects it at validation, so ``send`` raises
    ServiceError and no connection to the metadata IP is made."""
    resolver = _Resolver({})
    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with _connect_spy() as targets:
            with pytest.raises(ServiceError):
                _service().send(
                    title="t",
                    body="b",
                    service_urls="json:///169.254.169.254/path",
                )
    assert not any(addr[0] == "169.254.169.254" for addr in targets), targets


# ==========================================================================
# Fixed vendor endpoints behind a private forward proxy. requests honours
# HTTPS_PROXY, so the only lookup in the sending thread is the proxy host,
# and the send-time guard checks it. The vendor partition follows the
# operator's private-IP opt-in: refused by default, allowed with the flag.
# ==========================================================================
@pytest.mark.parametrize("allow_private_ips", [False, True])
def test_vendor_send_through_private_proxy_follows_operator_opt_in(
    monkeypatch, allow_private_ips
):
    for name in ("NO_PROXY", "no_proxy", "ALL_PROXY", "all_proxy"):
        monkeypatch.delenv(name, raising=False)
    monkeypatch.setenv("HTTPS_PROXY", "http://proxy.lan:3128")
    monkeypatch.setenv("https_proxy", "http://proxy.lan:3128")
    resolver = _Resolver({"proxy.lan": [["10.0.0.8"]]})
    targets = []

    def refuse_connect(self, address):
        # Record the attempt but never open a real connection.
        targets.append(address)
        raise ConnectionRefusedError("test proxy is not listening")

    with patch.object(dns_pinning, "_real_getaddrinfo", resolver):
        with patch.object(socket.socket, "connect", refuse_connect):
            result = _service(allow_private_ips=allow_private_ips).test_service(
                "discord://123456789/abcdefghijklmnop"
            )

    assert result["success"] is False
    assert ("proxy.lan", 3128) in resolver.calls
    reached_proxy = any(addr[0] == "10.0.0.8" for addr in targets)
    assert reached_proxy is allow_private_ips, targets
    if not allow_private_ips:
        assert "SSRF" in result["error"], result["error"]