diff --git a/benchmarks/gluon-nlp/src/gluonnlp/data/conll.py b/benchmarks/gluon-nlp/src/gluonnlp/data/conll.py index b2602e9..d725d98 100644 --- a/benchmarks/gluon-nlp/src/gluonnlp/data/conll.py +++ b/benchmarks/gluon-nlp/src/gluonnlp/data/conll.py @@ -375,7 +375,26 @@ def _extract_archive(self): root = self._root path = os.path.join(root, archive_file_name) with tarfile.open(path, 'r:gz') 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) for fn in glob.glob(os.path.join(root, 'conll04st-release', '*.gz')): shutil.copy(fn, root) shutil.rmtree(os.path.join(root, 'conll04st-release'), ignore_errors=True) @@ -461,7 +480,26 @@ def _extract_archive(self): root = self._root path = os.path.join(root, archive_file_name) with tarfile.open(path, 'r:gz') 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) for fn in glob.glob(os.path.join(root, 'ud-treebanks-v2.1', '*', '*.conllu')): shutil.copy(fn, root) for data_license in glob.glob(os.path.join(root, 'ud-treebanks-v2.1', '*', 'LICENSE.txt')): diff --git a/benchmarks/gluon-nlp/src/gluonnlp/data/corpora/google_billion_word.py b/benchmarks/gluon-nlp/src/gluonnlp/data/corpora/google_billion_word.py index 36128a4..1a375a4 100644 --- a/benchmarks/gluon-nlp/src/gluonnlp/data/corpora/google_billion_word.py +++ b/benchmarks/gluon-nlp/src/gluonnlp/data/corpora/google_billion_word.py @@ -123,7 +123,26 @@ def _get_data(self): path=self._root, sha1_hash=archive_hash) # extract archive with tarfile.open(archive_file_path, 'r:gz') as tf: - tf.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(tf, path=self._root) def _get_vocab(self): archive_file_name, archive_hash = self._archive_vocab diff --git a/benchmarks/gluon-nlp/src/gluonnlp/data/word_embedding_evaluation.py b/benchmarks/gluon-nlp/src/gluonnlp/data/word_embedding_evaluation.py index a2e6bec..17f148f 100644 --- a/benchmarks/gluon-nlp/src/gluonnlp/data/word_embedding_evaluation.py +++ b/benchmarks/gluon-nlp/src/gluonnlp/data/word_embedding_evaluation.py @@ -75,7 +75,26 @@ def _download_data(self): zf.extractall(path=self.root) elif downloaded_file_path.lower().endswith('tar.gz'): with tarfile.open(downloaded_file_path, 'r') as tf: - tf.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(tf, path=self.root) elif len(self._checksums) > 1: err = 'Failed retrieving {clsname}.'.format( clsname=self.__class__.__name__)