diff options
| author | Dennis Fink | 2026-11-17 17:09:59 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-17 17:09:59 +0100 |
| commit | 6d6d079a3a99d3d3a922a640b6925eb2adf044de (patch) | |
| tree | 33cc19241f4ce86ea43b2f194087c58e3fdc8a4d /webmentions_ssg/forms | |
| parent | 227092b57acc886469e16bcc4c0d3256e86d3d1e (diff) | |
| download | webmentions-ssg-6d6d079a3a99d3d3a922a640b6925eb2adf044de.tar.gz webmentions-ssg-6d6d079a3a99d3d3a922a640b6925eb2adf044de.zip | |
refactor(core)!: split forms and models by feature
Replace monolithic form and model modules with focused packages for
receiver, moderation, source, user, and shared validation concerns.
Rename source policy concepts to domain moderation, including routes,
models, tables, configuration, templates, and tests.
BREAKING CHANGE: form and model import paths, moderation routes, and
`SOURCE_ALLOWLIST_ENABLED` have changed; use `ALLOWLIST_ENABLED` and the
new feature-specific modules.
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/forms.py | 268 | ||||
| -rw-r--r-- | webmentions_ssg/forms/__init__.py | 5 | ||||
| -rw-r--r-- | webmentions_ssg/forms/admin.py | 14 | ||||
| -rw-r--r-- | webmentions_ssg/forms/endpoint.py | 52 | ||||
| -rw-r--r-- | webmentions_ssg/forms/moderation.py | 25 | ||||
| -rw-r--r-- | webmentions_ssg/forms/received.py | 70 | ||||
| -rw-r--r-- | webmentions_ssg/forms/user.py | 17 | ||||
| -rw-r--r-- | webmentions_ssg/forms/validators.py | 135 |
8 files changed, 318 insertions, 268 deletions
diff --git a/webmentions_ssg/forms.py b/webmentions_ssg/forms.py deleted file mode 100644 index 3b61a8a..0000000 --- a/webmentions_ssg/forms.py +++ /dev/null @@ -1,268 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Webmention forms and URL validation.""" - -import ipaddress - -import httpx -from flask import current_app -from flask_wtf import FlaskForm -from wtforms import ( - DateField, - Field, - HiddenField, - IntegerField, - PasswordField, - SearchField, - SelectField, - StringField, - SubmitField, - ValidationError, -) -from wtforms.form import BaseForm -from wtforms.validators import ( - AnyOf, - InputRequired, - NumberRange, - Optional, - StopValidation, -) - -from .hostname import normalize_hostname -from .net import is_public_address -from .validation import is_control_character - -RECEIVED_STATUS_CHOICES = { - "": "All statuses", - "pending": "Pending", - "verifying": "Verifying", - "verified": "Verified", - "declined": "Declined", -} - - -class MaxURLBytes: - """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 StrictHTTPURL: - """Require an HTTP(S) URL that httpx can parse without rewriting whitespace. - - 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 is_control_character(character) - 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.")) - - if parsed.scheme not in ("http", "https"): - raise StopValidation( - field.gettext(f"{field.name} must begin with http or https") - ) - - -class AllowedTargetHostname: - """Validate that a URL uses an allowed hostname. - - :param message: Optional validation error message. - """ - - def __init__(self, message: str | None = None) -> None: - """Initialize the validator. - - :param message: Optional validation error message. - """ - self.message = message - - def __call__(self, form: BaseForm, field: Field) -> None: - """Validate that a URL uses an allowed hostname. - - :param form: Form containing the field. - :param field: Field containing the URL to validate. - :raises ValidationError: If the URL hostname is not configured as allowed. - """ - try: - hostname = normalize_hostname(field.data, from_url=True) - except ValueError: - hostname = None - - if hostname in current_app.config["ALLOWED_TARGET_HOSTNAMES"]: - return - raise ValidationError(self.message or field.gettext("Invalid input.")) - - -class RejectNonPublicIPLiteral: - """Reject non-public IP literals without performing DNS in the web worker.""" - - def __call__(self, form: BaseForm, field: Field) -> None: - try: - hostname = normalize_hostname(field.data, from_url=True) - except ValueError as exc: - raise ValidationError(field.gettext("Invalid input.")) from exc - - try: - address = ipaddress.ip_address(hostname) - except ValueError: - return # Named hosts are resolved only during the background fetch. - - if not is_public_address(address): - raise ValidationError(field.gettext("URL must use a public address.")) - - -class LoginForm(FlaskForm): - """Form for authenticating a user.""" - - username = StringField("Username", validators=[InputRequired()]) - password = PasswordField("Password", validators=[InputRequired()]) - submit = SubmitField("Sign In") - - -class WebmentionSubmissionForm(FlaskForm): - """Form for receiving a Webmention. - - The source must be a public HTTP or HTTPS URL distinct from the target. - The target must be an HTTP or HTTPS URL using an allowed hostname. - """ - - source = StringField( - "source", - validators=[ - InputRequired(), - MaxURLBytes(), - StrictHTTPURL(), - RejectNonPublicIPLiteral(), - ], - ) - target = StringField( - "target", - validators=[ - InputRequired(), - MaxURLBytes(), - StrictHTTPURL(), - AllowedTargetHostname(), - ], - ) - - def validate_source(self, field: Field) -> None: - """Reject a source URL that equals the target URL. - - :param field: Source URL field being validated. - :raises ValidationError: If the source URL equals the target URL. - """ - if field.data == self.target.data: - raise ValidationError(field.gettext("Field must not be equal to target.")) - - -class CanonicalDate: - """Require HTML date inputs to use the canonical YYYY-MM-DD format.""" - - def __call__(self, form: BaseForm, field: DateField) -> None: - if ( - field.data is not None - and field.raw_data - and field.raw_data[0] != field.data.isoformat() - ): - raise ValidationError(field.gettext("Invalid date.")) - - -def default_received_page_size() -> int: - """Use 25 rows unless the configured maximum is lower.""" - return min(25, current_app.config["RECEIVED_MAX_PAGE_SIZE"]) - - -class ReceivedWebmentionFilterForm(FlaskForm): - """GET filters for the received Webmention list.""" - - status = SelectField("Status", choices=RECEIVED_STATUS_CHOICES, default="") - q = SearchField("Source or target") - date_from = DateField( - "Updated from (UTC)", validators=[Optional(), CanonicalDate()] - ) - date_to = DateField("Updated to (UTC)", validators=[Optional(), CanonicalDate()]) - sort = HiddenField( - default="received", - validators=[AnyOf(("received", "updated", "status", "source", "target"))], - ) - order = HiddenField(default="desc", validators=[AnyOf(("asc", "desc"))]) - per_page = IntegerField( - "Rows per page", - default=default_received_page_size, - validators=[NumberRange(min=1)], - ) - - def validate_per_page(self, field: IntegerField) -> None: - """Enforce the deployment's maximum received-list page size.""" - if ( - field.data is not None - and field.data > current_app.config["RECEIVED_MAX_PAGE_SIZE"] - ): - raise ValidationError("Rows per page exceeds the configured maximum.") - - def validate_date_to(self, field: DateField) -> None: - """Require the end date to be on or after the start date.""" - if ( - self.date_from.data is not None - and field.data is not None - and field.data < self.date_from.data - ): - raise ValidationError("End date must be on or after start date.") - - -class ConfirmActionForm(FlaskForm): - """Form for confirming an administrative action.""" - - submit = SubmitField("Confirm") - - -class SourceDomainForm(FlaskForm): - """Add a domain to the source blocklist or allowlist.""" - - domain = StringField("Source domain", validators=[InputRequired()]) - submit = SubmitField("Add domain") - - def validate_domain(self, field: Field) -> None: - """Require a bare hostname and persist its canonical form.""" - try: - field.data = normalize_hostname(field.data) - except ValueError as exc: - raise ValidationError(str(exc)) from exc diff --git a/webmentions_ssg/forms/__init__.py b/webmentions_ssg/forms/__init__.py new file mode 100644 index 0000000..5f61c0e --- /dev/null +++ b/webmentions_ssg/forms/__init__.py @@ -0,0 +1,5 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Forms for webmention submission, authentication, filtering and moderation.""" diff --git a/webmentions_ssg/forms/admin.py b/webmentions_ssg/forms/admin.py new file mode 100644 index 0000000..97d523d --- /dev/null +++ b/webmentions_ssg/forms/admin.py @@ -0,0 +1,14 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Administrative action confirmation forms.""" + +from flask_wtf import FlaskForm +from wtforms import SubmitField + + +class ConfirmActionForm(FlaskForm): + """Form for confirming an administrative action.""" + + submit = SubmitField("Confirm") diff --git a/webmentions_ssg/forms/endpoint.py b/webmentions_ssg/forms/endpoint.py new file mode 100644 index 0000000..e063e73 --- /dev/null +++ b/webmentions_ssg/forms/endpoint.py @@ -0,0 +1,52 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Webmention submission form.""" + +from flask_wtf import FlaskForm +from wtforms import Field, StringField, ValidationError +from wtforms.validators import InputRequired + +from .validators import ( + AllowedTargetHostname, + MaxURLBytes, + RejectNonPublicIPLiteral, + StrictHTTPURL, +) + + +class WebmentionSubmissionForm(FlaskForm): + """Form for receiving a Webmention. + + The source must be a public HTTP or HTTPS URL distinct from the target. + The target must be an HTTP or HTTPS URL using an allowed hostname. + """ + + source = StringField( + "source", + validators=[ + InputRequired(), + MaxURLBytes(), + StrictHTTPURL(), + RejectNonPublicIPLiteral(), + ], + ) + target = StringField( + "target", + validators=[ + InputRequired(), + MaxURLBytes(), + StrictHTTPURL(), + AllowedTargetHostname(), + ], + ) + + def validate_source(self, field: Field) -> None: + """Reject a source URL that equals the target URL. + + :param field: Source URL field being validated. + :raises ValidationError: If the source URL equals the target URL. + """ + if field.data == self.target.data: + raise ValidationError(field.gettext("Field must not be equal to target.")) diff --git a/webmentions_ssg/forms/moderation.py b/webmentions_ssg/forms/moderation.py new file mode 100644 index 0000000..5987d14 --- /dev/null +++ b/webmentions_ssg/forms/moderation.py @@ -0,0 +1,25 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Forms for managing domain allowlists and blocklists.""" + +from flask_wtf import FlaskForm +from wtforms import Field, StringField, SubmitField, ValidationError +from wtforms.validators import InputRequired + +from ..hostname import normalize_hostname + + +class ModerateDomainForm(FlaskForm): + """Add a source domain to the blocklist or allowlist.""" + + domain = StringField("Source domain", validators=[InputRequired()]) + submit = SubmitField("Add domain") + + def validate_domain(self, field: Field) -> None: + """Require a bare hostname and persist its canonical form.""" + try: + field.data = normalize_hostname(field.data) + except ValueError as exc: + raise ValidationError(str(exc)) from exc diff --git a/webmentions_ssg/forms/received.py b/webmentions_ssg/forms/received.py new file mode 100644 index 0000000..c4a6ed0 --- /dev/null +++ b/webmentions_ssg/forms/received.py @@ -0,0 +1,70 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Received Webmention filtering form.""" + +from flask import current_app +from flask_wtf import FlaskForm +from wtforms import ( + DateField, + HiddenField, + IntegerField, + SearchField, + SelectField, + ValidationError, +) +from wtforms.validators import AnyOf, NumberRange, Optional + +from .validators import CanonicalDate + +RECEIVED_STATUS_CHOICES = { + "": "All statuses", + "pending": "Pending", + "verifying": "Verifying", + "verified": "Verified", + "declined": "Declined", +} + + +def default_received_page_size() -> int: + """Use 25 rows unless the configured maximum is lower.""" + return min(25, current_app.config["RECEIVED_MAX_PAGE_SIZE"]) + + +class ReceivedWebmentionFilterForm(FlaskForm): + """GET filters for the received Webmention list.""" + + status = SelectField("Status", choices=RECEIVED_STATUS_CHOICES, default="") + q = SearchField("Source or target") + date_from = DateField( + "Updated from (UTC)", validators=[Optional(), CanonicalDate()] + ) + date_to = DateField("Updated to (UTC)", validators=[Optional(), CanonicalDate()]) + sort = HiddenField( + default="received", + validators=[AnyOf(("received", "updated", "status", "source", "target"))], + ) + order = HiddenField(default="desc", validators=[AnyOf(("asc", "desc"))]) + per_page = IntegerField( + "Rows per page", + default=default_received_page_size, + validators=[NumberRange(min=1)], + ) + + def validate_per_page(self, field: IntegerField) -> None: + """Enforce the deployment's maximum received-list page size.""" + if ( + field.data is not None + and field.data > current_app.config["RECEIVED_MAX_PAGE_SIZE"] + ): + raise ValidationError("Rows per page exceeds the configured maximum.") + + def validate_date_to(self, field: DateField) -> None: + """Require the end date to be on or after the start date.""" + if ( + self.date_from.data is not None + and field.data is not None + and field.data < self.date_from.data + ): + raise ValidationError("End date must be on or after start date.") diff --git a/webmentions_ssg/forms/user.py b/webmentions_ssg/forms/user.py new file mode 100644 index 0000000..3bb6421 --- /dev/null +++ b/webmentions_ssg/forms/user.py @@ -0,0 +1,17 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""User authentication forms.""" + +from flask_wtf import FlaskForm +from wtforms import PasswordField, StringField, SubmitField +from wtforms.validators import InputRequired + + +class LoginForm(FlaskForm): + """Form for authenticating a user.""" + + username = StringField("Username", validators=[InputRequired()]) + password = PasswordField("Password", validators=[InputRequired()]) + submit = SubmitField("Sign In") diff --git a/webmentions_ssg/forms/validators.py b/webmentions_ssg/forms/validators.py new file mode 100644 index 0000000..e65e9cb --- /dev/null +++ b/webmentions_ssg/forms/validators.py @@ -0,0 +1,135 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Custom Webmention URL and date field validators.""" + +import ipaddress + +import httpx +from flask import current_app +from wtforms import DateField, Field, ValidationError +from wtforms.form import BaseForm +from wtforms.validators import StopValidation + +from ..hostname import normalize_hostname +from ..net import is_public_address +from ..validation import is_control_character + + +class MaxURLBytes: + """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 StrictHTTPURL: + """Require an HTTP(S) URL that httpx can parse without rewriting whitespace. + + 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 is_control_character(character) + 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.")) + + if parsed.scheme not in ("http", "https"): + raise StopValidation( + field.gettext(f"{field.name} must begin with http or https") + ) + + +class AllowedTargetHostname: + """Validate that a URL uses an allowed hostname. + + :param message: Optional validation error message. + """ + + def __init__(self, message: str | None = None) -> None: + """Initialize the validator. + + :param message: Optional validation error message. + """ + self.message = message + + def __call__(self, form: BaseForm, field: Field) -> None: + """Validate that a URL uses an allowed hostname. + + :param form: Form containing the field. + :param field: Field containing the URL to validate. + :raises ValidationError: If the URL hostname is not configured as allowed. + """ + try: + hostname = normalize_hostname(field.data, from_url=True) + except ValueError: + hostname = None + + if hostname in current_app.config["ALLOWED_TARGET_HOSTNAMES"]: + return + raise ValidationError(self.message or field.gettext("Invalid input.")) + + +class RejectNonPublicIPLiteral: + """Reject non-public IP literals without performing DNS in the web worker.""" + + def __call__(self, form: BaseForm, field: Field) -> None: + try: + hostname = normalize_hostname(field.data, from_url=True) + except ValueError as exc: + raise ValidationError(field.gettext("Invalid input.")) from exc + + try: + address = ipaddress.ip_address(hostname) + except ValueError: + return # Named hosts are resolved only during the background fetch. + + if not is_public_address(address): + raise ValidationError(field.gettext("URL must use a public address.")) + + +class CanonicalDate: + """Require HTML date inputs to use the canonical YYYY-MM-DD format.""" + + def __call__(self, form: BaseForm, field: DateField) -> None: + if ( + field.data is not None + and field.raw_data + and field.raw_data[0] != field.data.isoformat() + ): + raise ValidationError(field.gettext("Invalid date.")) |
