File size: 3,527 Bytes
d096f37 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# SPDX-FileCopyrightText: 2023-present Antoine Pitrou <[email protected]>
#
# SPDX-License-Identifier: Apache-2.0
_ERROR_MSG = """\
Disallowed deserialization of 'arrow.py_extension_type':
storage_type = {storage_type}
serialized = {serialized}
pickle disassembly:\n{pickle_disassembly}
Reading of untrusted Parquet or Feather files with a PyExtensionType column
allows arbitrary code execution.
If you trust this file, you can enable reading the extension type by one of:
- upgrading to pyarrow >= 14.0.1, and call `pa.PyExtensionType.set_auto_load(True)`
- disable this error by running `import pyarrow_hotfix; pyarrow_hotfix.uninstall()`
We strongly recommend updating your Parquet/Feather files to use extension types
derived from `pyarrow.ExtensionType` instead, and register this type explicitly.
See https://arrow.apache.org/docs/dev/python/extending_types.html#defining-extension-types-user-defined-types
for more details.
"""
try:
_import_error = ModuleNotFoundError
except NameError:
_import_error = ImportError # ModuleNotFoundError unavailable in py3.5
def install():
import atexit
try:
import pyarrow as pa
except _import_error:
# Not installed; nothing to do here.
return
if not hasattr(pa, "ExtensionType"):
# Unsupported PyArrow version?
return
if getattr(pa, "_hotfix_installed", False):
return
class ForbiddenExtensionType(pa.ExtensionType):
def __arrow_ext_serialize__(self):
return b""
@classmethod
def __arrow_ext_deserialize__(cls, storage_type, serialized):
import io
import pickletools
out = io.StringIO()
pickletools.dis(serialized, out)
raise RuntimeError(
_ERROR_MSG.format(
storage_type=storage_type,
serialized=serialized,
pickle_disassembly=out.getvalue(),
)
)
if hasattr(pa, "unregister_extension_type"):
# 0.15.0 <= PyArrow
pa.unregister_extension_type("arrow.py_extension_type")
pa.register_extension_type(ForbiddenExtensionType(pa.null(),
"arrow.py_extension_type"))
elif hasattr(pa.lib, "_unregister_py_extension_type"):
# 0.14.1 <= PyArrow < 0.15.0
pa.lib._unregister_py_extension_type()
atexit.unregister(pa.lib._unregister_py_extension_type)
else:
# PyArrow 0.14.0
del pa.lib._extension_types_initializer
pa._hotfix_installed = True
def uninstall():
import atexit
try:
import pyarrow as pa
except _import_error:
# Not installed; nothing to do here.
return
if not hasattr(pa, "ExtensionType"):
# Unsupported PyArrow version?
return
if not getattr(pa, "_hotfix_installed", False):
return
if hasattr(pa, "unregister_extension_type"):
# 0.15.0 <= PyArrow
pa.unregister_extension_type("arrow.py_extension_type")
pa.lib._register_py_extension_type()
elif hasattr(pa.lib, "_register_py_extension_type"):
# 0.14.1 <= PyArrow < 0.15.0
pa.lib._register_py_extension_type()
atexit.register(pa.lib._unregister_py_extension_type)
elif hasattr(pa.lib, "_ExtensionTypesInitializer"):
# PyArrow 0.14.0
pa.lib._extension_types_initializer = pa.lib._ExtensionTypesInitializer()
pa._hotfix_installed = False
install()
|