CVE-2024-27351: Potential regular expression denial-of-service in django.utils.text.Truncator.words()
The Django Truncator.words() method with html=True and the truncatewords_html template filter used regular expressions vulnerable to catastrophic backtracking. An attacker could supply a crafted string to cause a denial-of-service by consuming excessive CPU time.
- Triaged in 3 days
- Fixed in 24 days
- Paid in 24 days
- Disclosed after 54 days
Summary by the program
CVE-2024-27351: Potential regular expression denial-of-service in django.utils.text.Truncator.words() django.utils.text.Truncator.words() method (with html=True) and truncatewords_html template filter were subject to a potential regular expression denial-of-service attack using a suitably crafted string (follow up to CVE-2019-14232 and CVE-2023-43665).
Thanks Seokchan Yoon for the report.
This issue has severity "moderate" according to the Django security policy.
Full Security Advisory: https://www.djangoproject.com/weblog/2024/mar/04/security-releases/
The report
TL;DR
CVE-2024-27351: Potential regular expression denial-of-service in django.utils.text.Truncator.words()
Details:
django.utils.text.Truncator.words() method (with html=True) and truncatewords_html template filter were subject to a potential regular expression denial-of-service attack using a suitably crafted string (follow up to CVE-2019-14232 and CVE-2023-43665).
- The
Truncatorclass truncates text based on word count. - When the
htmlflag is set, the internal_truncate_html()method is used. - This method relies on regular expressions stored in variables (
re_charsandre_words) to perform the truncation. - These regular expressions are vulnerable to ReDoS attacks, which can cause significant performance degradation and denial-of-service.
PoC:
#!/usr/bin/env python3
from django.utils.text import Truncator
import time
MAX_LENGTH = 65535
payload = '<' * MAX_LENGTH
print('[INFO] %d bytes of payload' % len(payload))
start_time = time.time()
Truncator(payload).words(3, truncate='...', html=True) # BOOM!
end_time = time.time()
print('[INFO] Truncator().words() took %lf seconds' % (end_time - start_time))
The impact of this vulnerability may vary depending on the computing environment. In my tests using an AMD Ryzen 7 3700X with 32GB RAM, I observed a notable delay of approximately 40 seconds.
Impact
An attacker could exploit this vulnerability to:
- Consume excessive server resources, causing performance degradation and potential service outages.
- Delay application response times significantly.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely, the regular expressions used in the HTML truncation logic had nested quantifiers or ambiguous matching that allowed catastrophic backtracking on crafted input.
Impact
An attacker could cause significant delays (about 40 seconds in the reporter's test) and potentially service outages by consuming server resources.
What the attacker needed
Application uses Truncator.words() with html=True or…; attacker can supply the input string
The fix
The advisory links to Django security releases, but the text does not state a fix.
How it was found
- Import the Truncator class from django.utils.text.
- Create a string consisting of many '<' characters (e.g., 65535).
- Call Truncator(payload).words(3, truncate='...', html=True) and measure the time taken.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A regular expression is a pattern used to search text. Some patterns can take a very long time when given a specially crafted input, which is called a regular expression denial-of-service (ReDoS). Here, a string made of many '<' characters made Django's text truncation function run for about 40 seconds, which could slow down or crash a website.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.