diff options
| author | Dennis Fink | 2026-11-13 08:39:18 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-13 08:39:18 +0100 |
| commit | 3496b3882ca08e74c108d5abe417eccec5ed4f48 (patch) | |
| tree | 9b3b272e1b1162611feb8d33deb5eceb78efee88 /webmentions_ssg | |
| parent | a227ec62b1cb0d5a4bb840072173607879e5d2f8 (diff) | |
| download | webmentions-ssg-3496b3882ca08e74c108d5abe417eccec5ed4f48.tar.gz webmentions-ssg-3496b3882ca08e74c108d5abe417eccec5ed4f48.zip | |
fix(core): reject all Unicode control characters
Use shared Unicode category validation so passwords reject C1 controls
in addition to C0 and DEL, and reuse the same helper for URL validation.
Diffstat (limited to 'webmentions_ssg')
| -rw-r--r-- | webmentions_ssg/auth.py | 3 | ||||
| -rw-r--r-- | webmentions_ssg/forms.py | 4 | ||||
| -rw-r--r-- | webmentions_ssg/validation.py | 12 |
3 files changed, 16 insertions, 3 deletions
diff --git a/webmentions_ssg/auth.py b/webmentions_ssg/auth.py index 209bdd6..1707917 100644 --- a/webmentions_ssg/auth.py +++ b/webmentions_ssg/auth.py @@ -15,6 +15,7 @@ from flask_login import LoginManager from .extensions import db from .models import User +from .validation import is_control_character AUTH = LoginManager() @@ -38,7 +39,7 @@ def validate_password(password: str) -> None: raise ValueError( f"Password must be at least {MIN_PASSWORD_LENGTH} characters long." ) - if any(ord(char) < 32 or ord(char) == 127 for char in password): + if any(is_control_character(char) for char in password): raise ValueError("Password must not contain control characters.") diff --git a/webmentions_ssg/forms.py b/webmentions_ssg/forms.py index 0d83327..005bd20 100644 --- a/webmentions_ssg/forms.py +++ b/webmentions_ssg/forms.py @@ -5,7 +5,6 @@ """Webmention forms and URL validation.""" import ipaddress -import unicodedata from urllib.parse import urlsplit import httpx @@ -16,6 +15,7 @@ from wtforms.form import BaseForm from wtforms.validators import InputRequired, StopValidation from .net import is_public_address +from .validation import is_control_character class URLMaxBytes: @@ -57,7 +57,7 @@ class HTTPURL: raise StopValidation(field.gettext("Invalid URL.")) if any( - character.isspace() or unicodedata.category(character) == "Cc" + character.isspace() or is_control_character(character) for character in value ): raise StopValidation(field.gettext("Invalid URL.")) diff --git a/webmentions_ssg/validation.py b/webmentions_ssg/validation.py new file mode 100644 index 0000000..e39d4d5 --- /dev/null +++ b/webmentions_ssg/validation.py @@ -0,0 +1,12 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Shared character validation helpers.""" + +import unicodedata + + +def is_control_character(character: str) -> bool: + """Identify Unicode control characters, including C0, DEL, and C1.""" + return unicodedata.category(character) == "Cc" |
