From 4bd1d9e348918b2e55db47a1ac3489d035cf55e2 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Thu, 12 Nov 2026 19:57:08 +0100 Subject: 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. --- tests/test_net.py | 266 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 266 insertions(+) create mode 100644 tests/test_net.py (limited to 'tests/test_net.py') diff --git a/tests/test_net.py b/tests/test_net.py new file mode 100644 index 0000000..928a0d5 --- /dev/null +++ b/tests/test_net.py @@ -0,0 +1,266 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Security regression tests for DNS-pinned network connections.""" + +import ipaddress +import socket +from unittest.mock import Mock, PropertyMock, patch + +import httpcore +import httpx +import pytest +from flask import Flask + +from webmentions_ssg.net import ( + AddressResolutionError, + DeadlineStream, + NetworkDeadline, + PublicHTTPTransport, + PublicNetworkBackend, + UnsafeAddressError, + is_http_url, + is_public_address, + public_addresses, +) + + +@pytest.mark.parametrize("host", ["127.0.0.1", "169.254.169.254", "::1"]) +def test_backend_rejects_private_ip_without_socket( + monkeypatch: pytest.MonkeyPatch, host: str +) -> None: + create_socket = Mock(side_effect=AssertionError("must not open socket")) + monkeypatch.setattr(socket, "socket", create_socket) + with pytest.raises(UnsafeAddressError): + PublicNetworkBackend(NetworkDeadline(5)).connect_tcp(host, 80) + create_socket.assert_not_called() + + +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.net.public_addresses", dns) + socket_instance = Mock() + create_socket = Mock(return_value=socket_instance) + monkeypatch.setattr(socket, "socket", create_socket) + monkeypatch.setattr( + socket, "getaddrinfo", Mock(side_effect=AssertionError("second resolution")) + ) + + stream = PublicNetworkBackend(NetworkDeadline(5)).connect_tcp( + "rebinding.example", 443, timeout=2.0 + ) + assert isinstance(stream, DeadlineStream) + assert stream.get_extra_info("socket") is socket_instance + dns.assert_called_once() + assert dns.call_args.args == ("rebinding.example",) + assert 0 < dns.call_args.kwargs["lifetime"] <= 2.0 + socket_instance.connect.assert_called_once_with(("93.184.216.34", 443)) + create_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM) + + +class FakeStream(httpcore.NetworkStream): + """In-memory TCP/TLS stream so HTTP framing and SNI can be tested.""" + + def __init__(self, response: bytes) -> None: + self.response = response + self.writes = bytearray() + self.sni: str | None = None + + def read(self, max_bytes: int, timeout: float | None = None) -> bytes: + part, self.response = self.response[:max_bytes], self.response[max_bytes:] + return part + + def write(self, buffer: bytes, timeout: float | None = None) -> None: + self.writes.extend(buffer) + + def start_tls( + self, + ssl_context, + server_hostname: str | None = None, + timeout: float | None = None, + ) -> httpcore.NetworkStream: + self.sni = server_hostname + return self + + def get_extra_info(self, info: str): + return None + + def close(self) -> None: + pass + + +@pytest.mark.parametrize("scheme", ["http", "https"]) +def test_transport_keeps_original_http_host_and_tls_sni( + monkeypatch: pytest.MonkeyPatch, scheme: str +) -> None: + stream = FakeStream(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK") + backend = Mock(return_value=stream) + monkeypatch.setattr(PublicNetworkBackend, "connect_tcp", backend) + + with httpx.Client(transport=PublicHTTPTransport(5.0), trust_env=False) as client: + response = client.get(f"{scheme}://source.example/path") + + assert response.status_code == 200 + assert response.text == "OK" + assert b"Host: source.example\r\n" in stream.writes + assert b"GET /path HTTP/1.1\r\n" in stream.writes + assert stream.sni == ("source.example" if scheme == "https" else None) + backend.assert_called_once() + assert backend.call_args.kwargs["host"] == "source.example" + assert backend.call_args.kwargs["port"] == (443 if scheme == "https" else 80) + + +def test_deadline_clips_each_read_and_stops_trickle( + monkeypatch: pytest.MonkeyPatch, +) -> None: + clock = [100.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)) + + clock[0] = 103.0 + assert stream.read(1, timeout=5.0) == b"x" + assert fake_stream.read.call_args.kwargs["timeout"] == pytest.approx(2.0) + + clock[0] = 105.1 + with pytest.raises(httpcore.ReadTimeout, match="Overall HTTP request deadline"): + stream.read(1, timeout=5.0) + assert fake_stream.read.call_count == 1 + + +def test_redirects_go_through_same_destination_guard( + monkeypatch: pytest.MonkeyPatch, +) -> None: + redirect = FakeStream( + b"HTTP/1.1 302 Found\r\nLocation: http://internal.example/\r\n" + b"Content-Length: 0\r\n\r\n" + ) + checked_hosts: list[str] = [] + + def connect( + self: PublicNetworkBackend, host: str, port: int, **kwargs: object + ) -> httpcore.NetworkStream: + checked_hosts.append(host) + if host == "internal.example": + raise UnsafeAddressError("Non-public network address") + return redirect + + monkeypatch.setattr(PublicNetworkBackend, "connect_tcp", connect) + with ( + httpx.Client( + transport=PublicHTTPTransport(5.0), follow_redirects=True + ) as client, + pytest.raises(UnsafeAddressError, match="Non-public"), + ): + 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 -- cgit v1.3.1