From 13a9d9ddce09230017699bc34ac3bf08ed2f84ed Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Fri, 30 Oct 2026 20:31:43 +0100 Subject: 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. --- tests/forms/test_init.py | 49 +++++++++++--- tests/forms/test_validators.py | 6 ++ tests/tasks/test_receiver.py | 58 +++++----------- tests/tasks/test_sender.py | 37 ---------- tests/test_config.py | 1 + tests/test_public_transport.py | 149 +++++++++++++++++++++++++++++++++++++++++ tests/test_url_security.py | 29 +++++++- 7 files changed, 242 insertions(+), 87 deletions(-) create mode 100644 tests/test_public_transport.py (limited to 'tests') diff --git a/tests/forms/test_init.py b/tests/forms/test_init.py index 6fe1849..ffcbd8e 100644 --- a/tests/forms/test_init.py +++ b/tests/forms/test_init.py @@ -2,8 +2,6 @@ # # SPDX-License-Identifier: BSD-3-Clause -from unittest.mock import Mock - import pytest from flask import Flask from werkzeug.datastructures import MultiDict @@ -14,13 +12,6 @@ VALID_SOURCE = "https://source.example/post" VALID_TARGET = "https://dennisfink.me/blog/example/" -@pytest.fixture(autouse=True) -def public_urls(monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.setattr( - "webmentions_ssg.forms.validators.is_public_url", Mock(return_value=True) - ) - - @pytest.mark.parametrize( ("form_data", "invalid_field", "expected_error"), [ @@ -113,3 +104,43 @@ def test_admin_action_form_accepts_submission(app: Flask) -> None: with app.test_request_context(method="POST"): form = AdminActionForm(meta={"csrf": False}) assert form.validate() + + +def test_endpoint_does_not_do_dns_in_web_request( + app: Flask, monkeypatch: pytest.MonkeyPatch +) -> None: + def unexpected_dns_lookup(*args: object, **kwargs: object) -> None: + raise AssertionError("No DNS lookup belongs in form validation") + + monkeypatch.setattr( + "webmentions_ssg.url_security.dns.resolver.resolve_name", + unexpected_dns_lookup, + ) + with app.test_request_context("/endpoint", method="POST"): + for source in (VALID_SOURCE, "ftp://source.example/post", "not a URL"): + form = EndpointForm( + formdata=MultiDict({"source": source, "target": VALID_TARGET}), + meta={"csrf": False}, + ) + assert form.validate() is (source == VALID_SOURCE) + + +@pytest.mark.parametrize("source", ["http://127.0.0.1/", "http://[::1]/", "http://169.254.169.254/"]) +def test_endpoint_rejects_private_ip_literals(app: Flask, source: str) -> None: + with app.test_request_context("/endpoint", method="POST"): + form = EndpointForm( + formdata=MultiDict({"source": source, "target": VALID_TARGET}), + meta={"csrf": False}, + ) + assert not form.validate() + assert "URL must use a public address." in form.errors["source"] + + +def test_endpoint_handles_malformed_ipv6_without_crashing(app: Flask) -> None: + with app.test_request_context("/endpoint", method="POST"): + form = EndpointForm( + formdata=MultiDict({"source": VALID_SOURCE, "target": "http://[::1"}), + meta={"csrf": False}, + ) + assert not form.validate() + assert "Invalid input." in form.errors["target"] diff --git a/tests/forms/test_validators.py b/tests/forms/test_validators.py index 6e26039..2858b4c 100644 --- a/tests/forms/test_validators.py +++ b/tests/forms/test_validators.py @@ -112,3 +112,9 @@ def test_public_url_uses_custom_message(monkeypatch: pytest.MonkeyPatch) -> None with pytest.raises(ValidationError, match="Public URL required"): PublicURL("Public URL required")(form, form.url) + + +def test_allowed_hostname_handles_invalid_ipv6(app: Flask) -> None: + form = URLForm(MultiDict({"url": "http://[::1"})) + with app.app_context(), pytest.raises(ValidationError, match="Invalid input"): + AllowedHostname()(form, form.url) diff --git a/tests/tasks/test_receiver.py b/tests/tasks/test_receiver.py index 4daca58..102410d 100644 --- a/tests/tasks/test_receiver.py +++ b/tests/tasks/test_receiver.py @@ -236,7 +236,7 @@ def test_fetch_source_returns_response_and_body( assert options["follow_redirects"] is True assert options["max_redirects"] == app.config.get("MAX_REDIRECTS", 20) assert options["trust_env"] is False - assert options["event_hooks"] == {"request": [receiver.ensure_public_request]} + assert options["transport"].__class__.__name__ == "PublicHTTPTransport" @patch("webmentions_ssg.tasks.receiver.httpx.Client") @@ -680,52 +680,30 @@ def test_verify_webmention_ignores_unknown_identifier( assert f"Cannot verify unknown ReceivedWebmention {identifier}" in caplog.text -@patch("webmentions_ssg.tasks.receiver.is_public_url", return_value=True) -def test_ensure_public_request_accepts_public_url( - is_public_url: Mock, receiver: ModuleType +@patch("webmentions_ssg.tasks.receiver.httpx.Client") +def test_fetch_source_rejects_unsafe_destination( + httpx_client: Mock, app: Flask, receiver: ModuleType ) -> None: - receiver.ensure_public_request(httpx.Request("GET", SOURCE_URL)) - is_public_url.assert_called_once_with(SOURCE_URL) - + from webmentions_ssg.url_security import UnsafeAddressError -@patch("webmentions_ssg.tasks.receiver.is_public_url", return_value=False) -def test_ensure_public_request_rejects_non_public_address( - is_public_url: Mock, receiver: ModuleType -) -> None: - with pytest.raises( - receiver.VerificationError, match="Source resolves to a non-public address" + client = httpx_client.return_value.__enter__.return_value + client.stream.side_effect = UnsafeAddressError("Local address") + with app.app_context(), pytest.raises( + receiver.VerificationError, match="non-public address" ): - receiver.ensure_public_request(httpx.Request("GET", SOURCE_URL)) - - is_public_url.assert_called_once_with(SOURCE_URL) + receiver.fetch_source(SOURCE_URL) -@patch( - "webmentions_ssg.tasks.receiver.is_public_url", - side_effect=AddressResolutionError("Could not resolve hostname"), -) -def test_ensure_public_request_maps_dns_failure_to_temporary_error( - is_public_url: Mock, receiver: ModuleType +@patch("webmentions_ssg.tasks.receiver.httpx.Client") +def test_fetch_source_treats_dns_failure_as_temporary( + httpx_client: Mock, app: Flask, receiver: ModuleType ) -> None: - with pytest.raises( - receiver.TemporaryFetchError, match="Source hostname could not be resolved" + client = httpx_client.return_value.__enter__.return_value + client.stream.side_effect = AddressResolutionError("DNS timeout") + with app.app_context(), pytest.raises( + receiver.TemporaryFetchError, match="could not be resolved" ): - receiver.ensure_public_request(httpx.Request("GET", SOURCE_URL)) - - is_public_url.assert_called_once_with(SOURCE_URL) - - -@patch( - "webmentions_ssg.tasks.receiver.is_public_url", - side_effect=ValueError("No hostname was specified"), -) -def test_ensure_public_request_rejects_url_without_hostname( - is_public_url: Mock, receiver: ModuleType -) -> None: - with pytest.raises(receiver.VerificationError, match="Source URL has no hostname"): - receiver.ensure_public_request(httpx.Request("GET", SOURCE_URL)) - - is_public_url.assert_called_once_with(SOURCE_URL) + receiver.fetch_source(SOURCE_URL) @patch("webmentions_ssg.tasks.receiver.BeautifulSoup") diff --git a/tests/tasks/test_sender.py b/tests/tasks/test_sender.py index 0db0d8c..38bae11 100644 --- a/tests/tasks/test_sender.py +++ b/tests/tasks/test_sender.py @@ -1214,43 +1214,6 @@ def test_send_webmention_ignores_unknown_identifier( assert f"Cannot send unknown SentWebmention {identifier}" in caplog.text -def test_ensure_public_request_accepts_public_url( - sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch -) -> None: - is_public_url = Mock(return_value=True) - monkeypatch.setattr(sender_module, "is_public_url", is_public_url) - sender_module.ensure_public_request(httpx.Request("GET", TARGET_URL)) - is_public_url.assert_called_once_with(TARGET_URL) - - -def test_ensure_public_request_rejects_non_public_address( - sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch -) -> None: - is_public_url = Mock(return_value=False) - monkeypatch.setattr(sender_module, "is_public_url", is_public_url) - with pytest.raises( - sender_module.PermanentSenderError, - match="Request resolves to a non-public address", - ): - sender_module.ensure_public_request(httpx.Request("GET", TARGET_URL)) - is_public_url.assert_called_once_with(TARGET_URL) - - -def test_ensure_public_request_maps_dns_failure_to_temporary_error( - sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch -) -> None: - is_public_url = Mock( - side_effect=AddressResolutionError("Could not resolve hostname") - ) - monkeypatch.setattr(sender_module, "is_public_url", is_public_url) - with pytest.raises( - sender_module.TemporarySenderError, - match="Request hostname could not be resolved", - ): - sender_module.ensure_public_request(httpx.Request("GET", TARGET_URL)) - is_public_url.assert_called_once_with(TARGET_URL) - - def test_resolve_endpoint_rejects_invalid_url( sender_module: ModuleType, monkeypatch: pytest.MonkeyPatch ) -> None: diff --git a/tests/test_config.py b/tests/test_config.py index 57ac423..1b45946 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -72,6 +72,7 @@ def test_endpoint_limits_are_inherited_from_defaults() -> None: assert config.MAX_REDIRECTS == 20 assert config.MAX_SOURCE_BYTES == 1_000_000 assert config.REQUEST_TIMEOUT == 5.0 + assert config.REQUEST_TOTAL_TIMEOUT == 30.0 assert DefaultConfig.ALLOWED_HOSTNAMES == set() diff --git a/tests/test_public_transport.py b/tests/test_public_transport.py new file mode 100644 index 0000000..e7d77fa --- /dev/null +++ b/tests/test_public_transport.py @@ -0,0 +1,149 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Security regression tests for DNS-pinned network connections.""" + +import socket +from unittest.mock import Mock + +import httpcore +import httpx +import pytest + +from webmentions_ssg.public_transport import ( + DeadlineStream, + NetworkDeadline, + PublicHTTPTransport, + PublicNetworkBackend, +) +from webmentions_ssg.url_security import UnsafeAddressError + + +@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.public_transport.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.public_transport.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"] 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") -- cgit v1.3.1