summaryrefslogtreecommitdiff
path: root/django/utils/text.py
diff options
context:
space:
mode:
authorNatalia <124304+nessita@users.noreply.github.com>2026-01-21 15:24:55 -0300
committerJacob Walls <jacobtylerwalls@gmail.com>2026-02-03 08:25:31 -0500
commitb40cfc6052ced26dcd8166a58ea6f841d0d2cac8 (patch)
treeb9b1cfa7998a9065b0b0066a668a81107a48e875 /django/utils/text.py
parenta14363102d98fa29b8cced578eb3a0fadaa5bcb7 (diff)
[4.2.x] Fixed CVE-2026-1285 -- Mitigated potential DoS in django.utils.text.Truncator for HTML input.
The `TruncateHTMLParser` used `deque.remove()` to remove tags from the stack when processing end tags. With crafted input containing many unmatched end tags, this caused repeated full scans of the tag stack, leading to quadratic time complexity. The fix uses LIFO semantics, only removing a tag from the stack when it matches the most recently opened tag. This avoids linear scans for unmatched end tags and reduces complexity to linear time. Refs #30686 and 6ee37ada3241ed263d8d1c2901b030d964cbd161. Thanks Seokchan Yoon for the report. Backport of a33540b3e20b5d759aa8b2e4b9ca0e8edd285344 from main.
Diffstat (limited to 'django/utils/text.py')
-rw-r--r--django/utils/text.py14
1 files changed, 5 insertions, 9 deletions
diff --git a/django/utils/text.py b/django/utils/text.py
index b018f2601f..694baf1ac3 100644
--- a/django/utils/text.py
+++ b/django/utils/text.py
@@ -272,15 +272,11 @@ class Truncator(SimpleLazyObject):
if self_closing or tagname in html4_singlets:
pass
elif closing_tag:
- # Check for match in open tags list
- try:
- i = open_tags.index(tagname)
- except ValueError:
- pass
- else:
- # SGML: An end tag closes, back to the matching start tag,
- # all unclosed intervening start tags with omitted end tags
- open_tags = open_tags[i + 1 :]
+ # Remove from the list only if the tag matches the most
+ # recently opened tag (LIFO). This avoids O(n) linear scans
+ # for unmatched end tags if `list.index()` would be called.
+ if open_tags and open_tags[0] == tagname:
+ open_tags = open_tags[1:]
else:
# Add it to the start of the open tags list
open_tags.insert(0, tagname)