From ee6b32377308dbf1bec158ce0ebfa64b29177c7f Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sun, 2 Oct 2022 11:01:55 +0000 Subject: [PATCH 1/2] Adding tarfile member sanitization to extractall() --- .../multi-frameworks/common/utils.py | 21 ++++++++++++++++++- .../research/neural_gpu/wmt_utils.py | 21 ++++++++++++++++++- .../tutorials/rnn/translate/data_utils.py | 21 ++++++++++++++++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/deep-learning/multi-frameworks/common/utils.py b/deep-learning/multi-frameworks/common/utils.py index eddac3594..6fe6d6d38 100755 --- a/deep-learning/multi-frameworks/common/utils.py +++ b/deep-learning/multi-frameworks/common/utils.py @@ -167,7 +167,26 @@ def maybe_download_cifar(src="https://ikpublictutorial.blob.core.windows.net/dee fname, h = urlretrieve(src, './delete.me') print('Extracting files...') with tarfile.open(fname) 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) + + + safe_extract(tar) os.remove(fname) return process_cifar() diff --git a/tensorflow_dl_models/research/neural_gpu/wmt_utils.py b/tensorflow_dl_models/research/neural_gpu/wmt_utils.py index 826fcdf05..c7fc210d8 100755 --- a/tensorflow_dl_models/research/neural_gpu/wmt_utils.py +++ b/tensorflow_dl_models/research/neural_gpu/wmt_utils.py @@ -84,7 +84,26 @@ def get_wmt_enfr_train_set(directory): _WMT_ENFR_TRAIN_URL) print("Extracting tar file %s" % corpus_file) with tarfile.open(corpus_file, "r") as corpus_tar: - corpus_tar.extractall(directory) + 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(corpus_tar, directory) gunzip_file(train_path + ".fr.gz", train_path + ".fr") gunzip_file(train_path + ".en.gz", train_path + ".en") return train_path diff --git a/tensorflow_dl_models/tutorials/rnn/translate/data_utils.py b/tensorflow_dl_models/tutorials/rnn/translate/data_utils.py index 2acab8d14..b2879190f 100755 --- a/tensorflow_dl_models/tutorials/rnn/translate/data_utils.py +++ b/tensorflow_dl_models/tutorials/rnn/translate/data_utils.py @@ -80,7 +80,26 @@ def get_wmt_enfr_train_set(directory): _WMT_ENFR_TRAIN_URL) print("Extracting tar file %s" % corpus_file) with tarfile.open(corpus_file, "r") as corpus_tar: - corpus_tar.extractall(directory) + 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(corpus_tar, directory) gunzip_file(train_path + ".fr.gz", train_path + ".fr") gunzip_file(train_path + ".en.gz", train_path + ".en") return train_path From b5abd79ac414fb36379d6df81b7bd6454b1cdd18 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Tue, 4 Oct 2022 17:20:35 +0000 Subject: [PATCH 2/2] Adding numeric_owner as keyword arguement --- deep-learning/multi-frameworks/common/utils.py | 2 +- tensorflow_dl_models/research/neural_gpu/wmt_utils.py | 2 +- tensorflow_dl_models/tutorials/rnn/translate/data_utils.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/deep-learning/multi-frameworks/common/utils.py b/deep-learning/multi-frameworks/common/utils.py index 6fe6d6d38..7bf283962 100755 --- a/deep-learning/multi-frameworks/common/utils.py +++ b/deep-learning/multi-frameworks/common/utils.py @@ -183,7 +183,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) diff --git a/tensorflow_dl_models/research/neural_gpu/wmt_utils.py b/tensorflow_dl_models/research/neural_gpu/wmt_utils.py index c7fc210d8..61cb70e69 100755 --- a/tensorflow_dl_models/research/neural_gpu/wmt_utils.py +++ b/tensorflow_dl_models/research/neural_gpu/wmt_utils.py @@ -100,7 +100,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(corpus_tar, directory) diff --git a/tensorflow_dl_models/tutorials/rnn/translate/data_utils.py b/tensorflow_dl_models/tutorials/rnn/translate/data_utils.py index b2879190f..68b7a7fce 100755 --- a/tensorflow_dl_models/tutorials/rnn/translate/data_utils.py +++ b/tensorflow_dl_models/tutorials/rnn/translate/data_utils.py @@ -96,7 +96,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(corpus_tar, directory)