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. --- webmentions_ssg/forms/__init__.py | 4 +++- webmentions_ssg/forms/validators.py | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) (limited to 'webmentions_ssg/forms') diff --git a/webmentions_ssg/forms/__init__.py b/webmentions_ssg/forms/__init__.py index f698026..5006365 100644 --- a/webmentions_ssg/forms/__init__.py +++ b/webmentions_ssg/forms/__init__.py @@ -10,7 +10,7 @@ from flask_wtf import FlaskForm from wtforms import PasswordField, StringField, SubmitField from wtforms.validators import URL, InputRequired, Regexp -from .validators import AllowedHostname, NotEqualTo, PublicIPLiteral +from .validators import AllowedHostname, NotEqualTo, PublicIPLiteral, URLMaxBytes class LoginForm(FlaskForm): @@ -35,6 +35,7 @@ class EndpointForm(FlaskForm): "source", validators=[ InputRequired(), + URLMaxBytes(), URL(), Regexp( "^https?://.*", @@ -49,6 +50,7 @@ class EndpointForm(FlaskForm): "target", validators=[ InputRequired(), + URLMaxBytes(), URL(), Regexp( "^https?://.*", diff --git a/webmentions_ssg/forms/validators.py b/webmentions_ssg/forms/validators.py index 2ce2b5b..36f36ce 100644 --- a/webmentions_ssg/forms/validators.py +++ b/webmentions_ssg/forms/validators.py @@ -14,6 +14,32 @@ from wtforms.form import BaseForm from ..url_security import AddressResolutionError, is_public_url +class URLMaxBytes: + """Check the UTF-8 byte length of a submitted URL using the current config.""" + + def __init__( + self, config_key: str = "MAX_WEBMENTION_URL_BYTES", message: str | None = None + ) -> None: + self.config_key = config_key + self.message = message + + def __call__(self, form: BaseForm, field: Field) -> None: + if field.data is None: + return + + max_bytes = current_app.config[self.config_key] + + try: + length = len(field.data.encode("utf-8")) + except UnicodeEncodeError as exc: + raise ValidationError(field.gettext("URL must be valid UTF-8.")) from exc + + if length > max_bytes: + raise ValidationError( + self.message or field.gettext(f"URL must not exceed {max_bytes} bytes.") + ) + + class NotEqualTo: """ Validate that a field does not equal another field. -- cgit v1.3.1