Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba00bc5

Browse files
felixxmnessita
andcommitted
[4.1.x] Fixed CVE-2023-41164 -- Fixed potential DoS in django.utils.encoding.uri_to_iri().
Thanks MProgrammer (https://hackerone.com/mprogrammer) for the report. Co-authored-by: nessita <[email protected]>
1 parent 5253334 commit ba00bc5

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

django/utils/encoding.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,18 @@ def repercent_broken_unicode(path):
220220
repercent-encode any octet produced that is not part of a strictly legal
221221
UTF-8 octet sequence.
222222
"""
223+
changed_parts = []
223224
while True:
224225
try:
225226
path.decode()
226227
except UnicodeDecodeError as e:
227228
# CVE-2019-14235: A recursion shouldn't be used since the exception
228229
# handling uses massive amounts of memory
229230
repercent = quote(path[e.start : e.end], safe=b"/#%[]=:;$&()+,!?*@'~")
230-
path = path[: e.start] + repercent.encode() + path[e.end :]
231+
changed_parts.append(path[: e.start] + repercent.encode())
232+
path = path[e.end :]
231233
else:
232-
return path
234+
return b"".join(changed_parts) + path
233235

234236

235237
def filepath_to_uri(path):

docs/releases/3.2.21.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ Django 3.2.21 release notes
66

77
Django 3.2.21 fixes a security issue with severity "moderate" in 3.2.20.
88

9-
...
9+
CVE-2023-41164: Potential denial of service vulnerability in ``django.utils.encoding.uri_to_iri()``
10+
===================================================================================================
11+
12+
``django.utils.encoding.uri_to_iri()`` was subject to potential denial of
13+
service attack via certain inputs with a very large number of Unicode
14+
characters.

docs/releases/4.1.11.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ Django 4.1.11 release notes
66

77
Django 4.1.11 fixes a security issue with severity "moderate" in 4.1.10.
88

9-
...
9+
CVE-2023-41164: Potential denial of service vulnerability in ``django.utils.encoding.uri_to_iri()``
10+
===================================================================================================
11+
12+
``django.utils.encoding.uri_to_iri()`` was subject to potential denial of
13+
service attack via certain inputs with a very large number of Unicode
14+
characters.

tests/utils_tests/test_encoding.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import datetime
2+
import inspect
23
import sys
34
import unittest
45
from pathlib import Path
56
from unittest import mock
6-
from urllib.parse import quote_plus
7+
from urllib.parse import quote, quote_plus
78

89
from django.test import SimpleTestCase
910
from django.utils.encoding import (
@@ -120,6 +121,24 @@ def test_repercent_broken_unicode_recursion_error(self):
120121
except RecursionError:
121122
self.fail("Unexpected RecursionError raised.")
122123

124+
def test_repercent_broken_unicode_small_fragments(self):
125+
data = b"test\xfctest\xfctest\xfc"
126+
decoded_paths = []
127+
128+
def mock_quote(*args, **kwargs):
129+
# The second frame is the call to repercent_broken_unicode().
130+
decoded_paths.append(inspect.currentframe().f_back.f_locals["path"])
131+
return quote(*args, **kwargs)
132+
133+
with mock.patch("django.utils.encoding.quote", mock_quote):
134+
self.assertEqual(repercent_broken_unicode(data), b"test%FCtest%FCtest%FC")
135+
136+
# decode() is called on smaller fragment of the path each time.
137+
self.assertEqual(
138+
decoded_paths,
139+
[b"test\xfctest\xfctest\xfc", b"test\xfctest\xfc", b"test\xfc"],
140+
)
141+
123142

124143
class TestRFC3987IEncodingUtils(unittest.TestCase):
125144
def test_filepath_to_uri(self):

0 commit comments

Comments
 (0)