aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/forms.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-17 14:44:00 +0100
committerDennis Fink2026-11-17 14:44:00 +0100
commit227092b57acc886469e16bcc4c0d3256e86d3d1e (patch)
treec1de8d5f8f3a37562e482f84e8017213c08a05d6 /webmentions_ssg/forms.py
parentf98b0e446b41325c494bd97900b7bbf730a7d67e (diff)
downloadwebmentions-ssg-227092b57acc886469e16bcc4c0d3256e86d3d1e.tar.gz
webmentions-ssg-227092b57acc886469e16bcc4c0d3256e86d3d1e.zip
feat(receiver): add source domain access rules
Add persistent source block and allow lists with authenticated management, single-row and bulk blocking actions, and optional fail-closed allowlist mode. Reject disallowed sources before enqueueing and recheck policy in verification workers, while sharing canonical hostname normalization across receiver, sender, and configuration paths.
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/forms.py35
1 files changed, 26 insertions, 9 deletions
diff --git a/webmentions_ssg/forms.py b/webmentions_ssg/forms.py
index ae969b3..3b61a8a 100644
--- a/webmentions_ssg/forms.py
+++ b/webmentions_ssg/forms.py
@@ -30,7 +30,8 @@ from wtforms.validators import (
StopValidation,
)
-from .net import is_public_address, normalized_hostname
+from .hostname import normalize_hostname
+from .net import is_public_address
from .validation import is_control_character
RECEIVED_STATUS_CHOICES = {
@@ -120,11 +121,12 @@ class AllowedTargetHostname:
:param field: Field containing the URL to validate.
:raises ValidationError: If the URL hostname is not configured as allowed.
"""
- hostname = normalized_hostname(field.data)
- if (
- hostname is not None
- and hostname in current_app.config["ALLOWED_TARGET_HOSTNAMES"]
- ):
+ 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."))
@@ -133,9 +135,10 @@ class RejectNonPublicIPLiteral:
"""Reject non-public IP literals without performing DNS in the web worker."""
def __call__(self, form: BaseForm, field: Field) -> None:
- hostname = normalized_hostname(field.data)
- if hostname is None:
- raise ValidationError(field.gettext("Invalid input."))
+ 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)
@@ -249,3 +252,17 @@ 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