From f7c0691e0da84644ea8b2b83ad5574fe179da59b Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sat, 31 Oct 2026 17:49:44 +0100 Subject: fix(core): reject IPv6 transition addresses Harden public-address validation against NAT64, IPv4-compatible, 6to4, and IPv4-mapped IPv6 forms that can encapsulate private or metadata destinations. Apply the same check to literal form validation and DNS-resolved addresses so SSRF protections do not rely on Python's `is_global` behavior alone. --- webmentions_ssg/forms/validators.py | 4 ++-- webmentions_ssg/url_security.py | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) (limited to 'webmentions_ssg') diff --git a/webmentions_ssg/forms/validators.py b/webmentions_ssg/forms/validators.py index 36f36ce..c1f1c97 100644 --- a/webmentions_ssg/forms/validators.py +++ b/webmentions_ssg/forms/validators.py @@ -11,7 +11,7 @@ from flask import current_app from wtforms import Field, ValidationError from wtforms.form import BaseForm -from ..url_security import AddressResolutionError, is_public_url +from ..url_security import AddressResolutionError, is_public_address, is_public_url class URLMaxBytes: @@ -174,5 +174,5 @@ class PublicIPLiteral: except ValueError: return # Named hosts are resolved only during the background fetch. - if not address.is_global: + if not is_public_address(address): raise ValidationError(field.gettext("URL must use a public address.")) diff --git a/webmentions_ssg/url_security.py b/webmentions_ssg/url_security.py index 4e7940b..c4324e0 100644 --- a/webmentions_ssg/url_security.py +++ b/webmentions_ssg/url_security.py @@ -19,6 +19,32 @@ 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, ...]: @@ -48,7 +74,7 @@ def public_addresses( if not addresses: raise AddressResolutionError(f"Could not resolve hostname {hostname!r}") - if any(not address.is_global for address in addresses): + 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) -- cgit v1.3.1