summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorCarlton Gibson <carlton.gibson@noumenal.es>2019-02-11 11:08:45 +0100
committerCarlton Gibson <carlton.gibson@noumenal.es>2019-02-11 11:10:38 +0100
commit83ab3e26647f6a50cdfac01ecf735cad540b2f35 (patch)
tree3353ac9f575fff93ea97d1b05a3e22b8ae86bbcd /django
parentb89d31d2dcb3d5566bc8f16dd058223153585899 (diff)
[2.2.x] Fixed CVE-2019-6975 -- Fixed memory exhaustion in utils.numberformat.format().
Thanks Sjoerd Job Postmus for the report and initial patch. Thanks Michael Manfre, Tim Graham, and Florian Apolloner for review. Backport of 402c0caa851e265410fbcaa55318f22d2bf22ee2 from master
Diffstat (limited to 'django')
-rw-r--r--django/utils/numberformat.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/django/utils/numberformat.py b/django/utils/numberformat.py
index 9c0496342d..88b35fc435 100644
--- a/django/utils/numberformat.py
+++ b/django/utils/numberformat.py
@@ -27,7 +27,20 @@ def format(number, decimal_sep, decimal_pos=None, grouping=0, thousand_sep='',
# sign
sign = ''
if isinstance(number, Decimal):
- str_number = '{:f}'.format(number)
+ # Format values with more than 200 digits (an arbitrary cutoff) using
+ # scientific notation to avoid high memory usage in {:f}'.format().
+ _, digits, exponent = number.as_tuple()
+ if abs(exponent) + len(digits) > 200:
+ number = '{:e}'.format(number)
+ coefficient, exponent = number.split('e')
+ # Format the coefficient.
+ coefficient = format(
+ coefficient, decimal_sep, decimal_pos, grouping,
+ thousand_sep, force_grouping, use_l10n,
+ )
+ return '{}e{}'.format(coefficient, exponent)
+ else:
+ str_number = '{:f}'.format(number)
else:
str_number = str(number)
if str_number[0] == '-':