summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Apolloner <florian@apolloner.eu>2019-07-19 17:04:53 +0200
committerCarlton Gibson <carlton.gibson@noumenal.es>2019-07-31 12:43:32 +0200
commit5d50a2e5fa36ad23ab532fc54cf4073de84b3306 (patch)
tree941ae9629ac864d2d6999e9bdd18562f4b6dce64
parentf74b3ae3628c26e1b4f8db3d13a91d52a833a975 (diff)
[2.1.x] Fixed CVE-2019-14235 -- Fixed potential memory exhaustion in django.utils.encoding.uri_to_iri().
Thanks to Guido Vranken for initial report.
-rw-r--r--django/utils/encoding.py17
-rw-r--r--docs/releases/1.11.23.txt10
-rw-r--r--docs/releases/2.1.11.txt10
-rw-r--r--tests/utils_tests/test_encoding.py14
4 files changed, 42 insertions, 9 deletions
diff --git a/django/utils/encoding.py b/django/utils/encoding.py
index 4ff530c406..fd63c8d21c 100644
--- a/django/utils/encoding.py
+++ b/django/utils/encoding.py
@@ -228,13 +228,16 @@ def repercent_broken_unicode(path):
repercent-encode any octet produced that is not part of a strictly legal
UTF-8 octet sequence.
"""
- try:
- path.decode()
- except UnicodeDecodeError as e:
- repercent = quote(path[e.start:e.end], safe=b"/#%[]=:;$&()+,!?*@'~")
- path = repercent_broken_unicode(
- path[:e.start] + force_bytes(repercent) + path[e.end:])
- return path
+ while True:
+ try:
+ path.decode()
+ except UnicodeDecodeError as e:
+ # CVE-2019-14235: A recursion shouldn't be used since the exception
+ # handling uses massive amounts of memory
+ repercent = quote(path[e.start:e.end], safe=b"/#%[]=:;$&()+,!?*@'~")
+ path = path[:e.start] + force_bytes(repercent) + path[e.end:]
+ else:
+ return path
def filepath_to_uri(path):
diff --git a/docs/releases/1.11.23.txt b/docs/releases/1.11.23.txt
index 03b33ebf63..04acca90f1 100644
--- a/docs/releases/1.11.23.txt
+++ b/docs/releases/1.11.23.txt
@@ -45,3 +45,13 @@ CVE-2019-14234: SQL injection possibility in key and index lookups for ``JSONFie
<hstorefield.key>` for :class:`~django.contrib.postgres.fields.HStoreField`
were subject to SQL injection, using a suitably crafted dictionary, with
dictionary expansion, as the ``**kwargs`` passed to ``QuerySet.filter()``.
+
+CVE-2019-14235: Potential memory exhaustion in ``django.utils.encoding.uri_to_iri()``
+=====================================================================================
+
+If passed certain inputs, :func:`django.utils.encoding.uri_to_iri` could lead
+to significant memory usage due to excessive recursion when re-percent-encoding
+invalid UTF-8 octet sequences.
+
+``uri_to_iri()`` now avoids recursion when re-percent-encoding invalid UTF-8
+octet sequences.
diff --git a/docs/releases/2.1.11.txt b/docs/releases/2.1.11.txt
index 0de4175b5f..ae344f35b3 100644
--- a/docs/releases/2.1.11.txt
+++ b/docs/releases/2.1.11.txt
@@ -45,3 +45,13 @@ CVE-2019-14234: SQL injection possibility in key and index lookups for ``JSONFie
<hstorefield.key>` for :class:`~django.contrib.postgres.fields.HStoreField`
were subject to SQL injection, using a suitably crafted dictionary, with
dictionary expansion, as the ``**kwargs`` passed to ``QuerySet.filter()``.
+
+CVE-2019-14235: Potential memory exhaustion in ``django.utils.encoding.uri_to_iri()``
+=====================================================================================
+
+If passed certain inputs, :func:`django.utils.encoding.uri_to_iri` could lead
+to significant memory usage due to excessive recursion when re-percent-encoding
+invalid UTF-8 octet sequences.
+
+``uri_to_iri()`` now avoids recursion when re-percent-encoding invalid UTF-8
+octet sequences.
diff --git a/tests/utils_tests/test_encoding.py b/tests/utils_tests/test_encoding.py
index bca6549fe7..1a279f66c2 100644
--- a/tests/utils_tests/test_encoding.py
+++ b/tests/utils_tests/test_encoding.py
@@ -1,4 +1,5 @@
import datetime
+import sys
import unittest
from unittest import mock
from urllib.parse import quote_plus
@@ -6,8 +7,8 @@ from urllib.parse import quote_plus
from django.test import SimpleTestCase
from django.utils.encoding import (
DjangoUnicodeDecodeError, escape_uri_path, filepath_to_uri, force_bytes,
- force_text, get_system_encoding, iri_to_uri, smart_bytes, smart_text,
- uri_to_iri,
+ force_text, get_system_encoding, iri_to_uri, repercent_broken_unicode,
+ smart_bytes, smart_text, uri_to_iri,
)
from django.utils.functional import SimpleLazyObject
from django.utils.translation import gettext_lazy
@@ -86,6 +87,15 @@ class TestEncodingUtils(SimpleTestCase):
with mock.patch('locale.getdefaultlocale', side_effect=Exception):
self.assertEqual(get_system_encoding(), 'ascii')
+ def test_repercent_broken_unicode_recursion_error(self):
+ # Prepare a string long enough to force a recursion error if the tested
+ # function uses recursion.
+ data = b'\xfc' * sys.getrecursionlimit()
+ try:
+ self.assertEqual(repercent_broken_unicode(data), b'%FC' * sys.getrecursionlimit())
+ except RecursionError:
+ self.fail('Unexpected RecursionError raised.')
+
class TestRFC3987IEncodingUtils(unittest.TestCase):