Skip to content

Commit f9a7fb8

Browse files
committed
[3.2.x] Fixed CVE-2023-46695 -- Fixed potential DoS in UsernameField on Windows.
Thanks MProgrammer (https://hackerone.com/mprogrammer) for the report.
1 parent e6d2591 commit f9a7fb8

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

django/contrib/auth/forms.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@ def __init__(self, *args, **kwargs):
6262

6363
class UsernameField(forms.CharField):
6464
def to_python(self, value):
65-
return unicodedata.normalize('NFKC', super().to_python(value))
65+
value = super().to_python(value)
66+
if self.max_length is not None and len(value) > self.max_length:
67+
# Normalization can increase the string length (e.g.
68+
# "ff" -> "ff", "½" -> "1⁄2") but cannot reduce it, so there is no
69+
# point in normalizing invalid data. Moreover, Unicode
70+
# normalization is very slow on Windows and can be a DoS attack
71+
# vector.
72+
return value
73+
return unicodedata.normalize("NFKC", value)
6674

6775
def widget_attrs(self, widget):
6876
return {

docs/releases/3.2.23.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,14 @@ Django 3.2.23 release notes
66

77
Django 3.2.23 fixes a security issue with severity "moderate" in 3.2.22.
88

9-
...
9+
CVE-2023-46695: Potential denial of service vulnerability in ``UsernameField`` on Windows
10+
=========================================================================================
11+
12+
The :func:`NFKC normalization <python:unicodedata.normalize>` is slow on
13+
Windows. As a consequence, ``django.contrib.auth.forms.UsernameField`` was
14+
subject to a potential denial of service attack via certain inputs with a very
15+
large number of Unicode characters.
16+
17+
In order to avoid the vulnerability, invalid values longer than
18+
``UsernameField.max_length`` are no longer normalized, since they cannot pass
19+
validation anyway.

tests/auth_tests/test_forms.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.contrib.auth.forms import (
66
AdminPasswordChangeForm, AuthenticationForm, PasswordChangeForm,
77
PasswordResetForm, ReadOnlyPasswordHashField, ReadOnlyPasswordHashWidget,
8-
SetPasswordForm, UserChangeForm, UserCreationForm,
8+
SetPasswordForm, UserChangeForm, UserCreationForm, UsernameField,
99
)
1010
from django.contrib.auth.models import User
1111
from django.contrib.auth.signals import user_login_failed
@@ -132,6 +132,12 @@ def test_normalize_username(self):
132132
self.assertNotEqual(user.username, ohm_username)
133133
self.assertEqual(user.username, 'testΩ') # U+03A9 GREEK CAPITAL LETTER OMEGA
134134

135+
def test_invalid_username_no_normalize(self):
136+
field = UsernameField(max_length=254)
137+
# Usernames are not normalized if they are too long.
138+
self.assertEqual(field.to_python("½" * 255), "½" * 255)
139+
self.assertEqual(field.to_python("ff" * 254), "ff" * 254)
140+
135141
def test_duplicate_normalized_unicode(self):
136142
"""
137143
To prevent almost identical usernames, visually identical but differing

0 commit comments

Comments
 (0)