When detecting plugin zip safety dont use a hardcoded list of native code extensions

This commit is contained in:
Kovid Goyal 2021-01-11 07:19:25 +05:30
parent 31a1ce8d4a
commit e91ebda5e8
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -278,24 +278,26 @@ class Plugin(object): # {{{
''' '''
if self.plugin_path is not None: if self.plugin_path is not None:
from calibre.utils.zipfile import ZipFile from calibre.utils.zipfile import ZipFile
zf = ZipFile(self.plugin_path) from importlib.machinery import EXTENSION_SUFFIXES
extensions = {x.rpartition('.')[-1].lower() for x in with ZipFile(self.plugin_path) as zf:
zf.namelist()} extensions = {x.lower() for x in EXTENSION_SUFFIXES}
zip_safe = True zip_safe = True
for ext in ('pyd', 'so', 'dll', 'dylib'): for name in zf.namelist():
if ext in extensions: for q in extensions:
zip_safe = False if name.endswith(q):
break zip_safe = False
if zip_safe: break
sys.path.insert(0, self.plugin_path) if not zip_safe:
self.sys_insertion_path = self.plugin_path break
else: if zip_safe:
from calibre.ptempfile import TemporaryDirectory sys.path.insert(0, self.plugin_path)
self._sys_insertion_tdir = TemporaryDirectory('plugin_unzip') self.sys_insertion_path = self.plugin_path
self.sys_insertion_path = self._sys_insertion_tdir.__enter__(*args) else:
zf.extractall(self.sys_insertion_path) from calibre.ptempfile import TemporaryDirectory
sys.path.insert(0, self.sys_insertion_path) self._sys_insertion_tdir = TemporaryDirectory('plugin_unzip')
zf.close() self.sys_insertion_path = self._sys_insertion_tdir.__enter__(*args)
zf.extractall(self.sys_insertion_path)
sys.path.insert(0, self.sys_insertion_path)
def __exit__(self, *args): def __exit__(self, *args):
ip, it = getattr(self, 'sys_insertion_path', None), getattr(self, ip, it = getattr(self, 'sys_insertion_path', None), getattr(self,