diff options
| author | Dennis Fink | 2026-10-31 17:42:00 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-31 17:42:00 +0100 |
| commit | 6924ad7e52033365c55d5afacdd916c502cc80cb (patch) | |
| tree | 8a52b8e8fbae7a6bcc85d250cac2436a2c42e748 /webmentions_ssg/forms/validators.py | |
| parent | 3494eab1208dc18478485e02c61e292556502fa6 (diff) | |
| download | webmentions-ssg-6924ad7e52033365c55d5afacdd916c502cc80cb.tar.gz webmentions-ssg-6924ad7e52033365c55d5afacdd916c502cc80cb.zip | |
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.
Diffstat (limited to 'webmentions_ssg/forms/validators.py')
| -rw-r--r-- | webmentions_ssg/forms/validators.py | 26 |
1 files changed, 26 insertions, 0 deletions
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. |
