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 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) (limited to 'tests/forms/test_init.py') 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"] -- cgit v1.3.1