summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-07-12 11:38:34 +0200
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-07-31 16:11:59 +0200
commitfc76660f589ac07e45e9cd34ccb8087aeb11904b (patch)
tree2bcde8a31c4ecdda7dcd2e33609f4c1d9a5ef6a1 /django
parent7b1a76f899f7a7acb7d70b433cb0064c2184186b (diff)
[4.2.x] Fixed CVE-2024-41989 -- Prevented excessive memory consumption in floatformat.
Thanks Elias Myllymäki for the report. Co-authored-by: Shai Berger <shai@platonix.com>
Diffstat (limited to 'django')
-rw-r--r--django/template/defaultfilters.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
index d446b54ade..3f89eba6bb 100644
--- a/django/template/defaultfilters.py
+++ b/django/template/defaultfilters.py
@@ -163,6 +163,19 @@ def floatformat(text, arg=-1):
except ValueError:
return input_val
+ _, digits, exponent = d.as_tuple()
+ try:
+ number_of_digits_and_exponent_sum = len(digits) + abs(exponent)
+ except TypeError:
+ # Exponent values can be "F", "n", "N".
+ number_of_digits_and_exponent_sum = 0
+
+ # Values with more than 200 digits, or with a large exponent, are returned "as is"
+ # to avoid high memory consumption and potential denial-of-service attacks.
+ # The cut-off of 200 is consistent with django.utils.numberformat.floatformat().
+ if number_of_digits_and_exponent_sum > 200:
+ return input_val
+
try:
m = int(d) - d
except (ValueError, OverflowError, InvalidOperation):