Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit87d3daa

Browse files
committed
bpo-32997: Fix REDOS in fpformat
The regex to decode a number in fpformat is susceptible tocatastrophic backtracking.This is a potential DOS vector if a server is using fpformat onuntrusted number strings.Replace it with an equivalent non-vulnerable regex.The match behavior of the new regex is slightly different.This difference is addressed with a follow-up check.
1 parent8a7f1f4 commit87d3daa

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

‎Lib/fpformat.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
__all__= ["fix","sci","NotANumber"]
2020

2121
# Compiled regular expression to "decode" a number
22-
decoder=re.compile(r'^([-+]?)0*(\d*)((?:\.\d*)?)(([eE][-+]?\d+)?)$')
22+
decoder=re.compile(r'^([-+]?)0*([1-9]\d*)?((?:\.\d*)?)(([eE][-+]?\d+)?)$')
2323
# \0 the whole thing
2424
# \1 leading sign or empty
2525
# \2 digits left of decimal point
@@ -41,6 +41,8 @@ def extract(s):
4141
res=decoder.match(s)
4242
ifresisNone:raiseNotANumber,s
4343
sign,intpart,fraction,exppart=res.group(1,2,3,4)
44+
ifintpartisNone:# ([1-9]\d*)? may match nothing
45+
intpart=''
4446
ifsign=='+':sign=''
4547
iffraction:fraction=fraction[1:]
4648
ifexppart:expo=int(exppart[1:])

‎Lib/test/test_fpformat.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ def test_failing_values(self):
6767
else:
6868
self.fail("No exception on non-numeric sci")
6969

70+
deftest_REDOS(self):
71+
# This attack string will hang on the old decoder pattern.
72+
attack='+0'+ ('0'*1000000)+'++'
73+
digs=5# irrelevant
74+
75+
# fix returns input if it does not decode
76+
self.assertEqual(fpformat.fix(attack,digs),attack)
77+
# sci raises NotANumber
78+
withself.assertRaises(NotANumber):
79+
fpformat.sci(attack,digs)
7080

7181
deftest_main():
7282
run_unittest(FpformatTest)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
A regex in fpformat was vulnerable to catastrophic backtracking. This regex
2+
was a potential DOS vector (REDOS). Based on typical uses of fpformat the
3+
risk seems low. The regex has been refactored and is now safe. Patch by
4+
Jamie Davis.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp