aboutsummaryrefslogtreecommitdiff
path: root/tests/test_url_security.py
diff options
context:
space:
mode:
authorDennis Fink2026-10-30 20:31:43 +0100
committerDennis Fink2026-10-30 20:31:43 +0100
commit13a9d9ddce09230017699bc34ac3bf08ed2f84ed (patch)
tree3715bd13439a61a8cc6896581811a30b62ac1b16 /tests/test_url_security.py
parent8c561e702ed15022b17eae81e4185448c021e1cb (diff)
downloadwebmentions-ssg-13a9d9ddce09230017699bc34ac3bf08ed2f84ed.tar.gz
webmentions-ssg-13a9d9ddce09230017699bc34ac3bf08ed2f84ed.zip
fix(core): prevent DNS rebinding in HTTP requests
Pin outbound connections to validated public IP addresses so DNS changes cannot redirect receiver or sender requests onto private or link-local networks. Apply the same protection across redirects, add an overall request deadline, and keep DNS resolution out of synchronous endpoint form validation.
Diffstat (limited to '')
-rw-r--r--tests/test_url_security.py29
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")