diff options
| -rw-r--r-- | tests/test_forms.py | 29 | ||||
| -rw-r--r-- | webmentions_ssg/forms.py | 38 |
2 files changed, 39 insertions, 28 deletions
diff --git a/tests/test_forms.py b/tests/test_forms.py index 87b02cd..20e655c 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -63,6 +63,18 @@ class URLForm(Form): "target must begin with http or https", id="target-scheme", ), + pytest.param( + {"source": "http:///source.example/post", "target": VALID_TARGET}, + "source", + "Invalid URL.", + id="source-missing-host", + ), + pytest.param( + {"source": VALID_SOURCE, "target": "https://dennisfink.me:invalid/"}, + "target", + "Invalid URL.", + id="target-invalid-port", + ), ], ) def test_endpoint_form_rejects_invalid_field_syntax( @@ -75,6 +87,23 @@ def test_endpoint_form_rejects_invalid_field_syntax( assert expected_error in form.errors[invalid_field] +@pytest.mark.parametrize("scheme", ["http", "https", "HTTP", "HTTPS"]) +def test_endpoint_form_accepts_http_schemes_case_insensitively( + app: Flask, scheme: str +) -> None: + with app.test_request_context("/endpoint", method="POST"): + form = EndpointForm( + formdata=MultiDict( + { + "source": f"{scheme}://source.example/post", + "target": f"{scheme}://dennisfink.me/blog/example/", + } + ), + meta={"csrf": False}, + ) + assert form.validate() + + def test_endpoint_form_accepts_valid_data(app: Flask) -> None: with app.test_request_context("/endpoint", method="POST"): form = EndpointForm( diff --git a/webmentions_ssg/forms.py b/webmentions_ssg/forms.py index af24423..0d83327 100644 --- a/webmentions_ssg/forms.py +++ b/webmentions_ssg/forms.py @@ -5,7 +5,6 @@ """Webmention forms and URL validation.""" import ipaddress -import re import unicodedata from urllib.parse import urlsplit @@ -14,7 +13,7 @@ from flask import current_app 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 wtforms.validators import InputRequired, StopValidation from .net import is_public_address @@ -45,8 +44,8 @@ class URLMaxBytes: ) -class HTTPXURL: - """Reject URLs that cannot be fetched by httpx or contain whitespace/controls. +class HTTPURL: + """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). @@ -71,6 +70,11 @@ class HTTPXURL: 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 AllowedHostname: """Validate that a URL uses an allowed hostname. @@ -139,33 +143,11 @@ class EndpointForm(FlaskForm): source = StringField( "source", - validators=[ - InputRequired(), - URLMaxBytes(), - HTTPXURL(), - URL(), - Regexp( - "^https?://.*", - flags=re.IGNORECASE, - message="source must begin with http or https", - ), - PublicIPLiteral(), - ], + validators=[InputRequired(), URLMaxBytes(), HTTPURL(), PublicIPLiteral()], ) target = StringField( "target", - validators=[ - InputRequired(), - URLMaxBytes(), - HTTPXURL(), - URL(), - Regexp( - "^https?://.*", - flags=re.IGNORECASE, - message="target must begin with http or https", - ), - AllowedHostname(), - ], + validators=[InputRequired(), URLMaxBytes(), HTTPURL(), AllowedHostname()], ) def validate_source(self, field: Field) -> None: |
