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.

17.6 KB
Raw Blame
"""Classify the network destination represented by an Apprise URL."""

from dataclasses import dataclass
from enum import Enum
import ipaddress
import re
from string import ascii_letters, digits, hexdigits
from urllib.parse import SplitResult, unquote, urlsplit

from urllib3.exceptions import LocationParseError
from urllib3.util import parse_url


class NotificationDestinationKind(str, Enum):
    HTTP = "http"
    AUTHORITY_PLUGIN = "authority_plugin"
    VENDOR_PLUGIN = "vendor_plugin"


@dataclass(frozen=True, slots=True)
class NotificationDestination:
    kind: NotificationDestinationKind
    effective_host: str | None


class NotificationDestinationError(ValueError):
    """A notification URL whose destination cannot be classified safely."""

    def __init__(self, reason: str) -> None:
        super().__init__(reason)
        self.reason = reason

    def __str__(self) -> str:
        return self.reason


_UNRESERVED_HOST_ESCAPES = re.compile(r"%([0-9A-Fa-f]{2})")
_UNRESERVED_HOST_CHARACTERS = frozenset(ascii_letters + digits + "-._~")


def normalize_host_for_transport(host: str) -> str:
    """Decode one pass of unreserved host escapes as Requests prepares URLs."""

    def unescape(match: re.Match[str]) -> str:
        decoded = chr(int(match.group(1), 16))
        return (
            decoded
            if decoded in _UNRESERVED_HOST_CHARACTERS
            else match.group(0)
        )

    return _UNRESERVED_HOST_ESCAPES.sub(unescape, host)


def _required_host(parsed: SplitResult) -> str:
    try:
        host = parsed.hostname
    except ValueError as exc:
        raise NotificationDestinationError("invalid authority") from exc
    if not host:
        raise NotificationDestinationError("destination host is missing")
    if ":" in host and "%" in host:
        # An IPv6 zone identifier (``[fd00::1%25eth0]``) scopes a literal to
        # an interface; no notification endpoint needs one, and a scoped
        # address no longer equals the unscoped metadata literals.
        raise NotificationDestinationError("IPv6 zone identifier in host")
    # urlsplit leaves percent escapes in the hostname. Requests decodes
    # unreserved escapes while preparing the outbound URL, but urllib3's
    # parse_url changed its own decoding behavior between 2.7 and 2.8.
    # Normalize once so policy resolves the same host under either version.
    authority = f"[{host}]" if ":" in host else host
    try:
        network_host = parse_url(f"http://{authority}/").host
    except LocationParseError as exc:
        raise NotificationDestinationError("invalid authority") from exc
    if not network_host:
        raise NotificationDestinationError("destination host is missing")
    if network_host.startswith("[") and network_host.endswith("]"):
        network_host = network_host[1:-1]
    return normalize_host_for_transport(network_host).rstrip(".")


def _plugin_host(parsed: SplitResult) -> str:
    """Return the authority host of an Apprise plugin URL.

    Several plugins (Signal, ntfy, Matrix, email) parse with
    ``verify_host=False`` and fully percent-decode the host, so an escape
    such as ``%2F`` or ``%40`` moves the real destination
    (``evil.com%2F.local`` is sent to ``evil.com``). No legitimate plugin
    host needs an escape, so any ``%`` is refused instead of guessing each
    plugin's decoding.
    """
    try:
        raw_host = parsed.hostname
    except ValueError as exc:
        raise NotificationDestinationError("invalid authority") from exc
    if raw_host and "%" in raw_host:
        raise NotificationDestinationError(
            "percent-encoded plugin host is ambiguous"
        )
    return _required_host(parsed)


def parse_apprise_query(url: str) -> dict[str, list[str]]:
    """Parse query keys with the same normalization Apprise applies."""
    _, separator, query = url.partition("?")
    if not separator or not query:
        return {}

    parsed_query: dict[str, list[str]] = {}
    pairs = [
        part for section in query.split("&") for part in section.split(";")
    ]
    for pair in pairs:
        raw_key, _, raw_value = pair.partition("=")
        index = 0
        while index < len(raw_key):
            if raw_key[index] != "%":
                index += 1
                continue
            if (
                index + 2 >= len(raw_key)
                or raw_key[index + 1] not in hexdigits
                or raw_key[index + 2] not in hexdigits
            ):
                raise NotificationDestinationError(
                    "Malformed percent-encoding in notification parameter name"
                )
            index += 3

        apprise_key = (
            raw_key[:1] + raw_key[1:].replace("+", " ") if raw_key else ""
        )
        canonical_key = unquote(apprise_key).lower().strip()
        parsed_query.setdefault(canonical_key, []).append(
            unquote(raw_value).strip()
        )
    return parsed_query


def _query_value(url: str, key: str) -> str | None:
    values = parse_apprise_query(url).get(key)
    if values is None:
        return None
    if len(values) != 1 or not values[0].strip():
        raise NotificationDestinationError(f"ambiguous {key} parameter")
    return values[0].strip().lower()


def _apprise_host(host: object) -> str:
    """Normalize a host reported by Apprise for comparison with ours."""
    if not isinstance(host, str) or not host:
        raise NotificationDestinationError("destination host is missing")
    if host.startswith("[") and host.endswith("]"):
        host = host[1:-1]
    return host.rstrip(".").lower()


def _confirm_apprise_ipv6_host(url: str, host: str) -> str:
    """Refuse an IPv6 literal that Apprise would send somewhere else.

    Apprise's generic URL parser rewrites compressed IPv6 literals in the
    authority: on Apprise 1.13 ``json://[fd00::1]/x`` builds a plugin whose
    host is ``[fd00::]`` and ``[::ffff:10.0.0.5]`` becomes ``[::ffff:10]``.
    Policy would then vet one address while Apprise connects to another.
    For an IPv6 literal the plugin is therefore built the way
    ``Apprise.add`` builds it (``Apprise.instantiate``, no network I/O) and
    its host must be the same address. A URL Apprise cannot build is
    returned unchanged: ``Apprise.add`` rejects it and nothing is sent.
    Hostnames and IPv4 literals are not rewritten by that parser and are
    returned as they are.

    The port is compared too: Apprise's port regex for a bracketed literal
    is case-sensitive, so ``json://[FD00:0:0:0:0:0:0:1]:8080/x`` keeps the
    host but silently drops ``:8080`` and would post to the default port.
    """
    try:
        expected = ipaddress.ip_address(host)
    except ValueError:
        return host
    if expected.version != 6:
        return host

    from apprise import Apprise, AppriseAsset

    plugin = Apprise.instantiate(
        url, asset=AppriseAsset(async_mode=False, http_redirects=False)
    )
    if plugin is None:
        return host
    try:
        actual = ipaddress.ip_address(
            _apprise_host(getattr(plugin, "host", None))
        )
    except ValueError as exc:
        raise NotificationDestinationError(
            "IPv6 host is rewritten by Apprise"
        ) from exc
    if actual != expected:
        raise NotificationDestinationError("IPv6 host is rewritten by Apprise")
    try:
        expected_port = urlsplit(url).port
    except ValueError as exc:
        raise NotificationDestinationError("IPv6 port is malformed") from exc
    actual_port = getattr(plugin, "port", None)
    if isinstance(actual_port, str) and actual_port.isdigit():
        actual_port = int(actual_port)
    if actual_port != expected_port:
        raise NotificationDestinationError("IPv6 port is rewritten by Apprise")
    return host


def _apprise_ntfy_destination(url: str, host: str) -> NotificationDestination:
    """Classify an ntfy URL from the plugin object Apprise itself builds.

    ntfy picks cloud mode (a fixed POST to ntfy.sh, with the authority used
    as a topic) or private mode (a POST to the authority host) from the
    host's validity, the path, ``to=`` and ``mode=``. Re-implementing those
    rules drifts in both directions, so the decision is read from the plugin
    built the way ``Apprise.instantiate`` (called by ``Apprise.add``) builds
    it: ``url_to_dict`` followed by the plugin constructor. Construction
    performs no network I/O.
    """
    from apprise import AppriseAsset
    from apprise.plugins import url_to_dict
    from apprise.plugins.ntfy import NotifyNtfy

    results = url_to_dict(url)
    if not results or str(results.get("schema", "")).lower() not in (
        "ntfy",
        "ntfys",
    ):
        raise NotificationDestinationError(
            "ntfy URL is not accepted by Apprise"
        )
    try:
        plugin = NotifyNtfy(
            **{
                **results,
                "asset": AppriseAsset(async_mode=False, http_redirects=False),
                "tag": set(),
            }
        )
    except Exception as exc:  # Apprise.instantiate treats any error as reject
        raise NotificationDestinationError(
            "ntfy URL is not accepted by Apprise"
        ) from exc
    mode = str(getattr(plugin, "mode", "") or "").strip().lower()
    if mode == "cloud":
        return NotificationDestination(
            NotificationDestinationKind.VENDOR_PLUGIN,
            None,
        )
    if mode != "private":
        raise NotificationDestinationError("unsupported ntfy mode")
    if not getattr(plugin, "topics", None):
        # Apprise would send nothing; refuse rather than guess.
        raise NotificationDestinationError("private ntfy target is missing")
    apprise_host = _apprise_host(getattr(plugin, "host", None))
    if apprise_host != host:
        raise NotificationDestinationError("ntfy host is ambiguous")
    if apprise_host == "ntfy.sh":
        return NotificationDestination(
            NotificationDestinationKind.VENDOR_PLUGIN,
            None,
        )
    return NotificationDestination(
        NotificationDestinationKind.AUTHORITY_PLUGIN,
        apprise_host,
    )


def _apprise_matrix_details(url: str) -> tuple[bool, str]:
    """Return Apprise's own (is_t2bot, host) for a Matrix URL.

    Uses the parser ``Apprise.add`` calls (``url_to_dict``) without building
    the plugin, whose constructor reads Apprise's persistent store.
    """
    from apprise.plugins import url_to_dict

    results = url_to_dict(url)
    if not results or str(results.get("schema", "")).lower() != "matrix":
        raise NotificationDestinationError(
            "matrix URL is not accepted by Apprise"
        )
    mode = str(results.get("mode") or "").strip().lower()
    return mode == "t2bot", str(results.get("host") or "")


def _email_template_smtp_hosts() -> frozenset[str]:
    """Return the fixed SMTP hosts of Apprise's built-in mail providers."""
    from apprise.plugins.email.templates import EMAIL_TEMPLATES

    return frozenset(
        str(template[2]["smtp_host"]).rstrip(".").lower()
        for template in EMAIL_TEMPLATES
        if template[2].get("smtp_host")
    )


def _apprise_mail_host(url: str, host: str) -> str:
    """Confirm that Apprise's mail plugin connects to ``host`` or a provider.

    Apprise's email parser discards an authority it does not accept as a
    hostname (an empty port, an underscore) and then takes the SMTP host from
    the domain of an e-mail address in the userinfo or ``?user=``. The URL
    authority would then no longer be the host Apprise connects to. The
    plugin is therefore built the way ``Apprise.instantiate`` builds it
    (``url_to_dict`` and the constructor, no network I/O), and only two
    outcomes are accepted: Apprise keeps ``host`` and connects to it, or it
    connects to the fixed SMTP host of one of its built-in providers selected
    by ``host``. A URL whose plugin cannot be built is returned unchanged:
    ``Apprise.add`` builds the same plugin, rejects the URL and sends nothing.
    """
    from apprise import AppriseAsset
    from apprise.plugins import url_to_dict
    from apprise.plugins.email import NotifyEmail

    results = url_to_dict(url)
    if not results or str(results.get("schema", "")).lower() != "mailto":
        raise NotificationDestinationError(
            "mailto URL is not accepted by Apprise"
        )
    parsed_host = results.get("host")
    if not parsed_host or _apprise_host(parsed_host) != host:
        raise NotificationDestinationError("mail host is ambiguous")
    try:
        plugin = NotifyEmail(
            **{
                **results,
                "asset": AppriseAsset(async_mode=False, http_redirects=False),
                "tag": set(),
            }
        )
    except Exception:  # Apprise.instantiate treats any error as reject
        return host
    smtp_host = _apprise_host(getattr(plugin, "smtp_host", None))
    if smtp_host != host and smtp_host not in _email_template_smtp_hosts():
        raise NotificationDestinationError("mail host is ambiguous")
    return host


def _smtp_override_host(value: str) -> str:
    if any(character.isspace() for character in value):
        raise NotificationDestinationError("invalid smtp override")
    try:
        parsed = urlsplit(f"//{value}")
        port = parsed.port
    except ValueError as exc:
        raise NotificationDestinationError("invalid smtp override") from exc
    if (
        parsed.username is not None
        or parsed.password is not None
        or port is not None
        or parsed.path
        or parsed.query
        or parsed.fragment
    ):
        raise NotificationDestinationError("invalid smtp override")
    if "%" in value:
        raise NotificationDestinationError("invalid smtp override")
    return _required_host(parsed)


def parse_notification_destination(url: str) -> NotificationDestination:
    """Return the effective outbound destination for one Apprise URL."""
    if not isinstance(url, str) or not url.strip():
        raise NotificationDestinationError("notification URL is empty")
    try:
        normalized_url = url.strip()
        parsed = urlsplit(normalized_url)
    except ValueError as exc:
        raise NotificationDestinationError(
            "notification URL is malformed"
        ) from exc

    scheme = parsed.scheme.lower()
    match scheme:
        case "http" | "https":
            return NotificationDestination(
                NotificationDestinationKind.HTTP,
                _confirm_apprise_ipv6_host(
                    normalized_url, _required_host(parsed)
                ),
            )
        case (
            "json"
            | "xml"
            | "form"
            | "gotify"
            | "signal"
            | "mattermost"
            | "rocketchat"
        ):
            return NotificationDestination(
                NotificationDestinationKind.AUTHORITY_PLUGIN,
                _confirm_apprise_ipv6_host(
                    normalized_url, _plugin_host(parsed)
                ),
            )
        case "discord" | "slack" | "tgram" | "pushover" | "teams":
            if not parsed.netloc:
                raise NotificationDestinationError("vendor token is missing")
            return NotificationDestination(
                NotificationDestinationKind.VENDOR_PLUGIN,
                None,
            )
        case "ntfy" | "ntfys":
            # A duplicate or empty mode is ambiguous; fail before Apprise
            # silently picks one spelling.
            mode = _query_value(normalized_url, "mode")
            if mode not in (None, "cloud", "private"):
                raise NotificationDestinationError("unsupported ntfy mode")
            return _apprise_ntfy_destination(
                normalized_url, _plugin_host(parsed)
            )
        case "mailto":
            smtp_override = _query_value(normalized_url, "smtp")
            effective_host = (
                _smtp_override_host(smtp_override)
                if smtp_override is not None
                else _apprise_mail_host(normalized_url, _plugin_host(parsed))
            )
            return NotificationDestination(
                NotificationDestinationKind.AUTHORITY_PLUGIN,
                effective_host,
            )
        case "matrix":
            mode = _query_value(normalized_url, "mode")
            if mode not in (
                None,
                "off",
                "matrix",
                "slack",
                "hookshot",
                "t2bot",
            ):
                raise NotificationDestinationError("unsupported matrix mode")
            # Apprise alone decides between t2bot (a fixed POST to
            # webhooks.t2bot.io, with the authority used as the token) and a
            # homeserver/webhook mode that sends to the authority host.
            # Mirroring that rule here drifted from Apprise and refused
            # documented forms such as ``user@<token>`` and ``/#room``.
            apprise_t2bot, apprise_host = _apprise_matrix_details(
                normalized_url
            )
            if apprise_t2bot:
                token = parsed.netloc.rpartition("@")[2]
                if not re.fullmatch(r"[A-Za-z0-9]{64}", token):
                    raise NotificationDestinationError(
                        "vendor token is invalid"
                    )
                return NotificationDestination(
                    NotificationDestinationKind.VENDOR_PLUGIN,
                    None,
                )
            host = _plugin_host(parsed)
            if _apprise_host(apprise_host) != host:
                raise NotificationDestinationError("matrix host is ambiguous")
            return NotificationDestination(
                NotificationDestinationKind.AUTHORITY_PLUGIN,
                host,
            )
        case _:
            raise NotificationDestinationError(
                "unsupported notification scheme"
            )