aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Fink2026-11-13 08:39:18 +0100
committerDennis Fink2026-11-13 08:39:18 +0100
commit3496b3882ca08e74c108d5abe417eccec5ed4f48 (patch)
tree9b3b272e1b1162611feb8d33deb5eceb78efee88
parenta227ec62b1cb0d5a4bb840072173607879e5d2f8 (diff)
downloadwebmentions-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 '')
-rw-r--r--tests/test_auth.py8
-rw-r--r--tests/test_cli.py1
-rw-r--r--tests/test_forms.py1
-rw-r--r--webmentions_ssg/auth.py3
-rw-r--r--webmentions_ssg/forms.py4
-rw-r--r--webmentions_ssg/validation.py12
6 files changed, 25 insertions, 4 deletions
diff --git a/tests/test_auth.py b/tests/test_auth.py
index 008382a..c302e8f 100644
--- a/tests/test_auth.py
+++ b/tests/test_auth.py
@@ -8,10 +8,16 @@ import pytest
from flask import Flask
from webmentions_ssg import db
-from webmentions_ssg.auth import AUTH, is_safe_next_url, load_user
+from webmentions_ssg.auth import AUTH, is_safe_next_url, load_user, validate_password
from webmentions_ssg.models import User
+@pytest.mark.parametrize("character", ["\x00", "\x1f", "\x7f", "\x80", "\x9f"])
+def test_validate_password_rejects_unicode_controls(character: str) -> None:
+ with pytest.raises(ValueError, match="control characters"):
+ validate_password(f"valid passphrase{character}")
+
+
def test_user_loader_is_registered(app: Flask) -> None:
assert AUTH._user_callback is load_user
diff --git a/tests/test_cli.py b/tests/test_cli.py
index e6214f2..ac673b4 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -608,6 +608,7 @@ def test_users_cli_create_rolls_back_on_duplicate_race(app: Flask) -> None:
[
("short", "at least 8 characters"),
("good passphrase with\tab", "control characters"),
+ ("good passphrase with\x80c1", "control characters"),
],
)
@pytest.mark.parametrize("operation", ["create", "set-password"])
diff --git a/tests/test_forms.py b/tests/test_forms.py
index 20e655c..a37a6d0 100644
--- a/tests/test_forms.py
+++ b/tests/test_forms.py
@@ -222,6 +222,7 @@ def test_endpoint_form_applies_configurable_url_byte_limit_to_both_fields(
pytest.param("https://example.com/a b", id="ascii-space"),
pytest.param("https://example.com/a\u00a0b", id="unicode-whitespace"),
pytest.param("https://example.com/\x7fb", id="del"),
+ pytest.param("https://example.com/a\x80b", id="c1-control"),
],
)
def test_endpoint_rejects_invalid_httpx_urls_and_whitespace(
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"