티스토리 수익 글 보기

티스토리 수익 글 보기

[1.11.x] Fixed CVE-2019-14235 — Fixed potential memory exhaustion in… · django/django@869b34e · GitHub
Skip to content
/ django Public

Commit 869b34e

Browse files
apollo13carltongibson
authored andcommitted
[1.11.x] Fixed CVE-2019-14235 — Fixed potential memory exhaustion in django.utils.encoding.uri_to_iri().
Thanks to Guido Vranken for initial report.
1 parent ed682a2 commit 869b34e

File tree

3 files changed

+31
8
lines changed

3 files changed

+31
8
lines changed

django/utils/encoding.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,16 @@ def repercent_broken_unicode(path):
237237
we need to re-percent-encode any octet produced that is not part of a
238238
strictly legal UTF-8 octet sequence.
239239
"""
240-
try:
241-
path.decode('utf-8')
242-
except UnicodeDecodeError as e:
243-
repercent = quote(path[e.start:e.end], safe=b"/#%[]=:;$&()+,!?*@'~")
244-
path = repercent_broken_unicode(
245-
path[:e.start] + force_bytes(repercent) + path[e.end:])
246-
return path
240+
while True:
241+
try:
242+
path.decode('utf-8')
243+
except UnicodeDecodeError as e:
244+
# CVE-2019-14235: A recursion shouldn't be used since the exception
245+
# handling uses massive amounts of memory
246+
repercent = quote(path[e.start:e.end], safe=b"/#%[]=:;$&()+,!?*@'~")
247+
path = path[:e.start] + force_bytes(repercent) + path[e.end:]
248+
else:
249+
return path
247250

248251

249252
def filepath_to_uri(path):

docs/releases/1.11.23.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,13 @@ CVE-2019-14234: SQL injection possibility in key and index lookups for ``JSONFie
4545
<hstorefield.key>` for :class:`~django.contrib.postgres.fields.HStoreField`
4646
were subject to SQL injection, using a suitably crafted dictionary, with
4747
dictionary expansion, as the ``**kwargs`` passed to ``QuerySet.filter()``.
48+
49+
CVE-2019-14235: Potential memory exhaustion in ``django.utils.encoding.uri_to_iri()``
50+
=====================================================================================
51+
52+
If passed certain inputs, :func:`django.utils.encoding.uri_to_iri` could lead
53+
to significant memory usage due to excessive recursion when re-percent-encoding
54+
invalid UTF-8 octet sequences.
55+
56+
``uri_to_iri()`` now avoids recursion when re-percent-encoding invalid UTF-8
57+
octet sequences.

tests/utils_tests/test_encoding.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
from __future__ import unicode_literals
33

44
import datetime
5+
import sys
56
import unittest
67

78
from django.utils import six
89
from django.utils.encoding import (
910
escape_uri_path, filepath_to_uri, force_bytes, force_text, iri_to_uri,
10-
smart_text, uri_to_iri,
11+
repercent_broken_unicode, smart_text, uri_to_iri,
1112
)
1213
from django.utils.functional import SimpleLazyObject
1314
from django.utils.http import urlquote_plus
@@ -76,6 +77,15 @@ def __unicode__(self):
7677
self.assertEqual(smart_text(1), '1')
7778
self.assertEqual(smart_text('foo'), 'foo')
7879

80+
def test_repercent_broken_unicode_recursion_error(self):
81+
# Prepare a string long enough to force a recursion error if the tested
82+
# function uses recursion.
83+
data = b'\xfc' * sys.getrecursionlimit()
84+
try:
85+
self.assertEqual(repercent_broken_unicode(data), b'%FC' * sys.getrecursionlimit())
86+
except RecursionError:
87+
self.fail('Unexpected RecursionError raised.')
88+
7989

8090
class TestRFC3987IEncodingUtils(unittest.TestCase):
8191

0 commit comments

Comments
 (0)