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

Skip to content

[2.7] bpo-32997: Fix REDOS in fpformat #5984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lib/fpformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
__all__ = ["fix","sci","NotANumber"]

# Compiled regular expression to "decode" a number
decoder = re.compile(r'^([-+]?)0*(\d*)((?:\.\d*)?)(([eE][-+]?\d+)?)$')
decoder = re.compile(r'^([-+]?)(\d*)((?:\.\d*)?)(([eE][-+]?\d+)?)$')
# \0 the whole thing
# \1 leading sign or empty
# \2 digits left of decimal point
Expand All @@ -41,6 +41,7 @@ def extract(s):
res = decoder.match(s)
if res is None: raise NotANumber, s
sign, intpart, fraction, exppart = res.group(1,2,3,4)
intpart = intpart.lstrip('0');
if sign == '+': sign = ''
if fraction: fraction = fraction[1:]
if exppart: expo = int(exppart[1:])
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_fpformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ def test_failing_values(self):
else:
self.fail("No exception on non-numeric sci")

def test_REDOS(self):
# This attack string will hang on the old decoder pattern.
attack = '+0' + ('0' * 1000000) + '++'
digs = 5 # irrelevant

# fix returns input if it does not decode
self.assertEqual(fpformat.fix(attack, digs), attack)
# sci raises NotANumber
with self.assertRaises(NotANumber):
fpformat.sci(attack, digs)

def test_main():
run_unittest(FpformatTest)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
A regex in fpformat was vulnerable to catastrophic backtracking. This regex
was a potential DOS vector (REDOS). Based on typical uses of fpformat the
risk seems low. The regex has been refactored and is now safe. Patch by
Jamie Davis.