diff options
| author | Dennis Fink | 2026-10-30 20:31:43 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-30 20:31:43 +0100 |
| commit | 13a9d9ddce09230017699bc34ac3bf08ed2f84ed (patch) | |
| tree | 3715bd13439a61a8cc6896581811a30b62ac1b16 /webmentions_ssg/forms | |
| parent | 8c561e702ed15022b17eae81e4185448c021e1cb (diff) | |
| download | webmentions-ssg-13a9d9ddce09230017699bc34ac3bf08ed2f84ed.tar.gz webmentions-ssg-13a9d9ddce09230017699bc34ac3bf08ed2f84ed.zip | |
fix(core): prevent DNS rebinding in HTTP requests
Pin outbound connections to validated public IP addresses so DNS changes
cannot redirect receiver or sender requests onto private or link-local
networks.
Apply the same protection across redirects, add an overall request
deadline, and keep DNS resolution out of synchronous endpoint form
validation.
Diffstat (limited to 'webmentions_ssg/forms')
| -rw-r--r-- | webmentions_ssg/forms/__init__.py | 4 | ||||
| -rw-r--r-- | webmentions_ssg/forms/validators.py | 27 |
2 files changed, 28 insertions, 3 deletions
diff --git a/webmentions_ssg/forms/__init__.py b/webmentions_ssg/forms/__init__.py index 2e3c72c..f698026 100644 --- a/webmentions_ssg/forms/__init__.py +++ b/webmentions_ssg/forms/__init__.py @@ -10,7 +10,7 @@ from flask_wtf import FlaskForm from wtforms import PasswordField, StringField, SubmitField from wtforms.validators import URL, InputRequired, Regexp -from .validators import AllowedHostname, NotEqualTo, PublicURL +from .validators import AllowedHostname, NotEqualTo, PublicIPLiteral class LoginForm(FlaskForm): @@ -42,7 +42,7 @@ class EndpointForm(FlaskForm): message="source must begin with http or https", ), NotEqualTo("target"), - PublicURL(), + PublicIPLiteral(), ], ) target = StringField( diff --git a/webmentions_ssg/forms/validators.py b/webmentions_ssg/forms/validators.py index dd0ee9b..e8f33eb 100644 --- a/webmentions_ssg/forms/validators.py +++ b/webmentions_ssg/forms/validators.py @@ -4,6 +4,7 @@ """Provide validators for URL safety and form field constraints.""" +import ipaddress from urllib.parse import urlsplit from flask import current_app @@ -85,7 +86,10 @@ class AllowedHostname: :param field: Field containing the URL to validate. :raises ValidationError: If the URL hostname is not configured as allowed. """ - hostname = urlsplit(field.data).hostname + try: + hostname = urlsplit(field.data).hostname + except (ValueError, TypeError): + hostname = None if hostname is not None and hostname in current_app.config["ALLOWED_HOSTNAMES"]: return raise ValidationError(self.message or field.gettext("Invalid input.")) @@ -125,3 +129,24 @@ class PublicURL: 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.""" + + def __call__(self, form: BaseForm, field: Field) -> None: + try: + hostname = urlsplit(field.data).hostname + except (ValueError, TypeError) as exc: + raise ValidationError(field.gettext("Invalid input.")) from exc + + if hostname is None: + raise ValidationError(field.gettext("Invalid input.")) + + try: + address = ipaddress.ip_address(hostname) + except ValueError: + return # Named hosts are resolved only during the background fetch. + + if not address.is_global: + raise ValidationError(field.gettext("URL must use a public address.")) |
