Fix unzipping individual files in subdirs (#387)

* Fix unzipping individual files in subdirs

* Escape member file name before extracting
This commit is contained in:
Daniel Markstedt 2021-10-27 19:21:43 -07:00 committed by GitHub
parent 61e596aedf
commit 508bf83292
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -83,8 +83,6 @@ def list_images():
zip_members = zip.namelist() zip_members = zip.namelist()
# Strip out directories from the list # Strip out directories from the list
zip_members = [x for x in zip_members if not x.endswith("/")] zip_members = [x for x in zip_members if not x.endswith("/")]
# Reduce members to file names only (strip out full path)
zip_members = [PurePath(x).name for x in zip_members]
else: else:
logging.warning(f"{zip_path} is an invalid zip file") logging.warning(f"{zip_path} is an invalid zip file")
zip_members = False zip_members = False
@ -161,6 +159,7 @@ def unzip_file(file_name, member=False):
Returns dict with (boolean) status and (list of str) msg Returns dict with (boolean) status and (list of str) msg
""" """
from subprocess import run from subprocess import run
from re import escape
server_info = get_server_info() server_info = get_server_info()
if member == False: if member == False:
@ -171,7 +170,7 @@ def unzip_file(file_name, member=False):
else: else:
unzip_proc = run( unzip_proc = run(
["unzip", "-d", server_info["image_dir"], "-n", "-j", \ ["unzip", "-d", server_info["image_dir"], "-n", "-j", \
f"{server_info['image_dir']}/{file_name}", member], capture_output=True f"{server_info['image_dir']}/{file_name}", escape(member)], capture_output=True
) )
if unzip_proc.returncode != 0: if unzip_proc.returncode != 0:
stderr = unzip_proc.stderr.decode("utf-8") stderr = unzip_proc.stderr.decode("utf-8")