aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/url_security.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-12 19:57:08 +0100
committerDennis Fink2026-11-12 19:57:08 +0100
commit4bd1d9e348918b2e55db47a1ac3489d035cf55e2 (patch)
treef7ed41a374a8a4136792039a262dc62aa8cf8c59 /webmentions_ssg/url_security.py
parent7035e9f9467684cccae2b7ff3faaa62e6a9f91aa (diff)
downloadwebmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.tar.gz
webmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.zip
refactor(core): split modules by responsibility
Break up monolithic views, task helpers, networking, CLI, and application extension setup so each module owns a focused concern. Reorganize the test suite around the new boundaries while preserving existing Webmention behavior and coverage.
Diffstat (limited to 'webmentions_ssg/url_security.py')
-rw-r--r--webmentions_ssg/url_security.py133
1 files changed, 0 insertions, 133 deletions
diff --git a/webmentions_ssg/url_security.py b/webmentions_ssg/url_security.py
deleted file mode 100644
index c4324e0..0000000
--- a/webmentions_ssg/url_security.py
+++ /dev/null
@@ -1,133 +0,0 @@
-# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
-#
-# SPDX-License-Identifier: BSD-3-Clause
-
-"""Validate URL schemes and publicly routable network destinations."""
-
-import ipaddress
-from urllib.parse import urlsplit
-
-import dns.exception
-import dns.resolver
-
-
-class AddressResolutionError(Exception):
- """A hostname could not be resolved (potentially transient)."""
-
-
-class UnsafeAddressError(ValueError):
- """A hostname or IP literal identifies a non-public network address."""
-
-
-_NAT64_PREFIX = ipaddress.IPv6Network("64:ff9b::/96")
-_IPV4_COMPATIBLE_PREFIX = ipaddress.IPv6Network("::/96")
-
-
-def is_public_address(address: ipaddress.IPv4Address | ipaddress.IPv6Address) -> bool:
- """Reject non-public addresses, including IPv4 hidden in IPv6 forms.
-
- ``is_global`` alone does not reliably exclude IPv6 transition addresses
- across Python versions or networks with NAT64/6to4 routing.
- """
- if isinstance(address, ipaddress.IPv6Address):
- # The well-known NAT64 prefix can translate an otherwise global IPv6
- # address into a private IPv4 destination. IPv4-compatible addresses
- # similarly embed IPv4 in ::/96, even without ipv4_mapped metadata.
- if address in _NAT64_PREFIX or address in _IPV4_COMPATIBLE_PREFIX:
- return False
-
- # 6to4 and IPv4-mapped IPv6 addresses expose their embedded IPv4
- # destination through ipaddress; validate that destination as well.
- for embedded in (address.sixtofour, address.ipv4_mapped):
- if embedded is not None and not embedded.is_global:
- return False
-
- return address.is_global
-
-
-def public_addresses(
- hostname: str, *, lifetime: float | None = None
-) -> tuple[str, ...]:
- """Resolve *once* and return only checked, globally routable IP addresses.
-
- A DNS name with a mixture of public and private answers is rejected in its
- entirety. IP literals are handled without DNS, including IPv6 literals.
- The returned strings must be used for the actual TCP connection; checking
- them and then connecting to ``hostname`` reintroduces DNS rebinding.
- """
- try:
- literal = ipaddress.ip_address(hostname)
- except ValueError:
- literal = None
-
- if literal is not None:
- addresses = (literal,)
- else:
- try:
- answers = dns.resolver.resolve_name(hostname, lifetime=lifetime)
- addresses = tuple(ipaddress.ip_address(a) for a in answers.addresses())
- except (dns.exception.DNSException, ValueError) as exc:
- raise AddressResolutionError(
- f"Could not resolve hostname {hostname!r}"
- ) from exc
-
- if not addresses:
- raise AddressResolutionError(f"Could not resolve hostname {hostname!r}")
-
- if any(not is_public_address(address) for address in addresses):
- raise UnsafeAddressError(f"Non-public network address for {hostname!r}")
-
- return tuple(str(address) for address in addresses)
-
-
-def is_public_url(url: str) -> bool:
- """Return whether the URL resolves entirely to public IP addresses.
-
- This is useful for diagnostics, but **not** sufficient to authorize a
- request: network connections must use the exact validated IP address.
- """
- hostname = urlsplit(url).hostname
-
- if hostname is None:
- raise ValueError("No hostname was specified")
-
- try:
- public_addresses(hostname)
- except UnsafeAddressError:
- return False
- return True
-
-
-def is_http_url(url: str) -> bool:
- """
- Check whether a URL is an absolute HTTP or HTTPS URL.
-
- :param url: URL to validate.
- :return: Whether the URL uses HTTP or HTTPS and contains a hostname.
- """
- try:
- parsed = urlsplit(url)
- except ValueError:
- return False
-
- return parsed.scheme.lower() in {"http", "https"} and parsed.hostname is not None
-
-
-def is_safe_next_url(next_url: str) -> bool:
- """Allow redirects only to paths on this site."""
- # Check the original string too: urlsplit("///evil.example").path is
- # "/evil.example", even though browsers can treat the original as external.
- if "\\" in next_url or not next_url.startswith("/") or next_url.startswith("//"):
- return False
-
- try:
- target = urlsplit(next_url)
- except ValueError:
- return False
-
- return (
- not target.scheme
- and not target.netloc
- and target.path.startswith("/")
- and not target.path.startswith("//")
- )