diff options
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/forms.py | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/webmentions_ssg/forms.py b/webmentions_ssg/forms.py index e3dcdc0..c8ad0ba 100644 --- a/webmentions_ssg/forms.py +++ b/webmentions_ssg/forms.py @@ -13,11 +13,11 @@ from wtforms import Field, PasswordField, StringField, SubmitField, ValidationEr from wtforms.form import BaseForm from wtforms.validators import InputRequired, StopValidation -from .net import hostname_of, is_public_address +from .net import is_public_address, normalized_hostname from .validation import is_control_character -class URLMaxBytes: +class MaxURLBytes: """Check the UTF-8 byte length of a submitted URL using the current config.""" def __init__( @@ -43,7 +43,7 @@ class URLMaxBytes: ) -class HTTPURL: +class StrictHTTPURL: """Require an HTTP(S) URL that httpx can parse without rewriting whitespace. httpx automatically percent-encodes some whitespace. Reject it instead of @@ -75,7 +75,7 @@ class HTTPURL: ) -class AllowedHostname: +class AllowedTargetHostname: """Validate that a URL uses an allowed hostname. :param message: Optional validation error message. @@ -95,8 +95,11 @@ class AllowedHostname: :param field: Field containing the URL to validate. :raises ValidationError: If the URL hostname is not configured as allowed. """ - hostname = hostname_of(field.data) - if hostname is not None and hostname in current_app.config["ALLOWED_HOSTNAMES"]: + hostname = normalized_hostname(field.data) + if ( + hostname is not None + and hostname in current_app.config["ALLOWED_TARGET_HOSTNAMES"] + ): return raise ValidationError(self.message or field.gettext("Invalid input.")) @@ -105,7 +108,7 @@ class PublicIPLiteral: """Reject non-public IP literals without performing DNS in the web worker.""" def __call__(self, form: BaseForm, field: Field) -> None: - hostname = hostname_of(field.data) + hostname = normalized_hostname(field.data) if hostname is None: raise ValidationError(field.gettext("Invalid input.")) @@ -126,7 +129,7 @@ class LoginForm(FlaskForm): submit = SubmitField("Sign In") -class EndpointForm(FlaskForm): +class WebmentionSubmissionForm(FlaskForm): """Form for receiving a Webmention. The source must be a public HTTP or HTTPS URL distinct from the target. @@ -135,11 +138,16 @@ class EndpointForm(FlaskForm): source = StringField( "source", - validators=[InputRequired(), URLMaxBytes(), HTTPURL(), PublicIPLiteral()], + validators=[InputRequired(), MaxURLBytes(), StrictHTTPURL(), PublicIPLiteral()], ) target = StringField( "target", - validators=[InputRequired(), URLMaxBytes(), HTTPURL(), AllowedHostname()], + validators=[ + InputRequired(), + MaxURLBytes(), + StrictHTTPURL(), + AllowedTargetHostname(), + ], ) def validate_source(self, field: Field) -> None: @@ -152,7 +160,7 @@ class EndpointForm(FlaskForm): raise ValidationError(field.gettext("Field must not be equal to target.")) -class AdminActionForm(FlaskForm): +class ConfirmActionForm(FlaskForm): """Form for confirming an administrative action.""" submit = SubmitField("Confirm") |
