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.

51.8 KB
Raw Blame
"""
Core notification service using Apprise.
"""

import contextlib
import functools
import re
from typing import Dict, List, Optional, Any, Tuple
from urllib.parse import urlparse

import apprise
from loguru import logger
from tenacity import (
    retry,
    stop_after_attempt,
    wait_exponential,
    retry_if_not_exception_type,
)

from .exceptions import ServiceError, SendError, SecurityBlockError
from .apprise_log_utils import install_apprise_log_record_factory
from .templates import EventType, NotificationTemplate
from ..security import dns_pinning
from ..security.notification_destination import (
    NotificationDestinationError,
    NotificationDestinationKind,
    parse_notification_destination,
)
from ..security.notification_validator import (
    NotificationURLValidator,
    parse_notification_url_list,
)

PRIVATE_IP_REJECTION_PREFIX = (
    NotificationURLValidator.PRIVATE_IP_REJECTION_PREFIX
)


# Backward compatibility constants - now handled by Tenacity internally
MAX_RETRY_ATTEMPTS = 3
INITIAL_RETRY_DELAY = 0.5
RETRY_BACKOFF_MULTIPLIER = 2

# Maximum number of notification targets Apprise may parse out of one
# test/configured URL string. Prevents an authenticated caller from
# triggering unbounded DNS resolution work (and an unbounded fan-out of
# outbound requests) via a comma/space-separated hostname list.
MAX_NOTIFICATION_TARGETS = 20


class NotificationService:
    """
    Low-level notification service that wraps Apprise.
    """

    # Regex patterns for common service types (for validation)
    SERVICE_PATTERNS = {
        "email": r"^mailto://",
        "discord": r"^discord://",
        "slack": r"^slack://",
        "telegram": r"^tgram://",
        "smtp": r"^(smtp|smtps)://",
    }

    def __init__(
        self,
        allow_private_ips: bool = False,
        outbound_allowed: bool = False,
    ):
        """
        Initialize the notification service.

        Args:
            allow_private_ips: Whether to allow notifications to private/local IPs
                              (default: False for security). Set to True for
                              development/testing environments only.
            outbound_allowed: Server-level master switch
                    (env-only via LDR_NOTIFICATIONS_ALLOW_OUTBOUND).
                    Default False — outbound notifications are off until
                    the operator opts in. See SECURITY.md "Notification
                    Webhook SSRF" for the rationale.
        """
        install_apprise_log_record_factory()
        self.apprise = self._new_apprise()
        self.allow_private_ips = allow_private_ips
        self.outbound_allowed = outbound_allowed

    @staticmethod
    def _new_apprise() -> apprise.Apprise:
        """Create an Apprise instance that delivers synchronously and does
        not follow HTTP redirects.

        ``async_mode=False`` forces Apprise to run every plugin's
        ``notify()`` in the calling thread instead of fanning out to a
        ``ThreadPoolExecutor``. This is required for the SSRF hardening in
        :meth:`_send_with_retry`: the DNS pin and the block-private window
        (``security.dns_pinning``) are thread-local, so they only apply to
        the resolution the sending thread performs. With the default async
        fan-out the plugins would resolve in worker threads that carry no
        pin/block and the guard would silently not apply (verified: the
        default asset resolves target hosts on worker threads).

        ``http_redirects=False`` disables 30x redirect-following for the
        raw-webhook plugins (``json``/``xml``/``form``, which pass
        ``allow_redirects=self.redirects`` to ``requests``). This closes the
        redirect attack class outright: a user-configured webhook can no
        longer 30x-redirect the send to a private/loopback host or to an
        arbitrary public host for data exfiltration. The pin +
        block-private window is then only responsible for the original
        validated host's own rebind, not for chasing redirect hops. It is a
        DEFAULT: a per-URL ``?redirect=yes`` can still override it, so
        :meth:`_disable_redirects` re-forces it off per-plugin after
        ``add()`` (see there).
        """
        return apprise.Apprise(
            asset=apprise.AppriseAsset(async_mode=False, http_redirects=False)
        )

    @staticmethod
    def _disable_redirects(apprise_instance: apprise.Apprise) -> None:
        """Force redirect-following OFF on every added plugin.

        ``_new_apprise`` sets the asset-level ``http_redirects=False``
        default, but Apprise lets a per-URL ``?redirect=yes`` query
        parameter override that default (``self.redirects =
        parse_bool(kwargs.get("redirect", asset.http_redirects))``). A
        low-privilege user could therefore re-enable redirect-following on
        their own webhook and reopen redirect-to-private / redirect-to-exfil.
        Setting ``redirects = False`` directly on each plugin AFTER it is
        parsed makes the closure airtight: the value is read at send time
        (``allow_redirects=self.redirects``), so this wins over any URL
        parameter. Redirect-following is never needed for legitimate
        webhook/plugin delivery (real endpoints answer 2xx directly), so
        this cannot break a genuine send.

        Not every plugin threads ``self.redirects`` into its HTTP call: the
        raw-webhook ``json``/``xml``/``form`` schemes and e.g. ``ntfy`` /
        ``gotify`` honour it, but a few (e.g. ``matrix://``) issue requests
        without it, so re-forcing the flag has no effect there. Those are
        backstopped by the block-private send window
        (``dns_pinning.pinned_notification_send``), which refuses a
        redirect/rebind to a blocked address at the ``getaddrinfo`` layer
        regardless of whether the plugin followed the redirect.
        """
        try:
            servers = list(apprise_instance)
        except TypeError as exc:
            # A mocked Apprise in unit tests is not iterable, so there is
            # nothing to harden (those tests never exercise a real redirect).
            # But if a REAL Apprise ever stops being iterable (upstream API
            # drift), silently returning here would leave the per-plugin
            # redirect-disable un-applied — fail-open. The asset-level
            # ``http_redirects=False`` default and the block-private window
            # still apply, but a per-URL ``?redirect=yes`` could re-enable
            # redirect-following. Log a warning (narrowed to TypeError) so
            # that drift is visible instead of silent.
            logger.warning(
                "Could not enumerate Apprise servers to force "
                "redirect-following off per plugin ({}); relying on the "
                "asset-level default and the block-private window",
                type(exc).__name__,
            )
            return
        for server in servers:
            server.redirects = False

    def _enforce_guarded_send_invariants(
        self, apprise_instance: apprise.Apprise, tag: Optional[str]
    ) -> None:
        """Enforce the fail-closed invariants for a DNS-guarded send.

        The pin + block-private window (``security.dns_pinning``) are
        thread-local and only cover IN-THREAD delivery. Refuse the send
        (fail closed) rather than silently sending unguarded unless:

        * ``async_mode`` is off — otherwise Apprise fans the plugins out to
          worker threads that carry neither the pin nor the block; and
        * ``tag`` is None — a multi-token tag likewise makes Apprise
          dispatch matching plugins across worker threads. No production
          caller passes a tag; this neutralizes a future one.

        Then force redirect-following off per plugin so a per-URL
        ``?redirect=yes`` cannot override the asset-level default.

        Raises:
            SecurityBlockError: if either invariant is violated — a
                deliberate fail-closed security refusal, not an incidental
                runtime error, so it is raised directly as this type
                (rather than a generic ``RuntimeError`` a caller would have
                to re-classify) and is excluded from Tenacity retries so it
                fails fast. See ``send()``'s docstring for why this must
                not be mistaken for a transient delivery failure.
        """
        if getattr(apprise_instance.asset, "async_mode", True) is True:
            raise SecurityBlockError(
                "Guarded notification send requires async_mode=False; "
                "refusing to send (the thread-local DNS pin/block would not "
                "apply to Apprise's worker-thread fan-out)."
            )
        if tag is not None:
            raise SecurityBlockError(
                "Guarded notification send does not support tag targeting; "
                "refusing to send (a tag can fan delivery out to worker "
                "threads that bypass the thread-local DNS pin/block)."
            )
        self._disable_redirects(apprise_instance)

    @staticmethod
    def _partition_urls(
        service_urls: str, separator: str = ","
    ) -> Tuple[List[str], List[str]]:
        """Split an Apprise URL string into (http(s), everything-else).

        Uses the same separator-aware parser as validation and egress filtering
        (``parse_notification_url_list``). The default comma parser recognizes
        scheme-aware boundaries — a comma/whitespace sequence only separates
        entries when a URL scheme follows it, so commas INSIDE one Apprise URL
        (e.g. a multi-target Telegram ``?to=id1,id2``) are preserved and
        dispatched as a single entry; an explicit custom separator preserves
        commas and whitespace inside each URL outright. ``_dispatch``
        further separates host-bearing plugins from fixed vendor plugins
        and applies the matching send-time DNS policy.

        NOTE: the ``strict`` (``http``/``https``) partition is only
        PARTLY deliverable. Apprise has no generic http(s) notifier, so an
        arbitrary webhook is rejected at ``add()``
        (``apprise.Apprise().add("https://example.com/hook")`` is False on
        apprise 1.13.0) and a strict-only ``add()`` in :meth:`_dispatch`
        returns False without sending. But Apprise DOES claim the native
        ``https://`` forms of several vendors, so those deliver from this
        partition:
        ``apprise.Apprise().add("https://discord.com/api/webhooks/<id>/<token>")``
        and the ``https://hooks.slack.com/services/...`` form both return
        True. Generic webhooks should still use the raw-webhook schemes
        ``json``/``xml``/``form`` (in the ``lenient`` partition).
        """
        strict: List[str] = []
        lenient: List[str] = []
        entries, invalid_fragment = parse_notification_url_list(
            service_urls, separator
        )
        if invalid_fragment is not None:
            # The only correct response to a non-``None`` fragment is to
            # refuse the whole input (see ``parse_notification_url_list``'s
            # docstring) — never dispatch the malformed entry verbatim.
            # Never log the fragment itself — it may contain credentials;
            # length/count only, as the other fragment-log sites do.
            # ``send()`` runs ``validate_multiple_urls`` (same
            # separator-aware parse) before reaching here, so a malformed
            # fragment is already rejected there; this branch only fires
            # for direct callers of this helper. Fail closed rather than
            # dispatching only the valid subset — the caller must validate
            # the list themselves before dispatching it.
            # Rendered positionally: loguru kwargs with no template
            # placeholder reach no production sink (#5619).
            logger.warning(
                "Notification service_url list contains a malformed "
                "fragment; refusing the whole list "
                "(fragment_length={}, entries_parsed={})",
                len(invalid_fragment),
                len(entries),
            )
            return [], []
        for entry in entries:
            scheme = entry.split(":", 1)[0].lower() if ":" in entry else ""
            if scheme in ("http", "https"):
                strict.append(entry)
            else:
                lenient.append(entry)
        return strict, lenient

    def _guarded_notify(
        self,
        title: str,
        body: str,
        apprise_instance: apprise.Apprise,
        guard_factory=None,
        tag: Optional[str] = None,
        attach: Optional[List[str]] = None,
    ) -> bool:
        """Run a single Apprise ``notify()`` under the DNS-guard and the
        fail-closed invariants. Returns Apprise's raw boolean result.

        Single source of truth for the guarded send: both the retrying
        delivery path (:meth:`_send_with_retry`) and the admin
        "Send Test Notification" path (:meth:`test_service`) call this, so
        neither re-implements the invariant-enforcement / redirect-disable /
        guarded-``notify`` sequence that the SSRF hardening depends on.

        When ``guard_factory`` is provided, the fail-closed invariants are
        enforced (``async_mode`` off, no ``tag``, redirects forced off) and
        the send runs inside the pin + block-private window it returns. The
        per-thread SSRF-block marker is reset first so a block during THIS
        ``notify`` is attributable to it (see
        ``dns_pinning.consume_ssrf_block``). ``guard_factory=None`` sends
        WITHOUT a guard and is for tests ONLY — it exercises the unguarded
        baseline the SSRF teeth-tests assert against; every production caller
        MUST pass a ``guard_factory``.

        Args:
            title: Notification title
            body: Notification body text
            apprise_instance: Apprise instance to use for sending
            guard_factory: Optional zero-arg callable returning a context
                manager wrapped around the ``notify()`` call (the DNS pin +
                block-private window; see ``security.dns_pinning``).
            tag: Optional tag to target specific services
            attach: Optional list of file paths to attach

        Returns:
            True iff Apprise reported the notification delivered.
        """
        if guard_factory is not None:
            # Guarded (user-supplied URL) send: enforce the fail-closed
            # invariants the thread-local pin/block depend on and disable
            # redirects before dispatching, then clear any stale SSRF-block
            # marker so consume_ssrf_block reflects only this attempt.
            self._enforce_guarded_send_invariants(apprise_instance, tag)
            dns_pinning.reset_ssrf_block()

        guard = (
            guard_factory()
            if guard_factory is not None
            else contextlib.nullcontext()
        )

        # Send notification inside the guard so the pin + block-private
        # window (if any) governs Apprise's send-time DNS resolution.
        # ``notify`` is typed ``bool | None``; coerce to a definite bool
        # (None == nothing delivered == False).
        with guard:
            return bool(
                apprise_instance.notify(
                    title=title,
                    body=body,
                    tag=tag,
                    attach=attach,
                )
            )

    @retry(
        stop=stop_after_attempt(3),
        wait=wait_exponential(multiplier=1, min=0.5, max=10),
        # Retry transient delivery failures (SendError, connection errors,
        # …) but NOT deliberate security refusals: a ValueError is a
        # connect-time SSRF block (the pin's rebind-to-private/metadata catch,
        # or the block-private window's send-time refusal surfaced below); a
        # RuntimeError (specifically
        # ``dns_pinning.NotificationGuardUnavailableError``) is the DNS-pin
        # shim not being installed; and a SecurityBlockError is the
        # async_mode/tag fail-closed invariant guard
        # (``_enforce_guarded_send_invariants``), raised directly as that
        # type rather than a generic RuntimeError. A confirmed-malicious or
        # misconfigured destination must fail fast, not be retried 3x.
        retry=retry_if_not_exception_type(
            (ValueError, RuntimeError, SecurityBlockError)
        ),
        reraise=True,
    )
    def _send_with_retry(
        self,
        title: str,
        body: str,
        apprise_instance: apprise.Apprise,
        guard_factory=None,
        tag: Optional[str] = None,
        attach: Optional[List[str]] = None,
    ) -> bool:
        """
        Send a notification using the provided Apprise instance with retry logic.

        This method is decorated with Tenacity to handle retries automatically.
        A fresh guard context is created per attempt (via ``guard_factory``)
        so retries re-resolve and re-pin.

        Args:
            title: Notification title
            body: Notification body text
            apprise_instance: Apprise instance to use for sending
            guard_factory: Optional zero-arg callable returning the per-send
                DNS guard context (see :meth:`_guarded_notify`).
            tag: Optional tag to target specific services
            attach: Optional list of file paths to attach

        Returns:
            True if notification was sent successfully

        Raises:
            SendError: If sending fails after all retry attempts (transient).
            ValueError: If a guarded send failed because the block-private
                window refused a send-time resolution as an SSRF target —
                a confirmed security block, raised non-retryably so it fails
                fast instead of being retried.
            SecurityBlockError: Propagated non-retryably from
                ``_guarded_notify`` -> ``_enforce_guarded_send_invariants``
                if the async_mode/tag fail-closed invariant is violated.
            dns_pinning.NotificationGuardUnavailableError: Propagated
                non-retryably (a ``RuntimeError`` subclass) from
                ``_guarded_notify`` -> ``guard_factory`` if the DNS-pin
                shim is not installed.
        """
        logger.debug(
            f"Sending notification: title='{title[:50]}...', tag={tag}"
        )
        logger.debug(f"Body preview: {body[:200]}...")

        notify_result = self._guarded_notify(
            title, body, apprise_instance, guard_factory, tag, attach
        )

        if notify_result:
            logger.debug(f"Notification sent successfully: '{title[:50]}...'")
            return True

        # A guarded send that failed AND tripped the block-private window is a
        # confirmed SSRF block: the window raised socket.gaierror on a
        # send-time resolution to an internal/private/metadata IP, which
        # Apprise swallowed into a generic delivery failure. Fail fast — raise
        # a non-retryable ValueError (excluded from the retry predicate above)
        # instead of a retryable SendError, so a confirmed-malicious /
        # misconfigured destination is not retried 3x (~10s and 3x DNS to a
        # hostile authority). The pin path's own rebind catch already raises
        # ValueError before this point; this covers the UNPINNED block-window
        # path (plugin-scheme endpoints, unresolvable-at-pin-time hosts).
        if guard_factory is not None and dns_pinning.consume_ssrf_block():
            logger.warning(
                "Notification send blocked by the SSRF block-private window; "
                "failing fast (not retrying)"
            )
            raise ValueError(
                "Notification send refused: a send-time DNS resolution was "
                "blocked as internal/private/metadata (possible SSRF)"
            )

        error_msg = "Failed to send notification to any service"
        logger.warning(error_msg)
        raise SendError(error_msg)

    def send(
        self,
        title: str,
        body: str,
        service_urls: Optional[str] = None,
        tag: Optional[str] = None,
        attach: Optional[List[str]] = None,
        separator: str = ",",
    ) -> bool:
        """
        Send a notification to separator-delimited service URLs with retry.

        Args:
            title: Notification title
            body: Notification body text
            service_urls: Service URLs delimited by ``separator``
            tag: Optional tag to target specific services
            attach: Optional list of file paths to attach
            separator: Delimiter between service URLs

        Returns:
            True if notification was sent successfully to at least one service

        Raises:
            SendError: If sending fails after all retry attempts (transient
                — the endpoint refused/failed the dispatch).
            SecurityBlockError: If a send-time SSRF/DNS-rebind block
                confirmed the destination is internal/private/metadata
                (permanent — not worth retrying). A ``ServiceError``
                subclass — it still satisfies ``except ServiceError`` /
                ``pytest.raises(ServiceError)`` and shares the same
                non-retryable signal as pre-dispatch validation rejects,
                but is tagged distinctly so callers can give it a more
                accurate message than "invalid URL".

        Note:
            Temporary Apprise instances are created for each send operation
            and are automatically garbage collected by Python when they go
            out of scope. This simple approach is ideal for small deployments
            (~5 users) and avoids memory management complexity.
        """
        # Defense-in-depth: enforce the operator-level master switch at
        # the service layer too, not just at NotificationManager.
        # Today the manager always wraps this method, but keeping the
        # gate here means a future direct caller cannot accidentally
        # bypass it. See SECURITY.md "Notification Webhook SSRF".
        if not self.outbound_allowed:
            logger.warning(
                "Notification not sent: outbound notifications are disabled "
                "at the server level. Set "
                "LDR_NOTIFICATIONS_ALLOW_OUTBOUND=true to enable. See "
                "SECURITY.md 'Notification Webhook SSRF'."
            )
            return False

        # If service_urls are provided, validate before trying to send
        if service_urls:
            logger.debug("Creating Apprise instance for provided service URLs")

            # Validate service URLs for security (SSRF prevention)
            is_valid, error_msg = (
                NotificationURLValidator.validate_multiple_urls(
                    service_urls,
                    allow_private_ips=self.allow_private_ips,
                    separator=separator,
                )
            )

            if not is_valid:
                logger.error("Service URL validation failed")
                raise ServiceError(f"Invalid service URL: {error_msg}")

        try:
            # If service_urls are provided, partition them by scheme and
            # dispatch each group with its own SSRF send-time guard.
            if service_urls:
                strict_urls, lenient_urls = self._partition_urls(
                    service_urls, separator
                )
                if not strict_urls and not lenient_urls:
                    logger.error("No service URLs after parsing")
                    return False
                return self._dispatch(
                    title, body, strict_urls, lenient_urls, tag, attach
                )
            # The pre-configured instance (self.apprise) is created empty in
            # __init__ and is never populated by this class — all delivery
            # goes through the guarded per-call service_urls path above.
            if len(self.apprise) == 0:
                logger.debug("No notification services configured in Apprise")
                return False

            # Defense-in-depth: if a future caller ever adds URLs directly to
            # self.apprise, refuse rather than deliver them here — this branch
            # has no DNS pin / block-private guard, so sending would silently
            # bypass the SSRF hardening the service_urls path enforces. Fail
            # closed. See SECURITY.md "Notification Webhook SSRF".
            logger.error(
                "Refusing to send via the pre-configured Apprise instance: "
                "it is populated but this path is unguarded. Pass "
                "service_urls to send() so the DNS pin / block-private guard "
                "applies."
            )
            return False

        except SecurityBlockError:
            # _enforce_guarded_send_invariants (called from _guarded_notify,
            # which _send_with_retry / _dispatch call) raises this DIRECTLY
            # — not a generic RuntimeError — for the async_mode/tag
            # fail-closed invariant guard: a deliberate security refusal,
            # not an incidental runtime error. It is already the precise,
            # final exception type (see its docstring), so propagate it
            # unchanged rather than re-wrapping: NotificationManager's
            # ``except SecurityBlockError`` maps it to the same
            # non-transient INVALID_URL reason as the confirmed-block paths
            # below.
            raise

        except ValueError as e:
            # _send_with_retry raises ValueError (excluded from Tenacity's
            # retry predicate) ONLY for a confirmed, non-retryable send-time
            # SSRF/DNS-rebind block — see its docstring and
            # dns_pinning.pinned_notification_send's own rebind catch. That
            # is a permanent, invalid-destination outcome, not a transient
            # delivery failure, so it must NOT be re-wrapped as SendError:
            # NotificationManager's ``except SendError`` maps to the
            # retryable WEBHOOK_FAILED reason, which would mislabel a
            # confirmed security block as "webhook delivery failed after
            # retries" and could invite a caller/monitor to retry a
            # malicious/rebinding destination. Route it through
            # SecurityBlockError — a ServiceError subclass, so it still
            # maps to INVALID_URL via NotificationManager's existing
            # ``except ServiceError`` handling — but tagged distinctly so
            # the manager can give this confirmed security block a more
            # accurate user-facing detail than the pre-dispatch
            # "check your URL/settings" text (see manager.py).
            logger.exception(
                f"Notification send blocked (SSRF/DNS-rebind): "
                f"'{title[:50]}...'"
            )
            raise SecurityBlockError(str(e)) from e

        except dns_pinning.NotificationGuardUnavailableError as e:
            # pinned_notification_send (via the guard_factory _dispatch
            # builds) raises this — a RuntimeError subclass defined in
            # security.dns_pinning, deliberately NOT imported/raised as a
            # notifications exception there so that general-purpose
            # security module stays free of a dependency on this
            # higher-level package — when the getaddrinfo shim it depends
            # on is not the active resolver. Also a deliberate fail-closed
            # security refusal, not a transient send failure (excluded from
            # Tenacity's retry predicate as a RuntimeError — see
            # _send_with_retry's decorator), so route it through
            # SecurityBlockError for the same reason as the ValueError
            # branch above: it must not be mislabeled as a retryable
            # WEBHOOK_FAILED.
            logger.exception(
                f"Notification send blocked (DNS-pin guard unavailable): "
                f"'{title[:50]}...'"
            )
            raise SecurityBlockError(str(e)) from e

        except SendError:
            # _send_with_retry's own terminal raise (line ~420, once all
            # retries are exhausted) is already the precise, final
            # exception for a confirmed webhook delivery failure.
            # Propagate unchanged — re-wrapping it here would just nest a
            # second SendError around the first for no benefit, and
            # routing it through the catch-all below would make it
            # indistinguishable from an incidental dispatch-time bug.
            raise

        except Exception:
            # Anything else reaching here is an incidental/unexpected
            # error from the dispatch machinery itself (e.g. a bug in
            # this method's call wiring, an Apprise internals exception
            # that isn't a genuine delivery failure) — NOT a confirmed
            # webhook delivery failure. Do NOT launder it into SendError:
            # NotificationManager maps SendError to the retryable
            # WEBHOOK_FAILED reason, which would mislabel an incidental
            # bug as "webhook delivery failed after retries" and would
            # make the manager's own ``except Exception`` -> EXCEPTION
            # branch unreachable for dispatch-time errors. Let it
            # propagate as-is so the manager's catch-all handles it.
            logger.exception(
                f"Unexpected error sending notification: '{title[:50]}...'"
            )
            raise

    def _dispatch(
        self,
        title: str,
        body: str,
        strict_urls: List[str],
        lenient_urls: List[str],
        tag: Optional[str],
        attach: Optional[List[str]],
    ) -> bool:
        """Send each scheme-partition under its SSRF send-time guard.

        ``strict_urls`` (http/https): pin each host and block private +
        metadata resolution for the send (honoring the operator
        ``allow_private_ips`` opt-in) — closes rebinding and
        redirect-to-internal. Link-local stays blocked even with the
        opt-in: Apprise turns some http(s) URLs into plugins that post to
        the URL host (Apprise API, Workflows).

        ``lenient_urls`` are classified by effective destination. Plugins
        whose authority selects the connection host use the operator's
        private-IP opt-in and a link-local block. Fixed vendor endpoints
        use the same operator opt-in (public-only by default): their hosts
        are fixed, so a private answer normally means a forward proxy
        (``HTTPS_PROXY``) that the operator must approve. Link-local and
        cloud-metadata addresses stay blocked for both.

        Each non-empty partition is dispatched (and retried by Tenacity)
        under its own guard. A partition that adds no URLs returns False
        (matching the historical add-failure behavior); a partition that
        fails to deliver after all retries raises ``SendError`` (which the
        caller re-wraps), so overall success requires every partition to
        deliver.
        """
        partitions = []
        if strict_urls:
            partitions.append(
                (
                    strict_urls,
                    lambda: dns_pinning.pinned_notification_send(
                        strict_urls,
                        allow_localhost=False,
                        allow_private_ips=self.allow_private_ips,
                        block_link_local=True,
                    ),
                )
            )
        authority_urls = []
        vendor_urls = []
        for entry in lenient_urls:
            try:
                destination = parse_notification_destination(entry)
            except NotificationDestinationError as exc:
                raise SecurityBlockError(
                    "Notification destination is unsupported or ambiguous"
                ) from exc
            if destination.kind is NotificationDestinationKind.AUTHORITY_PLUGIN:
                authority_urls.append(entry)
            elif destination.kind is NotificationDestinationKind.VENDOR_PLUGIN:
                vendor_urls.append(entry)
            else:
                raise SecurityBlockError("Notification partition is ambiguous")
        if authority_urls:
            partitions.append(
                (
                    authority_urls,
                    lambda: dns_pinning.pinned_notification_send(
                        authority_urls,
                        allow_localhost=False,
                        allow_private_ips=self.allow_private_ips,
                        block_link_local=True,
                    ),
                )
            )
        if vendor_urls:
            partitions.append(
                (
                    vendor_urls,
                    lambda: dns_pinning.pinned_notification_send(
                        vendor_urls,
                        allow_localhost=False,
                        allow_private_ips=self.allow_private_ips,
                        block_link_local=True,
                    ),
                )
            )

        any_ok = False
        for urls, guard_factory in partitions:
            temp_apprise = self._new_apprise()
            # apprise types ``add(servers=...)`` as an invariant ``list`` whose
            # element union includes ``str``; a ``list[str]`` is valid at
            # runtime but mypy rejects it on list invariance (upstream should
            # use ``Sequence``).
            if not temp_apprise.add(urls, tag=tag):  # type: ignore[arg-type]
                logger.error("Failed to add service URLs to Apprise")
                return False
            # _send_with_retry returns True or raises SendError after
            # Tenacity exhausts its retries; a raise propagates to send().
            self._send_with_retry(
                title, body, temp_apprise, guard_factory, tag, attach
            )
            any_ok = True
        return any_ok

    def send_event(
        self,
        event_type: EventType,
        context: Dict[str, Any],
        service_urls: Optional[str] = None,
        tag: Optional[str] = None,
        custom_template: Optional[Dict[str, str]] = None,
        separator: str = ",",
    ) -> bool:
        """
        Send a notification for a specific event type.

        Args:
            event_type: Type of event
            context: Context data for template formatting
            service_urls: Service URLs delimited by ``separator``
            tag: Optional tag to target specific services
            custom_template: Optional custom template override
            separator: Delimiter between service URLs

        Returns:
            True if notification was sent successfully
        """
        logger.debug(f"send_event: event_type={event_type.value}, tag={tag}")
        logger.debug(f"Context: {context}")

        # Format notification using template
        message = NotificationTemplate.format(
            event_type, context, custom_template
        )
        logger.debug(
            f"Template formatted - title: '{message['title'][:50]}...'"
        )

        # Send notification
        return self.send(
            title=message["title"],
            body=message["body"],
            service_urls=service_urls,
            tag=tag,
            separator=separator,
        )

    def test_service(self, url: str) -> Dict[str, Any]:
        """
        Test a notification service.

        Args:
            url: Apprise-compatible service URL

        Returns:
            Dict with 'success' boolean and optional 'error' message
        """
        # Server-level master switch (env-only). Mirrors
        # NotificationManager's gate so the "Send Test Notification"
        # button cannot bypass it. WARNING level so the operator sees
        # the actionable signal naming the env var.
        if not self.outbound_allowed:
            logger.warning(
                "Notification test refused: outbound notifications are "
                "disabled at the server level. Set "
                "LDR_NOTIFICATIONS_ALLOW_OUTBOUND=true to enable. See "
                "SECURITY.md 'Notification Webhook SSRF'."
            )
            return {
                "success": False,
                "error": (
                    "Outbound notifications are disabled. The server "
                    "administrator must set "
                    "LDR_NOTIFICATIONS_ALLOW_OUTBOUND=true to enable "
                    "notification webhooks. See SECURITY.md "
                    "'Notification Webhook SSRF' for details."
                ),
            }

        try:
            # ``url`` is a single form field, but Apprise's own
            # scheme-aware splitter treats it as a LIST — and commas are
            # legal URL characters. Validating ``url`` as ONE url
            # therefore checked only the leading entry while Apprise
            # still registered, and notified, every other entry:
            # ``discord://valid/tok,json://<cloud-metadata-host>/x``
            # passed validation on the discord entry alone and the
            # cloud-metadata target received the test notification
            # unvetted. Partition the input with the shared scheme-aware
            # parser — the same one the configured-send path uses via
            # ``validate_multiple_urls`` — and vet EVERY entry.
            url_entries, invalid_fragment = parse_notification_url_list(url)
            if invalid_fragment is not None:
                # The input does not partition unambiguously: either a
                # URL-like fragment carries no scheme (Apprise may repair
                # it or absorb it into a neighbouring entry) or an entry
                # contains a character that is illegal unencoded in a URI
                # (backslash, whitespace, a control byte) and the token
                # after it is a second, unvalidated destination. REFUSE
                # the whole input — mirrors ``validate_multiple_urls``,
                # which returns False for every fragment shape.
                #
                # Substituting ``url_entries = [invalid_fragment]`` here
                # (as this code did before) was exploitable: the
                # fragment is NOT necessarily scheme-less, because
                # ``_URL_BOUNDARY_RE`` splits only on ``[,\s]+`` while
                # the fragment is cut on ``RFC_FORBIDDEN_URL_CHARS_RE``,
                # which also covers backslash and non-``\s`` control
                # bytes. So ``json://<metadata-host>/x\https://vendor/ok``
                # stays ONE entry whose fragment is the *vendor* URL:
                # the loop below would validate only that decoy, the
                # smuggle check would compare ``1 > 1``, and the
                # then-current ``temp_apprise.add(url)`` would dispatch
                # the RAW original string to the metadata host — with the
                # pin/block-private policy derived from the decoy's
                # scheme. (``add()`` now takes the parsed entry list, so
                # the raw string is no longer in the dispatch path at
                # all; this refusal is still the primary defence, since
                # the decoy would otherwise be the only entry vetted.)
                # Never validate a fragment in place of the input it was
                # carved out of.
                #
                # The message is deliberately generic: the fragment's
                # own content is attacker-shaped AND may carry the
                # operator's real credentials, so nothing derived from
                # it is echoed to the user or the log.
                logger.warning(
                    "Test notification refused: the service URL does not "
                    "partition into unambiguous entries "
                    "({} entries parsed, {}-character trailing fragment).",
                    len(url_entries),
                    len(invalid_fragment),
                )
                return {
                    "success": False,
                    "error": (
                        "Notification service URL could not be parsed "
                        "unambiguously; refusing to send. Every entry must "
                        "begin with a protocol such as discord:// — remove "
                        "any spaces, backslashes or control characters and "
                        "configure one complete service URL per test."
                    ),
                }
            if not url_entries:
                return {
                    "success": False,
                    "error": "Invalid notification service URL.",
                }
            if len(url_entries) > MAX_NOTIFICATION_TARGETS:
                logger.warning(
                    f"Test notification refused: {len(url_entries)} targets "
                    f"exceeds the cap of {MAX_NOTIFICATION_TARGETS}."
                )
                return {
                    "success": False,
                    "error": (
                        f"Too many notification targets "
                        f"({len(url_entries)}). Maximum is "
                        f"{MAX_NOTIFICATION_TARGETS}."
                    ),
                }

            for entry in url_entries:
                # Validate each entry for security (SSRF prevention) and,
                # in the same pass, compute whether the admin env-var hint
                # would actually unblock a recoverable private-IP
                # rejection. Single-pass avoids a DNS-rebinding TOCTOU
                # window between the default-level validation and the
                # elevated-level hint decision — see
                # NotificationURLValidator.validate_service_url_with_hint.
                is_valid, error_msg, hint_would_help = (
                    NotificationURLValidator.validate_service_url_with_hint(
                        entry, allow_private_ips=self.allow_private_ips
                    )
                )

                if not is_valid:
                    # Never log the entry itself — an Apprise service URL
                    # carries credentials (bot tokens, webhook secrets).
                    # #5576 removed the masked-URL echo from this line for
                    # exactly that reason; keep it out of the per-entry
                    # loop too.
                    logger.warning(
                        f"Test service URL validation failed: {error_msg}"
                    )
                    # Surface the validator's reason so users know what to
                    # fix. The hostname/scheme echoed here was supplied by
                    # the user in the same request, so this is not a
                    # server-side leak. When the rejection is a recoverable
                    # private/internal IP — one that
                    # LDR_NOTIFICATIONS_ALLOW_PRIVATE_IPS=true OR
                    # LDR_SECURITY_ALLOW_NAT64=true would actually unblock —
                    # append the admin escape hatches; the user cannot fix
                    # this themselves. Suppress the hint for always-blocked
                    # categories (metadata, 6to4, Teredo, discard,
                    # IPv4-mapped IPv6 of metadata, IPv4-Compatible IPv6,
                    # IPv4-Translated/SIIT, NAT64-wrapped metadata): no env
                    # var can help, so naming one would mislead.
                    user_error = (
                        error_msg or "Invalid notification service URL."
                    )
                    if hint_would_help:
                        user_error += (
                            ". To unblock this destination, ask the server "
                            "administrator to set "
                            "LDR_NOTIFICATIONS_ALLOW_PRIVATE_IPS=true "
                            "(IPv6-only NAT64 deployments also need "
                            "LDR_SECURITY_ALLOW_NAT64=true). "
                            "See SECURITY.md 'Notification Webhook SSRF'."
                        )
                    return {
                        "success": False,
                        "error": user_error,
                    }

            # Create temporary Apprise instance (synchronous so the pin +
            # block-private window below applies in this thread).
            temp_apprise = self._new_apprise()
            # Hand Apprise the ALREADY-PARTITIONED entries, not the raw
            # string — the same thing :meth:`_dispatch` does. ``add()``
            # runs its own ``parse_urls`` splitter ONLY on a ``str``
            # argument; given a list it instantiates exactly one plugin
            # per element. Passing ``url`` here let LDR's boundary regex
            # and Apprise's ``URL_DETECTION_RE`` each get a vote on where
            # the entries start, and the two do not agree: Apprise's
            # scheme class is ``[a-z0-9]`` (a LEADING DIGIT starts a new
            # entry, ``+ . -`` do not) while LDR's requires a leading
            # letter and accepts ``+ . -``. So ``discord://a/b,7z://x``
            # is ONE entry to LDR (validated as a discord URL) and TWO to
            # Apprise — the second never seen by the SSRF validator. Only
            # the count guard below caught that, and only because every
            # scheme in ``ALLOWED_SCHEMES`` happens to be pure lowercase
            # alpha, which is a property of a constant rather than of
            # this code. With a list there is ONE parser in the path and
            # the set Apprise dispatches is exactly the set validated
            # above.
            add_result = temp_apprise.add(list(url_entries))

            if not add_result:
                return {
                    "success": False,
                    "error": "Failed to add service URL",
                }

            # Fail closed on a parser differential: if Apprise registered
            # MORE targets than we validated, at least one destination
            # would be notified without ever passing the SSRF validator.
            # Fewer is harmless (Apprise dropped an entry), so only the
            # smuggling direction is refused.
            #
            # With the list-form ``add()`` above this guard is STRUCTURAL,
            # not a live check: ``Apprise.__len__`` counts one per
            # instantiated plugin and the list form appends AT MOST ONE
            # plugin per element, so ``len(temp_apprise) > len(url_entries)``
            # cannot be true for any input on the current Apprise. It is
            # kept as a cheap invariant assertion — it still catches a
            # future Apprise release that expands one URL into several
            # servers, and it is the last thing standing if the ``add()``
            # argument is ever changed back to a string. The argument
            # shape itself is pinned by
            # ``test_test_service_success`` in ``tests/notifications/
            # test_service.py``, which asserts ``add`` receives the parsed
            # LIST.
            if len(temp_apprise) > len(url_entries):
                logger.warning(
                    f"Test notification refused: Apprise registered "
                    f"{len(temp_apprise)} targets but only "
                    f"{len(url_entries)} were validated."
                )
                return {
                    "success": False,
                    "error": (
                        "Notification service URL could not be parsed "
                        "unambiguously; refusing to send. Configure one "
                        "service URL per test."
                    ),
                }

            # Send the test through the SAME guarded dispatch core as the real
            # send path (:meth:`_guarded_notify`) — fail-closed invariants,
            # per-plugin redirect-disable, and the pin + block-private window
            # — instead of re-implementing it here. Single-shot (no Tenacity
            # retry) for fast test feedback. Every partition uses the
            # operator's private-IP opt-in (public-only by default) and
            # blocks the whole link-local range, as ``_dispatch`` does.
            # Tag is always None here.
            try:
                for entry in url_entries:
                    parse_notification_destination(entry)
            except NotificationDestinationError:
                return {
                    "success": False,
                    "error": "Notification destination is unsupported or ambiguous",
                }
            guard_factory = functools.partial(
                dns_pinning.pinned_notification_send,
                list(url_entries),
                allow_localhost=False,
                allow_private_ips=self.allow_private_ips,
                block_link_local=True,
            )

            result = self._guarded_notify(
                "Test Notification",
                (
                    "This is a test notification from Local Deep Research. "
                    "If you see this, your service is configured correctly!"
                ),
                temp_apprise,
                guard_factory,
                tag=None,
            )

            if result:
                return {
                    "success": True,
                    "message": "Test notification sent successfully",
                }

            # A guarded test send that failed AND tripped the block-private
            # window is a confirmed SSRF block: the window raised
            # socket.gaierror on a send-time resolution to an
            # internal/private/metadata IP, which Apprise swallowed into a
            # generic delivery failure. Surface the specific reason to the
            # admin instead of the generic message — mirrors the
            # consume_ssrf_block short-circuit in _send_with_retry. (The pin
            # path's own connect-time rebind catch raises ValueError before
            # this point and is reported via the except below; this covers the
            # UNPINNED block-window path — plugin-scheme endpoints and
            # unresolvable-at-pin-time hosts.)
            if dns_pinning.consume_ssrf_block():
                logger.warning(
                    "Test notification blocked by the SSRF block-private "
                    "window (send-time resolution to internal/private/"
                    "metadata)"
                )
                return {
                    "success": False,
                    "error": (
                        "Test notification refused: the destination resolved "
                        "to an internal/private/metadata address at send time "
                        "(possible SSRF). See SECURITY.md 'Notification "
                        "Webhook SSRF'."
                    ),
                }
            return {
                "success": False,
                "error": "Failed to send test notification",
            }

        except Exception:
            logger.exception("Error testing notification service")
            return {
                "success": False,
                "error": "Failed to test notification service.",
            }

    @staticmethod
    def _validate_url(url: str) -> None:
        """
        Validate a notification service URL.

        Args:
            url: URL to validate

        Raises:
            ServiceError: If URL is invalid

        Note:
            URL scheme validation is handled by Apprise itself, which maintains
            a comprehensive whitelist of supported notification services.
            Apprise will reject unsupported schemes like 'file://' or 'javascript://'.
            See: https://github.com/caronc/apprise/wiki
        """
        if not url or not isinstance(url, str):
            raise ServiceError("URL must be a non-empty string")

        # Check if it looks like a URL
        parsed = urlparse(url)
        if not parsed.scheme:
            raise ServiceError(
                "Invalid URL format. Must be an Apprise-compatible "
                "service URL (e.g., discord://webhook_id/token)"
            )

    def get_service_type(self, url: str) -> Optional[str]:
        """
        Detect service type from URL.

        Args:
            url: Service URL

        Returns:
            Service type name or None if unknown
        """
        for service_name, pattern in self.SERVICE_PATTERNS.items():
            if re.match(pattern, url, re.IGNORECASE):
                return service_name
        return "unknown"