aboutsummaryrefslogtreecommitdiff
path: root/tests/forms/test_endpoint.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/forms/test_endpoint.py')
-rw-r--r--tests/forms/test_endpoint.py228
1 files changed, 228 insertions, 0 deletions
diff --git a/tests/forms/test_endpoint.py b/tests/forms/test_endpoint.py
new file mode 100644
index 0000000..11f233d
--- /dev/null
+++ b/tests/forms/test_endpoint.py
@@ -0,0 +1,228 @@
+# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+import pytest
+from flask import Flask
+from werkzeug.datastructures import MultiDict
+
+from webmentions_ssg.forms.endpoint import WebmentionSubmissionForm
+
+VALID_SOURCE = "https://source.example/post"
+VALID_TARGET = "https://dennisfink.me/blog/example/"
+
+
+@pytest.mark.parametrize(
+ ("form_data", "invalid_field", "expected_error"),
+ [
+ pytest.param(
+ {"target": VALID_TARGET},
+ "source",
+ "This field is required.",
+ id="source-required",
+ ),
+ pytest.param(
+ {"source": "not a URL", "target": VALID_TARGET},
+ "source",
+ "Invalid URL.",
+ id="source-url",
+ ),
+ pytest.param(
+ {"source": "ftp://source.example/post", "target": VALID_TARGET},
+ "source",
+ "source must begin with http or https",
+ id="source-scheme",
+ ),
+ pytest.param(
+ {"source": VALID_SOURCE},
+ "target",
+ "This field is required.",
+ id="target-required",
+ ),
+ pytest.param(
+ {"source": VALID_SOURCE, "target": "not a URL"},
+ "target",
+ "Invalid URL.",
+ id="target-url",
+ ),
+ pytest.param(
+ {"source": VALID_SOURCE, "target": "ftp://dennisfink.me/blog/example/"},
+ "target",
+ "target must begin with http or https",
+ id="target-scheme",
+ ),
+ pytest.param(
+ {"source": "http:///source.example/post", "target": VALID_TARGET},
+ "source",
+ "Invalid URL.",
+ id="source-missing-host",
+ ),
+ pytest.param(
+ {"source": VALID_SOURCE, "target": "https://dennisfink.me:invalid/"},
+ "target",
+ "Invalid URL.",
+ id="target-invalid-port",
+ ),
+ ],
+)
+def test_endpoint_form_rejects_invalid_field_syntax(
+ app: Flask, form_data: dict[str, str], invalid_field: str, expected_error: str
+) -> None:
+ with app.test_request_context("/endpoint", method="POST"):
+ form = WebmentionSubmissionForm(
+ formdata=MultiDict(form_data), meta={"csrf": False}
+ )
+
+ assert not form.validate()
+ assert expected_error in form.errors[invalid_field]
+
+
+@pytest.mark.parametrize("scheme", ["http", "https", "HTTP", "HTTPS"])
+def test_endpoint_form_accepts_http_schemes_case_insensitively(
+ app: Flask, scheme: str
+) -> None:
+ with app.test_request_context("/endpoint", method="POST"):
+ form = WebmentionSubmissionForm(
+ formdata=MultiDict(
+ {
+ "source": f"{scheme}://source.example/post",
+ "target": f"{scheme}://dennisfink.me/blog/example/",
+ }
+ ),
+ meta={"csrf": False},
+ )
+ assert form.validate()
+
+
+def test_endpoint_form_accepts_valid_data(app: Flask) -> None:
+ with app.test_request_context("/endpoint", method="POST"):
+ form = WebmentionSubmissionForm(
+ formdata=MultiDict({"source": VALID_SOURCE, "target": VALID_TARGET}),
+ meta={"csrf": False},
+ )
+
+ assert form.validate()
+ assert form.errors == {}
+
+
+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.net.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 = WebmentionSubmissionForm(
+ 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 = WebmentionSubmissionForm(
+ 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_rejects_private_ip_literal_with_trailing_dot(app: Flask) -> None:
+ with app.test_request_context("/endpoint", method="POST"):
+ form = WebmentionSubmissionForm(
+ formdata=MultiDict(
+ {"source": "http://127.0.0.1./", "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 = WebmentionSubmissionForm(
+ formdata=MultiDict({"source": VALID_SOURCE, "target": "http://[::1"}),
+ meta={"csrf": False},
+ )
+ assert not form.validate()
+ assert "Invalid URL." in form.errors["target"]
+
+
+def test_endpoint_form_applies_configurable_url_byte_limit_to_both_fields(
+ app: Flask,
+) -> None:
+ with app.test_request_context("/endpoint", method="POST"):
+ app.config["MAX_WEBMENTION_URL_BYTES"] = len(VALID_SOURCE.encode("utf-8"))
+ form = WebmentionSubmissionForm(
+ formdata=MultiDict({"source": VALID_SOURCE + "x", "target": VALID_TARGET}),
+ meta={"csrf": False},
+ )
+ assert not form.validate()
+ assert "source" in form.errors
+ assert any("bytes" in message for message in form.errors["source"])
+
+ app.config["MAX_WEBMENTION_URL_BYTES"] = len(VALID_TARGET.encode("utf-8"))
+ form = WebmentionSubmissionForm(
+ formdata=MultiDict({"source": VALID_SOURCE, "target": VALID_TARGET + "x"}),
+ meta={"csrf": False},
+ )
+ assert not form.validate()
+ assert "target" in form.errors
+ assert any("bytes" in message for message in form.errors["target"])
+
+
+@pytest.mark.parametrize("field_name", ["source", "target"])
+@pytest.mark.parametrize(
+ "invalid_url",
+ [
+ pytest.param("http://ex☃mple.test/", id="invalid-idn"),
+ pytest.param("https://example.com/a\nb", id="newline"),
+ pytest.param("https://example.com/a\rb", id="carriage-return"),
+ pytest.param("https://example.com/\x00b", id="nul"),
+ pytest.param("https://example.com/a b", id="ascii-space"),
+ pytest.param("https://example.com/a\u00a0b", id="unicode-whitespace"),
+ pytest.param("https://example.com/\x7fb", id="del"),
+ pytest.param("https://example.com/a\x80b", id="c1-control"),
+ ],
+)
+def test_endpoint_rejects_invalid_httpx_urls_and_whitespace(
+ app: Flask, field_name: str, invalid_url: str
+) -> None:
+ data = {"source": VALID_SOURCE, "target": VALID_TARGET}
+ data[field_name] = invalid_url
+ with app.test_request_context("/endpoint", method="POST"):
+ form = WebmentionSubmissionForm(formdata=MultiDict(data), meta={"csrf": False})
+ assert not form.validate()
+ assert "Invalid URL." in form.errors[field_name]
+
+
+def test_endpoint_accepts_encoded_path_space(app: Flask) -> None:
+ with app.test_request_context("/endpoint", method="POST"):
+ form = WebmentionSubmissionForm(
+ formdata=MultiDict(
+ {"source": "https://source.example/a%20b", "target": VALID_TARGET}
+ ),
+ meta={"csrf": False},
+ )
+ assert form.validate()
+
+
+def test_endpoint_form_rejects_matching_source_and_target(app: Flask) -> None:
+ with app.test_request_context("/endpoint", method="POST"):
+ form = WebmentionSubmissionForm(
+ formdata=MultiDict({"source": VALID_TARGET, "target": VALID_TARGET}),
+ meta={"csrf": False},
+ )
+
+ assert not form.validate()
+ assert "Field must not be equal to target." in form.errors["source"]