diff options
| author | Dennis Fink | 2026-10-31 19:00:17 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-31 19:00:17 +0100 |
| commit | df3b79e2c89978ec21dc2da7f90801dce33aeab2 (patch) | |
| tree | 146f34d59b2b1957e53856a58777a3453960a2a5 /webmentions_ssg/forms/validators.py | |
| parent | f7c0691e0da84644ea8b2b83ad5574fe179da59b (diff) | |
| download | webmentions-ssg-df3b79e2c89978ec21dc2da7f90801dce33aeab2.tar.gz webmentions-ssg-df3b79e2c89978ec21dc2da7f90801dce33aeab2.zip | |
fix(receiver): prevent endless verification recovery
Cap stale verification requeues so repeatedly lost or crashing tasks
eventually become terminal failures instead of cycling indefinitely.
Reject URLs that httpx cannot safely fetch before enqueueing, and
persist unexpected verification errors as terminal failures.
Diffstat (limited to 'webmentions_ssg/forms/validators.py')
| -rw-r--r-- | webmentions_ssg/forms/validators.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/webmentions_ssg/forms/validators.py b/webmentions_ssg/forms/validators.py index c1f1c97..384c7a1 100644 --- a/webmentions_ssg/forms/validators.py +++ b/webmentions_ssg/forms/validators.py @@ -5,10 +5,13 @@ """Provide validators for URL safety and form field constraints.""" import ipaddress +import unicodedata from urllib.parse import urlsplit +import httpx from flask import current_app from wtforms import Field, ValidationError +from wtforms.validators import StopValidation from wtforms.form import BaseForm from ..url_security import AddressResolutionError, is_public_address, is_public_url @@ -40,6 +43,33 @@ class URLMaxBytes: ) +class HTTPXURL: + """Reject URLs that cannot be fetched by httpx or contain whitespace/controls. + + httpx automatically percent-encodes some whitespace. Reject it instead of + silently changing the submitted URL (which may be used in exact URL matches). + """ + + def __call__(self, form: BaseForm, field: Field) -> None: + value = field.data + if not isinstance(value, str): + raise StopValidation(field.gettext("Invalid URL.")) + + if any( + character.isspace() or unicodedata.category(character) == "Cc" + for character in value + ): + raise StopValidation(field.gettext("Invalid URL.")) + + try: + parsed = httpx.URL(value) + except (httpx.InvalidURL, ValueError, UnicodeError) as exc: + raise StopValidation(field.gettext("Invalid URL.")) from exc + + if not parsed.host: + raise StopValidation(field.gettext("Invalid URL.")) + + class NotEqualTo: """ Validate that a field does not equal another field. |
