summaryrefslogtreecommitdiffstats
path: root/meta-python/recipes-devtools/python/python3-django/CVE-2023-43665.patch
diff options
context:
space:
mode:
Diffstat (limited to 'meta-python/recipes-devtools/python/python3-django/CVE-2023-43665.patch')
-rw-r--r--meta-python/recipes-devtools/python/python3-django/CVE-2023-43665.patch199
1 files changed, 199 insertions, 0 deletions
diff --git a/meta-python/recipes-devtools/python/python3-django/CVE-2023-43665.patch b/meta-python/recipes-devtools/python/python3-django/CVE-2023-43665.patch
new file mode 100644
index 0000000000..dbfb9b68a8
--- /dev/null
+++ b/meta-python/recipes-devtools/python/python3-django/CVE-2023-43665.patch
@@ -0,0 +1,199 @@
1From b269a0063e9b10a6c88c92b24d1b92c7421950de Mon Sep 17 00:00:00 2001
2From: Natalia <124304+nessita@users.noreply.github.com>
3Date: Wed, 29 Nov 2023 12:20:01 +0000
4Subject: [PATCH 1/2] Fixed CVE-2023-43665 -- Mitigated potential DoS in
5 django.utils.text.Truncator when truncating HTML text.
6
7Thanks Wenchao Li of Alibaba Group for the report.
8
9CVE: CVE-2023-43665
10
11Upstream-Status: Backport [https://github.com/django/django/commit/ccdade1a0262537868d7ca64374de3d957ca50c5]
12
13Signed-off-by: Narpat Mali <narpat.mali@windriver.com>
14---
15 django/utils/text.py | 18 ++++++++++++++++-
16 docs/ref/templates/builtins.txt | 20 +++++++++++++++++++
17 docs/releases/2.2.28.txt | 20 +++++++++++++++++++
18 tests/utils_tests/test_text.py | 35 ++++++++++++++++++++++++---------
19 4 files changed, 83 insertions(+), 10 deletions(-)
20
21diff --git a/django/utils/text.py b/django/utils/text.py
22index 1fae7b2..06a377b 100644
23--- a/django/utils/text.py
24+++ b/django/utils/text.py
25@@ -57,7 +57,14 @@ def wrap(text, width):
26 class Truncator(SimpleLazyObject):
27 """
28 An object used to truncate text, either by characters or words.
29+
30+ When truncating HTML text (either chars or words), input will be limited to
31+ at most `MAX_LENGTH_HTML` characters.
32 """
33+
34+ # 5 million characters are approximately 4000 text pages or 3 web pages.
35+ MAX_LENGTH_HTML = 5_000_000
36+
37 def __init__(self, text):
38 super().__init__(lambda: str(text))
39
40@@ -154,6 +161,11 @@ class Truncator(SimpleLazyObject):
41 if words and length <= 0:
42 return ''
43
44+ size_limited = False
45+ if len(text) > self.MAX_LENGTH_HTML:
46+ text = text[: self.MAX_LENGTH_HTML]
47+ size_limited = True
48+
49 html4_singlets = (
50 'br', 'col', 'link', 'base', 'img',
51 'param', 'area', 'hr', 'input'
52@@ -203,10 +215,14 @@ class Truncator(SimpleLazyObject):
53 # Add it to the start of the open tags list
54 open_tags.insert(0, tagname)
55
56+ truncate_text = self.add_truncation_text("", truncate)
57+
58 if current_len <= length:
59+ if size_limited and truncate_text:
60+ text += truncate_text
61 return text
62+
63 out = text[:end_text_pos]
64- truncate_text = self.add_truncation_text('', truncate)
65 if truncate_text:
66 out += truncate_text
67 # Close any tags still open
68diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
69index c4b0fa3..4faab38 100644
70--- a/docs/ref/templates/builtins.txt
71+++ b/docs/ref/templates/builtins.txt
72@@ -2318,6 +2318,16 @@ If ``value`` is ``"<p>Joel is a slug</p>"``, the output will be
73
74 Newlines in the HTML content will be preserved.
75
76+.. admonition:: Size of input string
77+
78+ Processing large, potentially malformed HTML strings can be
79+ resource-intensive and impact service performance. ``truncatechars_html``
80+ limits input to the first five million characters.
81+
82+.. versionchanged:: 2.2.28
83+
84+ In older versions, strings over five million characters were processed.
85+
86 .. templatefilter:: truncatewords
87
88 ``truncatewords``
89@@ -2356,6 +2366,16 @@ If ``value`` is ``"<p>Joel is a slug</p>"``, the output will be
90
91 Newlines in the HTML content will be preserved.
92
93+.. admonition:: Size of input string
94+
95+ Processing large, potentially malformed HTML strings can be
96+ resource-intensive and impact service performance. ``truncatewords_html``
97+ limits input to the first five million characters.
98+
99+.. versionchanged:: 2.2.28
100+
101+ In older versions, strings over five million characters were processed.
102+
103 .. templatefilter:: unordered_list
104
105 ``unordered_list``
106diff --git a/docs/releases/2.2.28.txt b/docs/releases/2.2.28.txt
107index 40eb230..6a38e9c 100644
108--- a/docs/releases/2.2.28.txt
109+++ b/docs/releases/2.2.28.txt
110@@ -56,3 +56,23 @@ CVE-2023-41164: Potential denial of service vulnerability in ``django.utils.enco
111 ``django.utils.encoding.uri_to_iri()`` was subject to potential denial of
112 service attack via certain inputs with a very large number of Unicode
113 characters.
114+
115+Backporting the CVE-2023-43665 fix on Django 2.2.28.
116+
117+CVE-2023-43665: Denial-of-service possibility in ``django.utils.text.Truncator``
118+================================================================================
119+
120+Following the fix for :cve:`2019-14232`, the regular expressions used in the
121+implementation of ``django.utils.text.Truncator``'s ``chars()`` and ``words()``
122+methods (with ``html=True``) were revised and improved. However, these regular
123+expressions still exhibited linear backtracking complexity, so when given a
124+very long, potentially malformed HTML input, the evaluation would still be
125+slow, leading to a potential denial of service vulnerability.
126+
127+The ``chars()`` and ``words()`` methods are used to implement the
128+:tfilter:`truncatechars_html` and :tfilter:`truncatewords_html` template
129+filters, which were thus also vulnerable.
130+
131+The input processed by ``Truncator``, when operating in HTML mode, has been
132+limited to the first five million characters in order to avoid potential
133+performance and memory issues.
134diff --git a/tests/utils_tests/test_text.py b/tests/utils_tests/test_text.py
135index 27e440b..cb3063d 100644
136--- a/tests/utils_tests/test_text.py
137+++ b/tests/utils_tests/test_text.py
138@@ -1,5 +1,6 @@
139 import json
140 import sys
141+from unittest.mock import patch
142
143 from django.core.exceptions import SuspiciousFileOperation
144 from django.test import SimpleTestCase
145@@ -87,11 +88,17 @@ class TestUtilsText(SimpleTestCase):
146 # lazy strings are handled correctly
147 self.assertEqual(text.Truncator(lazystr('The quick brown fox')).chars(10), 'The quick…')
148
149- def test_truncate_chars_html(self):
150+ @patch("django.utils.text.Truncator.MAX_LENGTH_HTML", 10_000)
151+ def test_truncate_chars_html_size_limit(self):
152+ max_len = text.Truncator.MAX_LENGTH_HTML
153+ bigger_len = text.Truncator.MAX_LENGTH_HTML + 1
154+ valid_html = "<p>Joel is a slug</p>" # 14 chars
155 perf_test_values = [
156- (('</a' + '\t' * 50000) + '//>', None),
157- ('&' * 50000, '&' * 9 + '…'),
158- ('_X<<<<<<<<<<<>', None),
159+ ("</a" + "\t" * (max_len - 6) + "//>", None),
160+ ("</p" + "\t" * bigger_len + "//>", "</p" + "\t" * 6 + "…"),
161+ ("&" * bigger_len, "&" * 9 + "…"),
162+ ("_X<<<<<<<<<<<>", None),
163+ (valid_html * bigger_len, "<p>Joel is a…</p>"), # 10 chars
164 ]
165 for value, expected in perf_test_values:
166 with self.subTest(value=value):
167@@ -149,15 +156,25 @@ class TestUtilsText(SimpleTestCase):
168 truncator = text.Truncator('<p>I &lt;3 python, what about you?</p>')
169 self.assertEqual('<p>I &lt;3 python,…</p>', truncator.words(3, html=True))
170
171+ @patch("django.utils.text.Truncator.MAX_LENGTH_HTML", 10_000)
172+ def test_truncate_words_html_size_limit(self):
173+ max_len = text.Truncator.MAX_LENGTH_HTML
174+ bigger_len = text.Truncator.MAX_LENGTH_HTML + 1
175+ valid_html = "<p>Joel is a slug</p>" # 4 words
176 perf_test_values = [
177- ('</a' + '\t' * 50000) + '//>',
178- '&' * 50000,
179- '_X<<<<<<<<<<<>',
180+ ("</a" + "\t" * (max_len - 6) + "//>", None),
181+ ("</p" + "\t" * bigger_len + "//>", "</p" + "\t" * (max_len - 3) + "…"),
182+ ("&" * max_len, None), # no change
183+ ("&" * bigger_len, "&" * max_len + "…"),
184+ ("_X<<<<<<<<<<<>", None),
185+ (valid_html * bigger_len, valid_html * 12 + "<p>Joel is…</p>"), # 50 words
186 ]
187- for value in perf_test_values:
188+ for value, expected in perf_test_values:
189 with self.subTest(value=value):
190 truncator = text.Truncator(value)
191- self.assertEqual(value, truncator.words(50, html=True))
192+ self.assertEqual(
193+ expected if expected else value, truncator.words(50, html=True)
194+ )
195
196 def test_wrap(self):
197 digits = '1234 67 9'
198--
1992.40.0