From 6924ad7e52033365c55d5afacdd916c502cc80cb Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sat, 31 Oct 2026 17:42:00 +0100 Subject: feat(receiver): add resource limits and retention Bound incoming request and URL sizes so untrusted Webmention submissions cannot consume unbounded application or database resources. Purge old failed and deleted received Webmentions on a schedule, and use portable SHA-256 URL fingerprints to preserve uniqueness for long URLs. --- tests/forms/test_init.py | 23 +++++++++++++++++++++++ tests/forms/test_validators.py | 32 +++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) (limited to 'tests/forms') diff --git a/tests/forms/test_init.py b/tests/forms/test_init.py index 0f5cb23..2a68509 100644 --- a/tests/forms/test_init.py +++ b/tests/forms/test_init.py @@ -145,3 +145,26 @@ def test_endpoint_handles_malformed_ipv6_without_crashing(app: Flask) -> None: ) assert not form.validate() assert "Invalid input." 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 = EndpointForm( + 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 = EndpointForm( + 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"]) diff --git a/tests/forms/test_validators.py b/tests/forms/test_validators.py index 2858b4c..10796e9 100644 --- a/tests/forms/test_validators.py +++ b/tests/forms/test_validators.py @@ -9,7 +9,12 @@ from flask import Flask from werkzeug.datastructures import MultiDict from wtforms import Form, StringField, ValidationError -from webmentions_ssg.forms.validators import AllowedHostname, NotEqualTo, PublicURL +from webmentions_ssg.forms.validators import ( + AllowedHostname, + NotEqualTo, + PublicURL, + URLMaxBytes, +) from webmentions_ssg.url_security import AddressResolutionError @@ -118,3 +123,28 @@ 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) + + +def test_url_max_bytes_respects_utf8_byte_length_and_current_config(app: Flask) -> None: + validator = URLMaxBytes() + form = URLForm(MultiDict({"url": "é" * 3})) + with app.app_context(): + app.config["MAX_WEBMENTION_URL_BYTES"] = 6 + validator(form, form.url) + app.config["MAX_WEBMENTION_URL_BYTES"] = 5 + with pytest.raises(ValidationError, match="5 bytes"): + validator(form, form.url) + + +def test_url_max_bytes_custom_message(app: Flask) -> None: + form = URLForm(MultiDict({"url": "too-long"})) + with app.app_context(): + app.config["MAX_WEBMENTION_URL_BYTES"] = 2 + with pytest.raises(ValidationError, match="URL too long"): + URLMaxBytes(message="URL too long")(form, form.url) + + +def test_url_max_bytes_rejects_malformed_unicode(app: Flask) -> None: + form = URLForm(MultiDict({"url": "\ud800"})) + with app.app_context(), pytest.raises(ValidationError, match="valid UTF-8"): + URLMaxBytes()(form, form.url) -- cgit v1.3.1