diff options
| author | Dennis Fink | 2026-11-17 14:44:00 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-17 14:44:00 +0100 |
| commit | 227092b57acc886469e16bcc4c0d3256e86d3d1e (patch) | |
| tree | c1de8d5f8f3a37562e482f84e8017213c08a05d6 /webmentions_ssg/hostname.py | |
| parent | f98b0e446b41325c494bd97900b7bbf730a7d67e (diff) | |
| download | webmentions-ssg-227092b57acc886469e16bcc4c0d3256e86d3d1e.tar.gz webmentions-ssg-227092b57acc886469e16bcc4c0d3256e86d3d1e.zip | |
feat(receiver): add source domain access rules
Add persistent source block and allow lists with authenticated
management, single-row and bulk blocking actions, and optional
fail-closed allowlist mode.
Reject disallowed sources before enqueueing and recheck policy in
verification workers, while sharing canonical hostname normalization
across receiver, sender, and configuration paths.
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/hostname.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/webmentions_ssg/hostname.py b/webmentions_ssg/hostname.py new file mode 100644 index 0000000..4e55069 --- /dev/null +++ b/webmentions_ssg/hostname.py @@ -0,0 +1,61 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Canonical hostname normalization shared by URL, config, and source rules.""" + +import ipaddress +import re +from urllib.parse import urlsplit + +LABEL = re.compile(r"[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\Z") +PATTERN_LABEL = re.compile(r"[a-z0-9*?\[\]-]+\Z") + + +def normalize_hostname( + value: str, *, from_url: bool = False, allow_patterns: bool = False +) -> str: + """Return the canonical IDNA hostname or IP literal. + + Bare hostnames are required by default. ``from_url=True`` extracts a + hostname from a URL, including bracketed IPv6 literals. ``allow_patterns`` + permits sender-side ignored-hostname glob patterns; it must not be used + for receiver allow/block entries or allowed target hosts. + + Whitespace and malformed hosts are rejected. Callers normalizing config + collections may strip surrounding whitespace before calling this function. + + :raises ValueError: If the hostname is missing or malformed. + """ + if not isinstance(value, str) or not value or value != value.strip(): + raise ValueError("Enter a hostname, without a URL or whitespace.") + + if from_url: + try: + hostname = urlsplit(value).hostname + except ValueError as exc: + raise ValueError("Invalid hostname.") from exc + if hostname is None: + raise ValueError("Invalid hostname.") + else: + hostname = value + + hostname = hostname.lower().rstrip(".") + try: + return str(ipaddress.ip_address(hostname)) + except ValueError: + pass + + try: + hostname = hostname.encode("idna").decode("ascii").lower() + except (UnicodeError, ValueError) as exc: + raise ValueError("Invalid hostname.") from exc + + label_pattern = PATTERN_LABEL if allow_patterns else LABEL + if ( + not hostname + or len(hostname) > 253 + or any(not label_pattern.fullmatch(label) for label in hostname.split(".")) + ): + raise ValueError("Invalid hostname.") + return hostname |
