aboutsummaryrefslogtreecommitdiff
path: root/tests/test_net.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-12 19:57:08 +0100
committerDennis Fink2026-11-12 19:57:08 +0100
commit4bd1d9e348918b2e55db47a1ac3489d035cf55e2 (patch)
treef7ed41a374a8a4136792039a262dc62aa8cf8c59 /tests/test_net.py
parent7035e9f9467684cccae2b7ff3faaa62e6a9f91aa (diff)
downloadwebmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.tar.gz
webmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.zip
refactor(core): split modules by responsibility
Break up monolithic views, task helpers, networking, CLI, and application extension setup so each module owns a focused concern. Reorganize the test suite around the new boundaries while preserving existing Webmention behavior and coverage.
Diffstat (limited to '')
-rw-r--r--tests/test_net.py (renamed from tests/test_public_transport.py)124
1 files changed, 117 insertions, 7 deletions
diff --git a/tests/test_public_transport.py b/tests/test_net.py
index df7a1c3..928a0d5 100644
--- a/tests/test_public_transport.py
+++ b/tests/test_net.py
@@ -4,20 +4,26 @@
"""Security regression tests for DNS-pinned network connections."""
+import ipaddress
import socket
-from unittest.mock import Mock
+from unittest.mock import Mock, PropertyMock, patch
import httpcore
import httpx
import pytest
+from flask import Flask
-from webmentions_ssg.public_transport import (
+from webmentions_ssg.net import (
+ AddressResolutionError,
DeadlineStream,
NetworkDeadline,
PublicHTTPTransport,
PublicNetworkBackend,
+ UnsafeAddressError,
+ is_http_url,
+ is_public_address,
+ public_addresses,
)
-from webmentions_ssg.url_security import UnsafeAddressError
@pytest.mark.parametrize("host", ["127.0.0.1", "169.254.169.254", "::1"])
@@ -34,7 +40,7 @@ def test_backend_rejects_private_ip_without_socket(
def test_rebinding_cannot_change_socket_peer(monkeypatch: pytest.MonkeyPatch) -> None:
"""The address is resolved exactly once and the socket sees only that IP."""
dns = Mock(return_value=("93.184.216.34",))
- monkeypatch.setattr("webmentions_ssg.public_transport.public_addresses", dns)
+ monkeypatch.setattr("webmentions_ssg.net.public_addresses", dns)
socket_instance = Mock()
create_socket = Mock(return_value=socket_instance)
monkeypatch.setattr(socket, "socket", create_socket)
@@ -110,9 +116,7 @@ def test_deadline_clips_each_read_and_stops_trickle(
monkeypatch: pytest.MonkeyPatch,
) -> None:
clock = [100.0]
- monkeypatch.setattr(
- "webmentions_ssg.public_transport.time.monotonic", lambda: clock[0]
- )
+ monkeypatch.setattr("webmentions_ssg.net.time.monotonic", lambda: clock[0])
fake_stream = Mock()
fake_stream.read.return_value = b"x"
stream = DeadlineStream(fake_stream, NetworkDeadline(5.0))
@@ -154,3 +158,109 @@ def test_redirects_go_through_same_destination_guard(
client.get("http://source.example/post")
assert checked_hosts == ["source.example", "internal.example"]
+
+
+@pytest.mark.parametrize(
+ ("url", "expected"),
+ [
+ pytest.param("http://example.com/", True, id="http"),
+ pytest.param("https://example.com/path", True, id="https"),
+ pytest.param("HTTP://EXAMPLE.COM/", True, id="scheme-case-insensitive"),
+ pytest.param("https://example.com:8443/path", True, id="port"),
+ pytest.param("http://[2001:db8::1]/", True, id="ipv6"),
+ pytest.param("ftp://example.com/", False, id="ftp"),
+ pytest.param("mailto:example@example.com", False, id="mailto"),
+ pytest.param("/relative/url", False, id="relative"),
+ pytest.param("//example.com/path", False, id="scheme-relative"),
+ pytest.param("https:///missing-host", False, id="missing-host"),
+ pytest.param("", False, id="empty"),
+ pytest.param("http://[::1", False, id="malformed"),
+ ],
+)
+def test_is_http_url(url: str, expected: bool) -> None:
+ assert is_http_url(url) is expected
+
+
+def test_ip_literal_does_not_use_dns() -> None:
+ with patch("webmentions_ssg.net.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()
+
+
+@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.net.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.net.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.net.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.net.dns.resolver.resolve_name") as resolve:
+ resolve.return_value.addresses.return_value = []
+ with pytest.raises(AddressResolutionError):
+ public_addresses("empty.example")
+
+
+@pytest.mark.parametrize(
+ ("status", "expected"),
+ [
+ pytest.param(200, False, id="success"),
+ pytest.param(400, False, id="client-error"),
+ pytest.param(408, True, id="request-timeout"),
+ pytest.param(425, True, id="too-early"),
+ pytest.param(429, True, id="too-many-requests"),
+ pytest.param(500, True, id="server-error-lower-bound"),
+ pytest.param(599, True, id="server-error-upper-bound"),
+ pytest.param(600, False, id="outside-server-error-range"),
+ ],
+)
+def test_temporary_http_status(app: Flask, status: int, expected: bool) -> None:
+ _ = app
+ from webmentions_ssg.tasks import discovery
+
+ assert discovery.is_temporary_status(status) is expected