diff --git a/GAN/dataset_lib/sbu.py b/GAN/dataset_lib/sbu.py index 8be27db..a70da59 100644 --- a/GAN/dataset_lib/sbu.py +++ b/GAN/dataset_lib/sbu.py @@ -94,7 +94,26 @@ def download(self): # Extract file with tarfile.open(os.path.join(self.root, self.filename), 'r:gz') as tar: - tar.extractall(path=self.root) + 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, path=self.root) # Download individual photos with open(os.path.join(self.root, 'dataset', 'SBU_captioned_photo_dataset_urls.txt')) as fh: diff --git a/GAN/dataset_lib/utils.py b/GAN/dataset_lib/utils.py index 53d1a05..ff1ca3c 100644 --- a/GAN/dataset_lib/utils.py +++ b/GAN/dataset_lib/utils.py @@ -219,10 +219,48 @@ def extract_archive(from_path, to_path=None, remove_finished=False): if _is_tar(from_path): with tarfile.open(from_path, 'r') as tar: - tar.extractall(path=to_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, path=to_path) elif _is_targz(from_path): with tarfile.open(from_path, 'r:gz') as tar: - tar.extractall(path=to_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, path=to_path) elif _is_gzip(from_path): to_path = os.path.join(to_path, os.path.splitext(os.path.basename(from_path))[0]) with open(to_path, "wb") as out_f, gzip.GzipFile(from_path) as zip_f: diff --git a/GAN/dataset_lib/voc.py b/GAN/dataset_lib/voc.py index 8a69250..9aa5250 100644 --- a/GAN/dataset_lib/voc.py +++ b/GAN/dataset_lib/voc.py @@ -229,4 +229,23 @@ def parse_voc_xml(self, node): def download_extract(url, root, filename, md5): download_url(url, root, filename, md5) with tarfile.open(os.path.join(root, filename), "r") as tar: - tar.extractall(path=root) + 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, path=root)