File CVE-2023-41164.patch of Package python-Django1

From c91849d6fa7625f0337940d1b47980374ac30ef9 Mon Sep 17 00:00:00 2001
From: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Date: Tue, 22 Aug 2023 08:53:03 +0200
Subject: [PATCH] [3.2.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 <124304+nessita@users.noreply.github.com>
---
diff --git a/django/utils/encoding.py b/django/utils/encoding.py
index a29ef2be58..60581a6db3 100644
--- a/django/utils/encoding.py
+++ b/django/utils/encoding.py
@@ -237,6 +237,7 @@ def repercent_broken_unicode(path):
     we need to re-percent-encode any octet produced that is not part of a
     strictly legal UTF-8 octet sequence.
     """
+    changed_parts = []
     while True:
         try:
             path.decode('utf-8')
@@ -244,10 +245,10 @@ def repercent_broken_unicode(path):
             # 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:]
+            changed_parts.append(path[:e.start] + repercent.encode())
+            path = path[e.end:]
         else:
-            return path
-
+            return b"".join(changed_parts) + path
 
 def filepath_to_uri(path):
     """Convert a file system path to a URI portion that is suitable for
diff --git a/tests/utils_tests/test_encoding.py b/tests/utils_tests/test_encoding.py
index 2b4bcff870..b1a5176006 100644
--- a/tests/utils_tests/test_encoding.py
+++ b/tests/utils_tests/test_encoding.py
@@ -2,9 +2,11 @@
 from __future__ import unicode_literals
 
 import datetime
+import inspect
 import sys
 import unittest
 
+from django.test import mock
 from django.utils import six
 from django.utils.encoding import (
     escape_uri_path, filepath_to_uri, force_bytes, force_text, iri_to_uri,
@@ -12,6 +14,7 @@ from django.utils.encoding import (
 )
 from django.utils.functional import SimpleLazyObject
 from django.utils.http import urlquote_plus
+from django.utils.six.moves.urllib.parse import quote
 
 
 class TestEncodingUtils(unittest.TestCase):
@@ -86,6 +89,24 @@ class TestEncodingUtils(unittest.TestCase):
         except RecursionError:
             self.fail('Unexpected RecursionError raised.')
 
+    def test_repercent_broken_unicode_small_fragments(self):
+        data = b"test\xfctest\xfctest\xfc"
+        decoded_paths = []
+
+        def mock_quote(*args, **kwargs):
+            # The second frame is the call to repercent_broken_unicode().
+            decoded_paths.append(inspect.stack()[1][0].f_locals["path"])
+            return quote(*args, **kwargs)
+
+        with mock.patch("django.utils.encoding.quote", mock_quote):
+            self.assertEqual(repercent_broken_unicode(data), b"test%FCtest%FCtest%FC")
+
+        # decode() is called on smaller fragment of the path each time.
+        self.assertEqual(
+            decoded_paths,
+            [b"test\xfctest\xfctest\xfc", b"test\xfctest\xfc", b"test\xfc"],
+        )
+
 
 class TestRFC3987IEncodingUtils(unittest.TestCase):

-- 
2.34.1
openSUSE Build Service is sponsored by