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

Skip to content

Commit 0b42f6a

Browse files
sarahboycenessita
authored andcommitted
[5.1.x] Fixed CVE-2025-32873 -- Mitigated potential DoS in strip_tags().
Thanks to Elias Myllymäki for the report, and Shai Berger and Jake Howard for the reviews. Co-authored-by: Natalia <[email protected]> Backport of 9f3419b from main.
1 parent 1520d18 commit 0b42f6a

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

django/utils/html.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141

4242
MAX_STRIP_TAGS_DEPTH = 50
4343

44+
# HTML tag that opens but has no closing ">" after 1k+ chars.
45+
long_open_tag_without_closing_re = _lazy_re_compile(r"<[a-zA-Z][^>]{1000,}")
46+
4447

4548
@keep_lazy(SafeString)
4649
def escape(text):
@@ -207,6 +210,9 @@ def _strip_once(value):
207210
def strip_tags(value):
208211
"""Return the given HTML with all tags stripped."""
209212
value = str(value)
213+
for long_open_tag in long_open_tag_without_closing_re.finditer(value):
214+
if long_open_tag.group().count("<") >= MAX_STRIP_TAGS_DEPTH:
215+
raise SuspiciousOperation
210216
# Note: in typical case this loop executes _strip_once twice (the second
211217
# execution does not remove any more tags).
212218
strip_tags_depth = 0

docs/releases/4.2.21.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ Django 4.2.21 release notes
77
Django 4.2.21 fixes a security issue with severity "moderate", a data loss bug,
88
and a regression in 4.2.20.
99

10+
CVE-2025-32873: Denial-of-service possibility in ``strip_tags()``
11+
=================================================================
12+
13+
:func:`~django.utils.html.strip_tags` would be slow to evaluate certain inputs
14+
containing large sequences of incomplete HTML tags. This function is used to
15+
implement the :tfilter:`striptags` template filter, which was thus also
16+
vulnerable.
17+
18+
:func:`~django.utils.html.strip_tags` now raises a :exc:`.SuspiciousOperation`
19+
exception if it encounters an unusually large number of unclosed opening tags.
20+
1021
Bugfixes
1122
========
1223

docs/releases/5.1.9.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ Django 5.1.9 release notes
77
Django 5.1.9 fixes a security issue with severity "moderate", a data loss bug,
88
and a regression in 5.1.8.
99

10+
CVE-2025-32873: Denial-of-service possibility in ``strip_tags()``
11+
=================================================================
12+
13+
:func:`~django.utils.html.strip_tags` would be slow to evaluate certain inputs
14+
containing large sequences of incomplete HTML tags. This function is used to
15+
implement the :tfilter:`striptags` template filter, which was thus also
16+
vulnerable.
17+
18+
:func:`~django.utils.html.strip_tags` now raises a :exc:`.SuspiciousOperation`
19+
exception if it encounters an unusually large number of unclosed opening tags.
20+
1021
Bugfixes
1122
========
1223

tests/utils_tests/test_html.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,30 @@ def test_strip_tags(self):
126126
("><!" + ("&" * 16000) + "D", "><!" + ("&" * 16000) + "D"),
127127
("X<<<<br>br>br>br>X", "XX"),
128128
("<" * 50 + "a>" * 50, ""),
129+
(">" + "<a" * 500 + "a", ">" + "<a" * 500 + "a"),
130+
("<a" * 49 + "a" * 951, "<a" * 49 + "a" * 951),
131+
("<" + "a" * 1_002, "<" + "a" * 1_002),
129132
)
130133
for value, output in items:
131134
with self.subTest(value=value, output=output):
132135
self.check_output(strip_tags, value, output)
133136
self.check_output(strip_tags, lazystr(value), output)
134137

135-
def test_strip_tags_suspicious_operation(self):
138+
def test_strip_tags_suspicious_operation_max_depth(self):
136139
value = "<" * 51 + "a>" * 51, "<a>"
137140
with self.assertRaises(SuspiciousOperation):
138141
strip_tags(value)
139142

143+
def test_strip_tags_suspicious_operation_large_open_tags(self):
144+
items = [
145+
">" + "<a" * 501,
146+
"<a" * 50 + "a" * 950,
147+
]
148+
for value in items:
149+
with self.subTest(value=value):
150+
with self.assertRaises(SuspiciousOperation):
151+
strip_tags(value)
152+
140153
def test_strip_tags_files(self):
141154
# Test with more lengthy content (also catching performance regressions)
142155
for filename in ("strip_tags1.html", "strip_tags2.txt"):

0 commit comments

Comments
 (0)