From e1e738630680f1e414f476535c9bda0bec369d0f Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sat, 7 Jan 2023 02:16:24 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../datasets/inaturalist/inaturalist_data.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/avalanche/benchmarks/datasets/inaturalist/inaturalist_data.py b/avalanche/benchmarks/datasets/inaturalist/inaturalist_data.py index 44d235e34..e89da50ce 100644 --- a/avalanche/benchmarks/datasets/inaturalist/inaturalist_data.py +++ b/avalanche/benchmarks/datasets/inaturalist/inaturalist_data.py @@ -187,7 +187,26 @@ def download_inaturalist(self): os.path.join(self.data_folder, name[0]), "r:gz" ) as tar: self.log.info("Extracting INATURALIST images...") - tar.extractall(self.data_folder) + 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) + + + safe_extract(tar, self.data_folder) self.log.info("Done!") else: self.log.info("Skipping untarring, exists: ", save_name)