# SPDX-FileCopyrightText: 2026 Dennis Fink # # SPDX-License-Identifier: BSD-3-Clause """Security regression tests for DNS-pinned network connections.""" import ipaddress import socket import dns.exception from http import HTTPStatus from unittest.mock import Mock, PropertyMock, patch import httpcore import httpx import pytest from flask import Flask from webmentions_ssg.net import ( AddressResolutionError, DeadlineStream, PublicHTTPTransport, PublicNetworkBackend, RequestDeadline, ResponseTooLargeError, UnsafeAddressError, is_http_url, is_public_address, media_type_of, read_limited_body, resolve_public_addresses, ) 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("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(RequestDeadline(5)).connect_tcp(host, 80) create_socket.assert_not_called() def test_rebinding_cannot_change_socket_peer(monkeypatch: pytest.MonkeyPatch) -> None: """Verify that DNS rebinding cannot change the connected socket peer. :param monkeypatch: Pytest fixture for replacing the DNS resolver and socket calls. """ dns = Mock(return_value=("93.184.216.34",)) monkeypatch.setattr("webmentions_ssg.net.resolve_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(RequestDeadline(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) def test_backend_closes_failed_socket_and_tries_next_address( monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr( "webmentions_ssg.net.resolve_public_addresses", Mock(return_value=("93.184.216.34", "8.8.8.8")), ) first, second = Mock(), Mock() first.connect.side_effect = TimeoutError("connection timed out") monkeypatch.setattr(socket, "socket", Mock(side_effect=[first, second])) stream = PublicNetworkBackend(RequestDeadline(5)).connect_tcp("example.com", 443) assert isinstance(stream, DeadlineStream) first.close.assert_called_once_with() second.close.assert_not_called() first.connect.assert_called_once_with(("93.184.216.34", 443)) second.connect.assert_called_once_with(("8.8.8.8", 443)) stream.close() second.close.assert_called_once_with() @pytest.mark.parametrize( ("failure", "expected"), [ pytest.param(TimeoutError("timed out"), httpcore.ConnectTimeout, id="timeout"), pytest.param(OSError("refused"), httpcore.ConnectError, id="os-error"), pytest.param(ValueError("unexpected"), ValueError, id="unexpected"), ], ) def test_backend_closes_socket_on_failure( monkeypatch: pytest.MonkeyPatch, failure: Exception, expected: type[Exception] ) -> None: sock = Mock() sock.connect.side_effect = failure monkeypatch.setattr(socket, "socket", Mock(return_value=sock)) with pytest.raises(expected, match=str(failure)): PublicNetworkBackend(RequestDeadline(5)).connect_tcp("8.8.8.8", 443) sock.close.assert_called_once_with() @pytest.mark.parametrize("mode", ["declared", "streamed"]) def test_read_limited_body_raises_specific_size_error(mode: str) -> None: response = ( httpx.Response( HTTPStatus.OK, headers={"Content-Length": "11"}, content=b"x" * 11 ) if mode == "declared" else httpx.Response(HTTPStatus.OK, stream=httpx.ByteStream(b"x" * 11)) ) with pytest.raises(ResponseTooLargeError, match="Source document is too large"): read_limited_body(response, 10, label="Source") @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 == HTTPStatus.OK 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, RequestDeadline(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): resolve_public_addresses("169.254.169.254") with pytest.raises(UnsafeAddressError): resolve_public_addresses("::1") assert resolve_public_addresses("8.8.8.8") == ("8.8.8.8",) dns_resolve.assert_not_called() def test_public_addresses_resolves_hostname_once() -> None: with patch("webmentions_ssg.net.dns.resolver.resolve_name") as resolve: resolve.return_value.addresses.return_value = ["93.184.216.34", "8.8.8.8"] assert resolve_public_addresses("example.com", lifetime=1.5) == ( "93.184.216.34", "8.8.8.8", ) resolve.assert_called_once_with("example.com", lifetime=1.5) @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"): resolve_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"): resolve_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"): resolve_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): resolve_public_addresses("empty.example") @pytest.mark.parametrize( ("status", "expected"), [ pytest.param(HTTPStatus.OK, False, id="success"), pytest.param(HTTPStatus.BAD_REQUEST, False, id="client-error"), pytest.param(HTTPStatus.REQUEST_TIMEOUT, True, id="request-timeout"), pytest.param(HTTPStatus.TOO_EARLY, True, id="too-early"), pytest.param(HTTPStatus.TOO_MANY_REQUESTS, True, id="too-many-requests"), pytest.param( HTTPStatus.INTERNAL_SERVER_ERROR, 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_is_retryable_status(app: Flask, status: int, expected: bool) -> None: _ = app from webmentions_ssg.tasks import endpoint_discovery assert endpoint_discovery.is_retryable_status(status) is expected @pytest.mark.parametrize( ("headers", "expected"), [ pytest.param({}, "", id="missing"), pytest.param( {"Content-Type": " Text/HTML ; charset=UTF-8"}, "text/html", id="params" ), pytest.param( {"content-type": "APPLICATION/XHTML+XML"}, "application/xhtml+xml", id="case", ), ], ) def test_media_type_of(headers: dict[str, str], expected: str) -> None: assert media_type_of(httpx.Headers(headers)) == expected @pytest.mark.parametrize("seconds", [0, -0.1]) def test_request_deadline_requires_positive_timeout(seconds: float) -> None: with pytest.raises(ValueError, match="REQUEST_TOTAL_TIMEOUT_SECONDS"): RequestDeadline(seconds) def test_deadline_stream_bounds_write_and_tls_timeouts() -> None: stream = Mock(spec=httpcore.NetworkStream) stream.start_tls.return_value = Mock(spec=httpcore.NetworkStream) deadline = RequestDeadline(30) wrapper = DeadlineStream(stream, deadline) wrapper.write(b"payload", timeout=120) written_timeout = stream.write.call_args.kwargs["timeout"] assert 0 < written_timeout <= 30 secure = wrapper.start_tls(Mock(), server_hostname="example.org", timeout=120) assert isinstance(secure, DeadlineStream) assert secure._deadline is deadline assert stream.start_tls.call_args.kwargs["server_hostname"] == "example.org" tls_timeout = stream.start_tls.call_args.kwargs["timeout"] assert 0 < tls_timeout <= 30 def test_network_backend_applies_local_bind_and_socket_options( monkeypatch: pytest.MonkeyPatch, ) -> None: sock = Mock() monkeypatch.setattr(socket, "socket", Mock(return_value=sock)) options = [(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)] stream = PublicNetworkBackend(RequestDeadline(5)).connect_tcp( "8.8.8.8", 443, local_address="0.0.0.0", socket_options=options ) sock.bind.assert_called_once_with(("0.0.0.0", 0)) sock.setsockopt.assert_any_call(*options[0]) sock.connect.assert_called_once_with(("8.8.8.8", 443)) stream.close() sock.close.assert_called_once() def test_network_backend_rejects_empty_public_address_list( monkeypatch: pytest.MonkeyPatch, ) -> None: monkeypatch.setattr( "webmentions_ssg.net.resolve_public_addresses", lambda *args, **kwargs: () ) create_socket = Mock(side_effect=AssertionError("must not open socket")) monkeypatch.setattr(socket, "socket", create_socket) with pytest.raises(httpcore.ConnectError, match="No public addresses available"): PublicNetworkBackend(RequestDeadline(5)).connect_tcp("example.org", 443) create_socket.assert_not_called() @pytest.mark.parametrize( "failure", [ValueError("invalid"), dns.exception.DNSException("lookup failed")] ) def test_dns_resolution_errors_are_wrapped( monkeypatch: pytest.MonkeyPatch, failure: Exception ) -> None: from webmentions_ssg.net import resolve_hostname resolver = Mock(side_effect=failure) monkeypatch.setattr("webmentions_ssg.net.dns.resolver.resolve_name", resolver) with pytest.raises(AddressResolutionError, match="Could not resolve hostname"): resolve_hostname("missing.example", lifetime=1.5) resolver.assert_called_once_with("missing.example", lifetime=1.5)