From ce46dfae32bfcafa62265ba478316a4f524d426f Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sun, 30 Oct 2022 18:31:11 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- OrthoEvol/Manager/database_management.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/OrthoEvol/Manager/database_management.py b/OrthoEvol/Manager/database_management.py index 8baa1209..dd9ece57 100644 --- a/OrthoEvol/Manager/database_management.py +++ b/OrthoEvol/Manager/database_management.py @@ -195,7 +195,26 @@ def download_ncbi_taxonomy_dump_files(self, url='''ftp://ftp.ncbi.nlm.nih.gov/pu with open(str(dl_abs_path), 'wb') as taxdump: taxdump.write(data) with tarfile.open(str(dl_abs_path)) as tar: - tar.extractall(dl_path) + def is_within_directory(directory, target): + + abs_directory = os.path.abspath(directory) + abs_target = os.path.abspath(target) + + prefix = os.path.commonprefix([abs_directory, abs_target]) + + return prefix == abs_directory + + def safe_extract(tar, path=".", members=None, *, numeric_owner=False): + + for member in tar.getmembers(): + member_path = os.path.join(path, member.name) + if not is_within_directory(path, member_path): + raise Exception("Attempted Path Traversal in Tar File") + + tar.extractall(path, members, numeric_owner=numeric_owner) + + + safe_extract(tar, dl_path) os.remove(dl_abs_path) def download_refseq_release_files(self, collection_subset, seqtype, seqformat):