aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/forms/validators.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-12 19:57:08 +0100
committerDennis Fink2026-11-12 19:57:08 +0100
commit4bd1d9e348918b2e55db47a1ac3489d035cf55e2 (patch)
treef7ed41a374a8a4136792039a262dc62aa8cf8c59 /webmentions_ssg/forms/validators.py
parent7035e9f9467684cccae2b7ff3faaa62e6a9f91aa (diff)
downloadwebmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.tar.gz
webmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.zip
refactor(core): split modules by responsibility
Break up monolithic views, task helpers, networking, CLI, and application extension setup so each module owns a focused concern. Reorganize the test suite around the new boundaries while preserving existing Webmention behavior and coverage.
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/forms.py (renamed from webmentions_ssg/forms/validators.py)107
1 files changed, 66 insertions, 41 deletions
diff --git a/webmentions_ssg/forms/validators.py b/webmentions_ssg/forms.py
index 384c7a1..a721dc6 100644
--- a/webmentions_ssg/forms/validators.py
+++ b/webmentions_ssg/forms.py
@@ -2,19 +2,21 @@
#
# SPDX-License-Identifier: BSD-3-Clause
-"""Provide validators for URL safety and form field constraints."""
+"""Webmention forms and URL validation."""
import ipaddress
+import re
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 flask_wtf import FlaskForm
+from wtforms import Field, PasswordField, StringField, SubmitField, ValidationError
from wtforms.form import BaseForm
+from wtforms.validators import URL, InputRequired, Regexp, StopValidation
-from ..url_security import AddressResolutionError, is_public_address, is_public_url
+from .net import is_public_address
class URLMaxBytes:
@@ -143,7 +145,7 @@ class AllowedHostname:
:raises ValidationError: If the URL hostname is not configured as allowed.
"""
try:
- hostname = urlsplit(field.data).hostname
+ hostname = (urlsplit(field.data).hostname or "").rstrip(".")
except ValueError, TypeError:
hostname = None
if hostname is not None and hostname in current_app.config["ALLOWED_HOSTNAMES"]:
@@ -151,42 +153,6 @@ class AllowedHostname:
raise ValidationError(self.message or field.gettext("Invalid input."))
-class PublicURL:
- """
- Validate that a URL resolves exclusively to public IP addresses.
-
- :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 resolves exclusively to public IP addresses.
-
- :param form: Form containing the field.
- :param field: Field containing the URL to validate.
- :raises ValidationError: If the URL cannot be resolved, has no hostname, or
- resolves to a non-public address.
- """
- message = self.message
-
- if message is None:
- message = field.gettext("URL must resolve to a public address.")
-
- try:
- if not is_public_url(field.data):
- raise ValidationError(message)
- except (AddressResolutionError, ValueError) as exc:
- raise ValidationError(message) from exc
-
-
class PublicIPLiteral:
"""Reject non-public IP literals without performing DNS in the web worker."""
@@ -206,3 +172,62 @@ class PublicIPLiteral:
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 EndpointForm(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(),
+ URLMaxBytes(),
+ HTTPXURL(),
+ URL(),
+ Regexp(
+ "^https?://.*",
+ flags=re.IGNORECASE,
+ message="source must begin with http or https",
+ ),
+ NotEqualTo("target"),
+ PublicIPLiteral(),
+ ],
+ )
+ target = StringField(
+ "target",
+ validators=[
+ InputRequired(),
+ URLMaxBytes(),
+ HTTPXURL(),
+ URL(),
+ Regexp(
+ "^https?://.*",
+ flags=re.IGNORECASE,
+ message="target must begin with http or https",
+ ),
+ AllowedHostname(),
+ ],
+ )
+
+
+class AdminActionForm(FlaskForm):
+ """
+ Form for confirming an administrative action.
+ """
+
+ submit = SubmitField("Confirm")