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

Skip to content

Commit a5a89ea

Browse files
committed
[5.0.x] Fixed CVE-2024-53907 -- Mitigated potential DoS in strip_tags().
Thanks to jiangniao for the report, and Shai Berger and Natalia Bidart for the reviews.
1 parent baf63eb commit a5a89ea

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

django/utils/html.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from html.parser import HTMLParser
88
from urllib.parse import parse_qsl, quote, unquote, urlencode, urlsplit, urlunsplit
99

10+
from django.core.exceptions import SuspiciousOperation
1011
from django.utils.deprecation import RemovedInDjango60Warning
1112
from django.utils.encoding import punycode
1213
from django.utils.functional import Promise, cached_property, keep_lazy, keep_lazy_text
@@ -37,6 +38,7 @@
3738
}
3839

3940
MAX_URL_LENGTH = 2048
41+
MAX_STRIP_TAGS_DEPTH = 50
4042

4143

4244
@keep_lazy(SafeString)
@@ -202,15 +204,19 @@ def _strip_once(value):
202204
@keep_lazy_text
203205
def strip_tags(value):
204206
"""Return the given HTML with all tags stripped."""
205-
# Note: in typical case this loop executes _strip_once once. Loop condition
206-
# is redundant, but helps to reduce number of executions of _strip_once.
207207
value = str(value)
208+
# Note: in typical case this loop executes _strip_once twice (the second
209+
# execution does not remove any more tags).
210+
strip_tags_depth = 0
208211
while "<" in value and ">" in value:
212+
if strip_tags_depth >= MAX_STRIP_TAGS_DEPTH:
213+
raise SuspiciousOperation
209214
new_value = _strip_once(value)
210215
if value.count("<") == new_value.count("<"):
211216
# _strip_once wasn't able to detect more tags.
212217
break
213218
value = new_value
219+
strip_tags_depth += 1
214220
return value
215221

216222

docs/releases/4.2.17.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,19 @@ Django 4.2.17 release notes
66

77
Django 4.2.17 fixes one security issue with severity "high" and one security
88
issue with severity "moderate" in 4.2.16.
9+
10+
CVE-2024-53907: Denial-of-service possibility in ``strip_tags()``
11+
=================================================================
12+
13+
:func:`~django.utils.html.strip_tags` would be extremely slow to evaluate
14+
certain inputs containing large sequences of nested incomplete HTML entities.
15+
The ``strip_tags()`` method is used to implement the corresponding
16+
:tfilter:`striptags` template filter, which was thus also vulnerable.
17+
18+
``strip_tags()`` now has an upper limit of recursive calls to ``HTMLParser``
19+
before raising a :exc:`.SuspiciousOperation` exception.
20+
21+
Remember that absolutely NO guarantee is provided about the results of
22+
``strip_tags()`` being HTML safe. So NEVER mark safe the result of a
23+
``strip_tags()`` call without escaping it first, for example with
24+
:func:`django.utils.html.escape`.

docs/releases/5.0.10.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,19 @@ Django 5.0.10 release notes
66

77
Django 5.0.10 fixes one security issue with severity "high" and one security
88
issue with severity "moderate" in 5.0.9.
9+
10+
CVE-2024-53907: Denial-of-service possibility in ``strip_tags()``
11+
=================================================================
12+
13+
:func:`~django.utils.html.strip_tags` would be extremely slow to evaluate
14+
certain inputs containing large sequences of nested incomplete HTML entities.
15+
The ``strip_tags()`` method is used to implement the corresponding
16+
:tfilter:`striptags` template filter, which was thus also vulnerable.
17+
18+
``strip_tags()`` now has an upper limit of recursive calls to ``HTMLParser``
19+
before raising a :exc:`.SuspiciousOperation` exception.
20+
21+
Remember that absolutely NO guarantee is provided about the results of
22+
``strip_tags()`` being HTML safe. So NEVER mark safe the result of a
23+
``strip_tags()`` call without escaping it first, for example with
24+
:func:`django.utils.html.escape`.

tests/utils_tests/test_html.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
from datetime import datetime
33

4+
from django.core.exceptions import SuspiciousOperation
45
from django.core.serializers.json import DjangoJSONEncoder
56
from django.test import SimpleTestCase
67
from django.utils.deprecation import RemovedInDjango60Warning
@@ -123,12 +124,18 @@ def test_strip_tags(self):
123124
("<script>alert()</script>&h", "alert()h"),
124125
("><!" + ("&" * 16000) + "D", "><!" + ("&" * 16000) + "D"),
125126
("X<<<<br>br>br>br>X", "XX"),
127+
("<" * 50 + "a>" * 50, ""),
126128
)
127129
for value, output in items:
128130
with self.subTest(value=value, output=output):
129131
self.check_output(strip_tags, value, output)
130132
self.check_output(strip_tags, lazystr(value), output)
131133

134+
def test_strip_tags_suspicious_operation(self):
135+
value = "<" * 51 + "a>" * 51, "<a>"
136+
with self.assertRaises(SuspiciousOperation):
137+
strip_tags(value)
138+
132139
def test_strip_tags_files(self):
133140
# Test with more lengthy content (also catching performance regressions)
134141
for filename in ("strip_tags1.html", "strip_tags2.txt"):

0 commit comments

Comments
 (0)