From d439b8f8a3cdfd60249d11f59d045fa00c402072 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Mon, 3 Oct 2022 03:07:20 +0000 Subject: [PATCH 1/2] Adding tarfile member sanitization to extractall() --- .../scripts/prepare_pascal.py | 42 ++++++++++++++++++- .../scripts/prepare_pcontext.py | 21 +++++++++- .../6.DANet_PyTorch/scripts/prepare_pascal.py | 42 ++++++++++++++++++- .../scripts/prepare_pcontext.py | 21 +++++++++- 4 files changed, 120 insertions(+), 6 deletions(-) diff --git a/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pascal.py b/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pascal.py index 329e7a3..eba1c12 100644 --- a/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pascal.py +++ b/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pascal.py @@ -29,7 +29,26 @@ def download_voc(path, overwrite=False): filename = download(url, path=download_dir, overwrite=overwrite, sha1_hash=checksum) # extract with tarfile.open(filename) as tar: - tar.extractall(path=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) + + + safe_extract(tar, path=path) def download_aug(path, overwrite=False): _AUG_DOWNLOAD_URLS = [ @@ -40,7 +59,26 @@ def download_aug(path, overwrite=False): filename = download(url, path=download_dir, overwrite=overwrite, sha1_hash=checksum) # extract with tarfile.open(filename) as tar: - tar.extractall(path=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) + + + safe_extract(tar, path=path) shutil.move(os.path.join(path, 'benchmark_RELEASE'), os.path.join(path, 'VOCaug')) filenames = ['VOCaug/dataset/train.txt', 'VOCaug/dataset/val.txt'] diff --git a/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pcontext.py b/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pcontext.py index 92c1331..0999b7f 100644 --- a/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pcontext.py +++ b/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pcontext.py @@ -37,7 +37,26 @@ def download_ade(path, overwrite=False): # extract if os.path.splitext(filename)[1] == '.tar': with tarfile.open(filename) as tar: - tar.extractall(path=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) + + + safe_extract(tar, path=path) else: shutil.move(filename, os.path.join(path, 'VOCdevkit/VOC2010/'+os.path.basename(filename))) diff --git a/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pascal.py b/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pascal.py index 9f8bece..e258982 100644 --- a/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pascal.py +++ b/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pascal.py @@ -30,7 +30,26 @@ def download_voc(path, overwrite=False): filename = download(url, path=download_dir, overwrite=overwrite, sha1_hash=checksum) # extract with tarfile.open(filename) as tar: - tar.extractall(path=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) + + + safe_extract(tar, path=path) def download_aug(path, overwrite=False): @@ -42,7 +61,26 @@ def download_aug(path, overwrite=False): filename = download(url, path=download_dir, overwrite=overwrite, sha1_hash=checksum) # extract with tarfile.open(filename) as tar: - tar.extractall(path=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) + + + safe_extract(tar, path=path) shutil.move(os.path.join(path, 'benchmark_RELEASE'), os.path.join(path, 'VOCaug')) filenames = ['VOCaug/dataset/train.txt', 'VOCaug/dataset/val.txt'] diff --git a/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pcontext.py b/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pcontext.py index eecf81f..84bd0d5 100644 --- a/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pcontext.py +++ b/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pcontext.py @@ -36,7 +36,26 @@ def download_ade(path, overwrite=False): # extract if os.path.splitext(filename)[1] == '.tar': with tarfile.open(filename) as tar: - tar.extractall(path=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) + + + safe_extract(tar, path=path) else: shutil.move(filename, os.path.join(path, 'VOCdevkit/VOC2010/'+os.path.basename(filename))) From 5c936436d46ceefd34e52081419e2fb7d5488965 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Tue, 4 Oct 2022 21:37:26 +0000 Subject: [PATCH 2/2] Adding numeric_owner as keyword arguement --- .../3.FastFCN-PyTorch/scripts/prepare_pascal.py | 4 ++-- .../3.FastFCN-PyTorch/scripts/prepare_pcontext.py | 2 +- .../6.DANet_PyTorch/scripts/prepare_pascal.py | 4 ++-- .../6.DANet_PyTorch/scripts/prepare_pcontext.py | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pascal.py b/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pascal.py index eba1c12..bb368c4 100644 --- a/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pascal.py +++ b/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pascal.py @@ -45,7 +45,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(tar, path=path) @@ -75,7 +75,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(tar, path=path) diff --git a/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pcontext.py b/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pcontext.py index 0999b7f..23804b1 100644 --- a/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pcontext.py +++ b/Projects_DeepLearningRelated/3.FastFCN-PyTorch/scripts/prepare_pcontext.py @@ -53,7 +53,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(tar, path=path) diff --git a/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pascal.py b/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pascal.py index e258982..82ec3d8 100644 --- a/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pascal.py +++ b/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pascal.py @@ -46,7 +46,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(tar, path=path) @@ -77,7 +77,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(tar, path=path) diff --git a/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pcontext.py b/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pcontext.py index 84bd0d5..8fe023c 100644 --- a/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pcontext.py +++ b/Projects_DeepLearningRelated/6.DANet_PyTorch/scripts/prepare_pcontext.py @@ -52,7 +52,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(tar, path=path)