diff options
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/forms.py | 35 |
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 |
