rdobuilder c20473
From c811b37c65a4372a7ce613111d2a508c204f9833 Mon Sep 17 00:00:00 2001
rdobuilder c20473
From: Vinzent Steinberg <Vinzent.Steinberg@gmail.com>
rdobuilder c20473
Date: Wed, 10 Feb 2021 16:45:04 +0100
rdobuilder c20473
Subject: [PATCH 1/2] Fix ReDOS vulnerability
rdobuilder c20473
rdobuilder c20473
Fixes #548, with the workaround suggested by @yetingli.
rdobuilder c20473
---
rdobuilder c20473
 mpmath/ctx_mp.py             |  4 ++--
rdobuilder c20473
 mpmath/tests/test_convert.py | 10 ++++++++++
rdobuilder c20473
 2 files changed, 12 insertions(+), 2 deletions(-)
rdobuilder c20473
rdobuilder c20473
diff --git a/mpmath/ctx_mp.py b/mpmath/ctx_mp.py
rdobuilder c20473
index 39fc9411..93594dd4 100644
rdobuilder c20473
--- a/mpmath/ctx_mp.py
rdobuilder c20473
+++ b/mpmath/ctx_mp.py
rdobuilder c20473
@@ -42,8 +42,8 @@
rdobuilder c20473
 
rdobuilder c20473
 new = object.__new__
rdobuilder c20473
 
rdobuilder c20473
-get_complex = re.compile(r'^\(?(?P<re>[\+\-]?\d*\.?\d*(e[\+\-]?\d+)?)??'
rdobuilder c20473
-                         r'(?P<im>[\+\-]?\d*\.?\d*(e[\+\-]?\d+)?j)?\)?$')
rdobuilder c20473
+get_complex = re.compile(r'^\(?(?P<re>[\+\-]?\d*(\.\d*)?(e[\+\-]?\d+)?)??'
rdobuilder c20473
+                         r'(?P<im>[\+\-]?\d*(\.\d*)?(e[\+\-]?\d+)?j)?\)?$')
rdobuilder c20473
 
rdobuilder c20473
 if BACKEND == 'sage':
rdobuilder c20473
     from sage.libs.mpmath.ext_main import Context as BaseMPContext
rdobuilder c20473
diff --git a/mpmath/tests/test_convert.py b/mpmath/tests/test_convert.py
rdobuilder c20473
index 3e2f5559..cf1a91da 100644
rdobuilder c20473
--- a/mpmath/tests/test_convert.py
rdobuilder c20473
+++ b/mpmath/tests/test_convert.py
rdobuilder c20473
@@ -194,6 +194,16 @@ def test_mpmathify():
rdobuilder c20473
     assert mpmathify('(1.2e-10 - 3.4e5j)') == mpc('1.2e-10', '-3.4e5')
rdobuilder c20473
     assert mpmathify('1j') == mpc(1j)
rdobuilder c20473
 
rdobuilder c20473
+def test_issue548():
rdobuilder c20473
+    try:
rdobuilder c20473
+        # This expression is invalid, but may trigger the ReDOS vulnerability
rdobuilder c20473
+        # in the regular expression.
rdobuilder c20473
+        mpmathify('(' + '1' * 5000 + '!j')
rdobuilder c20473
+    except:
rdobuilder c20473
+        return
rdobuilder c20473
+    # The expression is invalid and should raise an exception.
rdobuilder c20473
+    assert False
rdobuilder c20473
+
rdobuilder c20473
 def test_compatibility():
rdobuilder c20473
     try:
rdobuilder c20473
         import numpy as np
rdobuilder c20473
rdobuilder c20473
From 2865c7d12b2a077d420427ad187eca831a48bff4 Mon Sep 17 00:00:00 2001
rdobuilder c20473
From: Vinzent Steinberg <Vinzent.Steinberg@gmail.com>
rdobuilder c20473
Date: Wed, 10 Feb 2021 16:47:57 +0100
rdobuilder c20473
Subject: [PATCH 2/2] Improve comment
rdobuilder c20473
rdobuilder c20473
---
rdobuilder c20473
 mpmath/tests/test_convert.py | 2 +-
rdobuilder c20473
 1 file changed, 1 insertion(+), 1 deletion(-)
rdobuilder c20473
rdobuilder c20473
diff --git a/mpmath/tests/test_convert.py b/mpmath/tests/test_convert.py
rdobuilder c20473
index cf1a91da..cb1db5b5 100644
rdobuilder c20473
--- a/mpmath/tests/test_convert.py
rdobuilder c20473
+++ b/mpmath/tests/test_convert.py
rdobuilder c20473
@@ -197,7 +197,7 @@ def test_mpmathify():
rdobuilder c20473
 def test_issue548():
rdobuilder c20473
     try:
rdobuilder c20473
         # This expression is invalid, but may trigger the ReDOS vulnerability
rdobuilder c20473
-        # in the regular expression.
rdobuilder c20473
+        # in the regular expression for parsing complex numbers.
rdobuilder c20473
         mpmathify('(' + '1' * 5000 + '!j')
rdobuilder c20473
     except:
rdobuilder c20473
         return