diff options
| author | Dennis Fink | 2026-10-31 17:49:44 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-31 17:49:44 +0100 |
| commit | f7c0691e0da84644ea8b2b83ad5574fe179da59b (patch) | |
| tree | 6993fb2c0b9e5f973623b616128cc793da8d1b5b | |
| parent | 6924ad7e52033365c55d5afacdd916c502cc80cb (diff) | |
| download | webmentions-ssg-f7c0691e0da84644ea8b2b83ad5574fe179da59b.tar.gz webmentions-ssg-f7c0691e0da84644ea8b2b83ad5574fe179da59b.zip | |
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.
Diffstat (limited to '')
| -rw-r--r-- | tests/forms/test_validators.py | 16 | ||||
| -rw-r--r-- | tests/test_url_security.py | 46 | ||||
| -rw-r--r-- | webmentions_ssg/forms/validators.py | 4 | ||||
| -rw-r--r-- | webmentions_ssg/url_security.py | 28 |
4 files changed, 89 insertions, 5 deletions
diff --git a/tests/forms/test_validators.py b/tests/forms/test_validators.py index 10796e9..ae4c8f2 100644 --- a/tests/forms/test_validators.py +++ b/tests/forms/test_validators.py @@ -12,6 +12,7 @@ from wtforms import Form, StringField, ValidationError from webmentions_ssg.forms.validators import ( AllowedHostname, NotEqualTo, + PublicIPLiteral, PublicURL, URLMaxBytes, ) @@ -119,6 +120,21 @@ def test_public_url_uses_custom_message(monkeypatch: pytest.MonkeyPatch) -> None PublicURL("Public URL required")(form, form.url) +@pytest.mark.parametrize( + "address", + ["64:ff9b::a9fe:a9fe", "::10.0.0.1", "2002:a9fe:a9fe::1", "::ffff:169.254.169.254"], +) +def test_public_ip_literal_rejects_ipv6_transition_addresses(address: str) -> None: + form = URLForm(MultiDict({"url": f"https://[{address}]/"})) + with pytest.raises(ValidationError, match="URL must use a public address"): + PublicIPLiteral()(form, form.url) + + +def test_public_ip_literal_accepts_public_ipv6_address() -> None: + form = URLForm(MultiDict({"url": "https://[2606:2800:220:1:248:1893:25c8:1946]/"})) + PublicIPLiteral()(form, form.url) + + def test_allowed_hostname_handles_invalid_ipv6(app: Flask) -> None: form = URLForm(MultiDict({"url": "http://[::1"})) with app.app_context(), pytest.raises(ValidationError, match="Invalid input"): diff --git a/tests/test_url_security.py b/tests/test_url_security.py index d25589f..95c2bd9 100644 --- a/tests/test_url_security.py +++ b/tests/test_url_security.py @@ -2,7 +2,8 @@ # # SPDX-License-Identifier: BSD-3-Clause -from unittest.mock import Mock, patch +import ipaddress +from unittest.mock import Mock, PropertyMock, patch import dns.resolver import pytest @@ -10,10 +11,11 @@ import pytest from webmentions_ssg.url_security import ( AddressResolutionError, UnsafeAddressError, - public_addresses, is_http_url, + is_public_address, is_public_url, is_safe_next_url, + public_addresses, ) @@ -136,6 +138,46 @@ def test_ip_literal_does_not_use_dns() -> None: dns_resolve.assert_not_called() +@pytest.mark.parametrize( + "address", + [ + pytest.param("64:ff9b::a9fe:a9fe", id="nat64-metadata"), + pytest.param("64:ff9b::808:808", id="nat64-public-ipv4"), + pytest.param("::10.0.0.1", id="ipv4-compatible-private"), + pytest.param("::8.8.8.8", id="ipv4-compatible-public"), + pytest.param("2002:a9fe:a9fe::1", id="6to4-metadata"), + pytest.param("2002:0a00:0001::1", id="6to4-private"), + pytest.param("::ffff:10.0.0.1", id="mapped-private"), + pytest.param("::ffff:169.254.169.254", id="mapped-metadata"), + ], +) +def test_public_addresses_rejects_ipv6_transition_literals(address: str) -> None: + with patch("webmentions_ssg.url_security.dns.resolver.resolve_name") as resolve: + with pytest.raises(UnsafeAddressError, match="Non-public"): + public_addresses(address) + resolve.assert_not_called() + + +@pytest.mark.parametrize( + "address", ["64:ff9b::a9fe:a9fe", "::10.0.0.1", "2002:a9fe:a9fe::1"] +) +def test_public_addresses_rejects_transition_addresses_in_dns(address: str) -> None: + with patch("webmentions_ssg.url_security.dns.resolver.resolve_name") as resolve: + resolve.return_value.addresses.return_value = ["93.184.216.34", address] + with pytest.raises(UnsafeAddressError, match="Non-public"): + public_addresses("transition.example") + + +@pytest.mark.parametrize("address", ["2002:0a00:0001::1", "::ffff:10.0.0.1"]) +def test_private_embedded_ipv4_rejected_even_if_ipv6_is_global(address: str) -> None: + # Simulate a Python version treating the outer transition address as global. + with patch.object( + ipaddress.IPv6Address, "is_global", new_callable=PropertyMock + ) as global_: + global_.return_value = True + assert not is_public_address(ipaddress.ip_address(address)) + + def test_public_addresses_rejects_mixed_dns_results() -> None: with patch("webmentions_ssg.url_security.dns.resolver.resolve_name") as resolve: resolve.return_value.addresses.return_value = ["93.184.216.34", "127.0.0.1"] 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) |
