diff --git a/Classification/train_concrete.py b/Classification/train_concrete.py index 5fc119d..f0f6966 100644 --- a/Classification/train_concrete.py +++ b/Classification/train_concrete.py @@ -46,7 +46,26 @@ def hook(self, block_num=1, block_size=1, total_size=None): if not isdir(cifar10_dataset_folder_path): with tarfile.open('cifar-10-python.tar.gz') as tar: - tar.extractall() + 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) tar.close() # Load raw data and reshape input images diff --git a/Classification/train_lbd.py b/Classification/train_lbd.py index 0601e79..8e95395 100644 --- a/Classification/train_lbd.py +++ b/Classification/train_lbd.py @@ -46,7 +46,26 @@ def hook(self, block_num=1, block_size=1, total_size=None): if not isdir(cifar10_dataset_folder_path): with tarfile.open('cifar-10-python.tar.gz') as tar: - tar.extractall() + 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) tar.close() # Load raw data and reshape input images diff --git a/Classification/train_vgg.py b/Classification/train_vgg.py index 409697f..3ca8c2d 100644 --- a/Classification/train_vgg.py +++ b/Classification/train_vgg.py @@ -46,7 +46,26 @@ def hook(self, block_num=1, block_size=1, total_size=None): if not isdir(cifar10_dataset_folder_path): with tarfile.open('cifar-10-python.tar.gz') as tar: - tar.extractall() + 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) tar.close() # Load raw data and reshape input images