diff options
Diffstat (limited to 'tests/test_url_security.py')
| -rw-r--r-- | tests/test_url_security.py | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/tests/test_url_security.py b/tests/test_url_security.py index 80b45e9..d25589f 100644 --- a/tests/test_url_security.py +++ b/tests/test_url_security.py @@ -9,6 +9,8 @@ import pytest from webmentions_ssg.url_security import ( AddressResolutionError, + UnsafeAddressError, + public_addresses, is_http_url, is_public_url, is_safe_next_url, @@ -114,7 +116,7 @@ def test_is_public_url_returns_true_for_public_addresses( def test_is_public_url_raises_for_resolution_failure(resolve_name: Mock) -> None: with pytest.raises(AddressResolutionError, match="Could not resolve hostname"): is_public_url("https://nonexistent.example/") - resolve_name.assert_called_once_with("nonexistent.example") + resolve_name.assert_called_once_with("nonexistent.example", lifetime=None) @patch("webmentions_ssg.url_security.dns.resolver.resolve_name") @@ -122,3 +124,28 @@ def test_is_public_url_raises_when_url_has_no_hostname(resolve_name: Mock) -> No with pytest.raises(ValueError, match="No hostname was specified"): is_public_url("/relative/url") resolve_name.assert_not_called() + + +def test_ip_literal_does_not_use_dns() -> None: + with patch("webmentions_ssg.url_security.dns.resolver.resolve_name") as dns_resolve: + with pytest.raises(UnsafeAddressError): + public_addresses("169.254.169.254") + with pytest.raises(UnsafeAddressError): + public_addresses("::1") + assert public_addresses("8.8.8.8") == ("8.8.8.8",) + dns_resolve.assert_not_called() + + +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"] + with pytest.raises(UnsafeAddressError, match="Non-public"): + public_addresses("rebinding.example", lifetime=1.5) + resolve.assert_called_once_with("rebinding.example", lifetime=1.5) + + +def test_public_addresses_rejects_empty_dns_result() -> None: + with patch("webmentions_ssg.url_security.dns.resolver.resolve_name") as resolve: + resolve.return_value.addresses.return_value = [] + with pytest.raises(AddressResolutionError): + public_addresses("empty.example") |
