b09f05
From 0fcfa065d900040c80628b31b8b6ea606c131086 Mon Sep 17 00:00:00 2001
b09f05
From: Paul Ivanov <pivanov5@bloomberg.net>
b09f05
Date: Wed, 30 Jan 2019 14:22:44 -0800
b09f05
Subject: [PATCH] BUG: load fails when using pickle without allow_pickle=True
b09f05
b09f05
a partial mitigation of #12759.
b09f05
b09f05
see also https://nvd.nist.gov/vuln/detail/CVE-2019-6446
b09f05
---
b09f05
 numpy/core/tests/test_regression.py |  2 +-
b09f05
 numpy/lib/format.py                 |  8 ++++++--
b09f05
 numpy/lib/npyio.py                  | 17 ++++++++++++-----
b09f05
 numpy/lib/tests/test_format.py      | 15 +++++++++------
b09f05
 numpy/lib/tests/test_io.py          |  2 +-
b09f05
 5 files changed, 29 insertions(+), 15 deletions(-)
b09f05
b09f05
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
b09f05
index a3b0114..2be6bf3 100644
b09f05
--- a/numpy/core/tests/test_regression.py
b09f05
+++ b/numpy/core/tests/test_regression.py
b09f05
@@ -96,7 +96,7 @@ class TestRegression(object):
b09f05
         ca = np.char.array(np.arange(1000, 1010), itemsize=4)
b09f05
         ca.dump(f)
b09f05
         f.seek(0)
b09f05
-        ca = np.load(f)
b09f05
+        ca = np.load(f, allow_pickle=True)
b09f05
         f.close()
b09f05
 
b09f05
     def test_noncontiguous_fill(self):
b09f05
diff --git a/numpy/lib/format.py b/numpy/lib/format.py
b09f05
index 363bb21..b91142c 100644
b09f05
--- a/numpy/lib/format.py
b09f05
+++ b/numpy/lib/format.py
b09f05
@@ -602,7 +602,7 @@ def write_array(fp, array, version=None, allow_pickle=True, pickle_kwargs=None):
b09f05
                 fp.write(chunk.tobytes('C'))
b09f05
 
b09f05
 
b09f05
-def read_array(fp, allow_pickle=True, pickle_kwargs=None):
b09f05
+def read_array(fp, allow_pickle=False, pickle_kwargs=None):
b09f05
     """
b09f05
     Read an array from an NPY file.
b09f05
 
b09f05
@@ -612,7 +612,11 @@ def read_array(fp, allow_pickle=True, pickle_kwargs=None):
b09f05
         If this is not a real file object, then this may take extra memory
b09f05
         and time.
b09f05
     allow_pickle : bool, optional
b09f05
-        Whether to allow reading pickled data. Default: True
b09f05
+        Whether to allow writing pickled data. Default: False
b09f05
+
b09f05
+        .. versionchanged:: 1.14.2
b09f05
+            Made default False in response to CVE-2019-6446.
b09f05
+
b09f05
     pickle_kwargs : dict
b09f05
         Additional keyword arguments to pass to pickle.load. These are only
b09f05
         useful when loading object arrays saved on Python 2 when using
b09f05
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
b09f05
index 76b135c..c6522f5 100644
b09f05
--- a/numpy/lib/npyio.py
b09f05
+++ b/numpy/lib/npyio.py
b09f05
@@ -130,7 +130,11 @@ class NpzFile(object):
b09f05
         An object on which attribute can be performed as an alternative
b09f05
         to getitem access on the `NpzFile` instance itself.
b09f05
     allow_pickle : bool, optional
b09f05
-        Allow loading pickled data. Default: True
b09f05
+        Allow loading pickled data. Default: False
b09f05
+
b09f05
+        .. versionchanged:: 1.14.2
b09f05
+            Made default False in response to CVE-2019-6446.
b09f05
+
b09f05
     pickle_kwargs : dict, optional
b09f05
         Additional keyword arguments to pass on to pickle.load.
b09f05
         These are only useful when loading object arrays saved on
b09f05
@@ -166,7 +170,7 @@ class NpzFile(object):
b09f05
 
b09f05
     """
b09f05
 
b09f05
-    def __init__(self, fid, own_fid=False, allow_pickle=True,
b09f05
+    def __init__(self, fid, own_fid=False, allow_pickle=False,
b09f05
                  pickle_kwargs=None):
b09f05
         # Import is postponed to here since zipfile depends on gzip, an
b09f05
         # optional component of the so-called standard library.
b09f05
@@ -265,7 +269,7 @@ class NpzFile(object):
b09f05
         return self.files.__contains__(key)
b09f05
 
b09f05
 
b09f05
-def load(file, mmap_mode=None, allow_pickle=True, fix_imports=True,
b09f05
+def load(file, mmap_mode=None, allow_pickle=False, fix_imports=True,
b09f05
          encoding='ASCII'):
b09f05
     """
b09f05
     Load arrays or pickled objects from ``.npy``, ``.npz`` or pickled files.
b09f05
@@ -287,8 +291,11 @@ def load(file, mmap_mode=None, allow_pickle=True, fix_imports=True,
b09f05
         Allow loading pickled object arrays stored in npy files. Reasons for
b09f05
         disallowing pickles include security, as loading pickled data can
b09f05
         execute arbitrary code. If pickles are disallowed, loading object
b09f05
-        arrays will fail.
b09f05
-        Default: True
b09f05
+        arrays will fail. Default: False
b09f05
+
b09f05
+        .. versionchanged:: 1.14.2
b09f05
+            Made default False in response to CVE-2019-6446.
b09f05
+
b09f05
     fix_imports : bool, optional
b09f05
         Only useful when loading Python 2 generated pickled files on Python 3,
b09f05
         which includes npy/npz files containing object arrays. If `fix_imports`
b09f05
diff --git a/numpy/lib/tests/test_format.py b/numpy/lib/tests/test_format.py
b09f05
index 2d2b4ce..04e090c 100644
b09f05
--- a/numpy/lib/tests/test_format.py
b09f05
+++ b/numpy/lib/tests/test_format.py
b09f05
@@ -426,7 +426,7 @@ def roundtrip(arr):
b09f05
     f = BytesIO()
b09f05
     format.write_array(f, arr)
b09f05
     f2 = BytesIO(f.getvalue())
b09f05
-    arr2 = format.read_array(f2)
b09f05
+    arr2 = format.read_array(f2, allow_pickle=True)
b09f05
     return arr2
b09f05
 
b09f05
 
b09f05
@@ -553,7 +553,7 @@ def test_pickle_python2_python3():
b09f05
         path = os.path.join(data_dir, fname)
b09f05
 
b09f05
         for encoding in ['bytes', 'latin1']:
b09f05
-            data_f = np.load(path, encoding=encoding)
b09f05
+            data_f = np.load(path, allow_pickle=True, encoding=encoding)
b09f05
             if fname.endswith('.npz'):
b09f05
                 data = data_f['x']
b09f05
                 data_f.close()
b09f05
@@ -575,16 +575,19 @@ def test_pickle_python2_python3():
b09f05
         if sys.version_info[0] >= 3:
b09f05
             if fname.startswith('py2'):
b09f05
                 if fname.endswith('.npz'):
b09f05
-                    data = np.load(path)
b09f05
+                    data = np.load(path, allow_pickle=True)
b09f05
                     assert_raises(UnicodeError, data.__getitem__, 'x')
b09f05
                     data.close()
b09f05
-                    data = np.load(path, fix_imports=False, encoding='latin1')
b09f05
+                    data = np.load(path, allow_pickle=True, fix_imports=False,
b09f05
+                                   encoding='latin1')
b09f05
                     assert_raises(ImportError, data.__getitem__, 'x')
b09f05
                     data.close()
b09f05
                 else:
b09f05
-                    assert_raises(UnicodeError, np.load, path)
b09f05
+                    assert_raises(UnicodeError, np.load, path,
b09f05
+                                  allow_pickle=True)
b09f05
                     assert_raises(ImportError, np.load, path,
b09f05
-                                  encoding='latin1', fix_imports=False)
b09f05
+                                  allow_pickle=True, fix_imports=False,
b09f05
+                                  encoding='latin1')
b09f05
 
b09f05
 
b09f05
 def test_pickle_disallow():
b09f05
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
b09f05
index 2daa015..bde2567 100644
b09f05
--- a/numpy/lib/tests/test_io.py
b09f05
+++ b/numpy/lib/tests/test_io.py
b09f05
@@ -87,7 +87,7 @@ class RoundtripTest(object):
b09f05
 
b09f05
         """
b09f05
         save_kwds = kwargs.get('save_kwds', {})
b09f05
-        load_kwds = kwargs.get('load_kwds', {})
b09f05
+        load_kwds = kwargs.get('load_kwds', {"allow_pickle": True})
b09f05
         file_on_disk = kwargs.get('file_on_disk', False)
b09f05
 
b09f05
         if file_on_disk:
b09f05
-- 
b09f05
2.21.0
b09f05