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 /tests/test_url_security.py | |
| 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/test_url_security.py | 46 |
1 files changed, 44 insertions, 2 deletions
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"] |
