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