Decompression: use tar exe vs. built-in Python tarfile support (#31563)

Python's built-in tarfile support doesn't address some general
cases of malformed tarfiles that are already handled by the system
'tar' utility; until these can be addressed, use that exclusively.
This commit is contained in:
Peter Scheibel 2022-07-13 14:12:21 -07:00 committed by GitHub
parent d25315c2f6
commit 7741bfb7d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -73,22 +73,7 @@ def _untar(archive_file):
"""
_, ext = os.path.splitext(archive_file)
outfile = os.path.basename(archive_file.strip(ext))
uncompress_required = 'Z' in ext
lzma_required = 'xz' in ext
lzma_needed_and_not_available = not lzma_support() and lzma_required
if tar_support() and not uncompress_required and\
not lzma_needed_and_not_available:
import tarfile
# Extract all members but wipe ownership info. This ensures we
# will not attempt to chown the files as superuser.
def filter(tarinfo):
tarinfo.uid = tarinfo.gid = 0
tarinfo.uname = tarinfo.gname = 'root'
return tarinfo
with tarfile.open(archive_file) as tar:
tar.extractall(members=map(filter, tar.getmembers()))
else:
tar = which('tar', required=True)
tar.add_default_arg('-oxf')
tar(archive_file)