diff options
Diffstat (limited to 'tests/forms')
| -rw-r--r-- | tests/forms/test_init.py | 49 | ||||
| -rw-r--r-- | tests/forms/test_validators.py | 6 |
2 files changed, 46 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"] 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) |
