aboutsummaryrefslogtreecommitdiff
path: root/tests/forms/test_init.py
diff options
context:
space:
mode:
authorDennis Fink2026-10-30 20:31:43 +0100
committerDennis Fink2026-10-30 20:31:43 +0100
commit13a9d9ddce09230017699bc34ac3bf08ed2f84ed (patch)
tree3715bd13439a61a8cc6896581811a30b62ac1b16 /tests/forms/test_init.py
parent8c561e702ed15022b17eae81e4185448c021e1cb (diff)
downloadwebmentions-ssg-13a9d9ddce09230017699bc34ac3bf08ed2f84ed.tar.gz
webmentions-ssg-13a9d9ddce09230017699bc34ac3bf08ed2f84ed.zip
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.
Diffstat (limited to 'tests/forms/test_init.py')
-rw-r--r--tests/forms/test_init.py49
1 files changed, 40 insertions, 9 deletions
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"]