diff --git a/engine/datasets/cars.py b/engine/datasets/cars.py index 9f02875..624a972 100644 --- a/engine/datasets/cars.py +++ b/engine/datasets/cars.py @@ -74,7 +74,26 @@ def download(self): if fname.endswith('tgz'): print("Extracting %s..." % fname) with tarfile.open(os.path.join(self.root, fname), "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) copyfile(os.path.join(self.root, 'cars_test_annos_withlabels.mat'), os.path.join(self.root, 'devkit', 'cars_test_annos_withlabels.mat')) \ No newline at end of file diff --git a/engine/datasets/cub200.py b/engine/datasets/cub200.py index 3deec14..ff8d9d6 100644 --- a/engine/datasets/cub200.py +++ b/engine/datasets/cub200.py @@ -51,7 +51,26 @@ def download(self): download_url(self.url, self.root, self.filename) print("Extracting %s..." % self.filename) 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) def __len__(self): return len(self.data) diff --git a/engine/datasets/dogs.py b/engine/datasets/dogs.py index 1581974..78ae9fd 100644 --- a/engine/datasets/dogs.py +++ b/engine/datasets/dogs.py @@ -55,4 +55,23 @@ def download(self): # extract file print("Extracting %s..." % fname) with tarfile.open(os.path.join(self.root, fname), "r") as tar: - tar.extractall(path=self.root) \ No newline at end of file + 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) \ No newline at end of file