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

Skip to content

Commit 3c9a277

Browse files
shaibfelixxm
andcommitted
[4.2.x] Fixed CVE-2024-27351 -- Prevented potential ReDoS in Truncator.words().
Thanks Seokchan Yoon for the report. Co-Authored-By: Mariusz Felisiak <[email protected]>
1 parent 7973951 commit 3c9a277

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

django/utils/text.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,61 @@ def capfirst(x):
2323
return x[0].upper() + x[1:]
2424

2525

26-
# Set up regular expressions
27-
re_words = _lazy_re_compile(r"<[^>]+?>|([^<>\s]+)", re.S)
26+
# ----- Begin security-related performance workaround -----
27+
28+
# We used to have, below
29+
#
30+
# re_words = _lazy_re_compile(r"<[^>]+?>|([^<>\s]+)", re.S)
31+
#
32+
# But it was shown that this regex, in the way we use it here, has some
33+
# catastrophic edge-case performance features. Namely, when it is applied to
34+
# text with only open brackets "<<<...". The class below provides the services
35+
# and correct answers for the use cases, but in these edge cases does it much
36+
# faster.
37+
re_notag = _lazy_re_compile(r"([^<>\s]+)", re.S)
38+
re_prt = _lazy_re_compile(r"<|([^<>\s]+)", re.S)
39+
40+
41+
class WordsRegex:
42+
@staticmethod
43+
def search(text, pos):
44+
# Look for "<" or a non-tag word.
45+
partial = re_prt.search(text, pos)
46+
if partial is None or partial[1] is not None:
47+
return partial
48+
49+
# "<" was found, look for a closing ">".
50+
end = text.find(">", partial.end(0))
51+
if end < 0:
52+
# ">" cannot be found, look for a word.
53+
return re_notag.search(text, pos + 1)
54+
else:
55+
# "<" followed by a ">" was found -- fake a match.
56+
end += 1
57+
return FakeMatch(text[partial.start(0) : end], end)
58+
59+
60+
class FakeMatch:
61+
__slots__ = ["_text", "_end"]
62+
63+
def end(self, group=0):
64+
assert group == 0, "This specific object takes only group=0"
65+
return self._end
66+
67+
def __getitem__(self, group):
68+
if group == 1:
69+
return None
70+
assert group == 0, "This specific object takes only group in {0,1}"
71+
return self._text
72+
73+
def __init__(self, text, end):
74+
self._text, self._end = text, end
75+
76+
77+
# ----- End security-related performance workaround -----
78+
79+
# Set up regular expressions.
80+
re_words = WordsRegex
2881
re_chars = _lazy_re_compile(r"<[^>]+?>|(.)", re.S)
2982
re_tag = _lazy_re_compile(r"<(/)?(\S+?)(?:(\s*/)|\s.*?)?>", re.S)
3083
re_newlines = _lazy_re_compile(r"\r\n|\r") # Used in normalize_newlines

docs/releases/3.2.25.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ Django 3.2.25 release notes
77
Django 3.2.25 fixes a security issue with severity "moderate" and a regression
88
in 3.2.24.
99

10+
CVE-2024-27351: Potential regular expression denial-of-service in ``django.utils.text.Truncator.words()``
11+
=========================================================================================================
12+
13+
``django.utils.text.Truncator.words()`` method (with ``html=True``) and
14+
:tfilter:`truncatewords_html` template filter were subject to a potential
15+
regular expression denial-of-service attack using a suitably crafted string
16+
(follow up to :cve:`2019-14232` and :cve:`2023-43665`).
17+
1018
Bugfixes
1119
========
1220

docs/releases/4.2.11.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ Django 4.2.11 release notes
77
Django 4.2.11 fixes a security issue with severity "moderate" and a regression
88
in 4.2.10.
99

10+
CVE-2024-27351: Potential regular expression denial-of-service in ``django.utils.text.Truncator.words()``
11+
=========================================================================================================
12+
13+
``django.utils.text.Truncator.words()`` method (with ``html=True``) and
14+
:tfilter:`truncatewords_html` template filter were subject to a potential
15+
regular expression denial-of-service attack using a suitably crafted string
16+
(follow up to :cve:`2019-14232` and :cve:`2023-43665`).
17+
1018
Bugfixes
1119
========
1220

tests/utils_tests/test_text.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,32 @@ def test_truncate_html_words(self):
183183
truncator = text.Truncator("<p>I &lt;3 python, what about you?</p>")
184184
self.assertEqual("<p>I &lt;3 python,…</p>", truncator.words(3, html=True))
185185

186+
# Only open brackets.
187+
test = "<" * 60_000
188+
truncator = text.Truncator(test)
189+
self.assertEqual(truncator.words(1, html=True), test)
190+
191+
# Tags with special chars in attrs.
192+
truncator = text.Truncator(
193+
"""<i style="margin: 5%; font: *;">Hello, my dear lady!</i>"""
194+
)
195+
self.assertEqual(
196+
"""<i style="margin: 5%; font: *;">Hello, my dear…</i>""",
197+
truncator.words(3, html=True),
198+
)
199+
200+
# Tags with special non-latin chars in attrs.
201+
truncator = text.Truncator("""<p data-x="א">Hello, my dear lady!</p>""")
202+
self.assertEqual(
203+
"""<p data-x="א">Hello, my dear…</p>""",
204+
truncator.words(3, html=True),
205+
)
206+
207+
# Misplaced brackets.
208+
truncator = text.Truncator("hello >< world")
209+
self.assertEqual(truncator.words(1, html=True), "hello…")
210+
self.assertEqual(truncator.words(2, html=True), "hello >< world")
211+
186212
@patch("django.utils.text.Truncator.MAX_LENGTH_HTML", 10_000)
187213
def test_truncate_words_html_size_limit(self):
188214
max_len = text.Truncator.MAX_LENGTH_HTML

0 commit comments

Comments
 (0)