From cbf7e71558c94ce1c327b683768a18a25d82d197 Mon Sep 17 00:00:00 2001 From: Ryan J Schave Date: Fri, 22 Mar 2019 13:16:26 -0400 Subject: [PATCH] Fixed #30257 -- Made UsernameValidators prohibit trailing newlines. --- django/contrib/auth/validators.py | 4 ++-- tests/auth_tests/test_validators.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/django/contrib/auth/validators.py b/django/contrib/auth/validators.py index b4878cfd45..9345c5cb0b 100644 --- a/django/contrib/auth/validators.py +++ b/django/contrib/auth/validators.py @@ -7,7 +7,7 @@ from django.utils.translation import gettext_lazy as _ @deconstructible class ASCIIUsernameValidator(validators.RegexValidator): - regex = r'^[\w.@+-]+$' + regex = r'^[\w.@+-]+\Z' message = _( 'Enter a valid username. This value may contain only English letters, ' 'numbers, and @/./+/-/_ characters.' @@ -17,7 +17,7 @@ class ASCIIUsernameValidator(validators.RegexValidator): @deconstructible class UnicodeUsernameValidator(validators.RegexValidator): - regex = r'^[\w.@+-]+$' + regex = r'^[\w.@+-]+\Z' message = _( 'Enter a valid username. This value may contain only letters, ' 'numbers, and @/./+/-/_ characters.' diff --git a/tests/auth_tests/test_validators.py b/tests/auth_tests/test_validators.py index 1c2c6b4aff..87e3f890bb 100644 --- a/tests/auth_tests/test_validators.py +++ b/tests/auth_tests/test_validators.py @@ -237,7 +237,7 @@ class UsernameValidatorsTests(SimpleTestCase): invalid_usernames = [ "o'connell", "عبد ال", "zerowidth\u200Bspace", "nonbreaking\u00A0space", - "en\u2013dash", + "en\u2013dash", 'trailingnewline\u000A', ] v = validators.UnicodeUsernameValidator() for valid in valid_usernames: @@ -250,7 +250,7 @@ class UsernameValidatorsTests(SimpleTestCase): def test_ascii_validator(self): valid_usernames = ['glenn', 'GLEnN', 'jean-marc'] - invalid_usernames = ["o'connell", 'Éric', 'jean marc', "أحمد"] + invalid_usernames = ["o'connell", 'Éric', 'jean marc', "أحمد", 'trailingnewline\n'] v = validators.ASCIIUsernameValidator() for valid in valid_usernames: with self.subTest(valid=valid):