From bc5828a9d9885cbde733aa16b9d93d32d2b9f00a Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Thu, 20 Oct 2022 19:38:42 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- src/deepdive/misc/extract_cid_relations.py | 21 ++++++++++++++++++++- src/deepdive/udf/load_articles.py | 21 ++++++++++++++++++++- src/deepdive/udf/load_biothing_tokens.py | 21 ++++++++++++++++++++- src/deepdive/udf/load_sentences.py | 21 ++++++++++++++++++++- 4 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/deepdive/misc/extract_cid_relations.py b/src/deepdive/misc/extract_cid_relations.py index 1d997bd..e3b0a3b 100644 --- a/src/deepdive/misc/extract_cid_relations.py +++ b/src/deepdive/misc/extract_cid_relations.py @@ -25,7 +25,26 @@ def main(): pubtator_files = filter_files_from_tar(tar, 'pubtator_cid') # extract pubtator and corenlp output files into tempdir - tar.extractall(path=td, members=pubtator_files) + 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=td, members=pubtator_files) pubtator_filepaths = sorted(glob.glob(os.path.join(td, '*', diff --git a/src/deepdive/udf/load_articles.py b/src/deepdive/udf/load_articles.py index 7feb52b..78f3683 100755 --- a/src/deepdive/udf/load_articles.py +++ b/src/deepdive/udf/load_articles.py @@ -89,7 +89,26 @@ def main(conf, current_chunk, total_chunks): input_files = filter_files_from_tar(tar, 'input_files') # extract input_files into tempdir - tar.extractall(path=td, members=input_files) + 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=td, members=input_files) # glob/read through input_files and print file data input_filepaths = glob.glob(os.path.join(td, diff --git a/src/deepdive/udf/load_biothing_tokens.py b/src/deepdive/udf/load_biothing_tokens.py index c83036b..4a9df0b 100755 --- a/src/deepdive/udf/load_biothing_tokens.py +++ b/src/deepdive/udf/load_biothing_tokens.py @@ -68,7 +68,26 @@ def main(conf, current_chunk, total_chunks): pubtator_files = [] # extract pubtator and corenlp output files into tempdir - tar.extractall(path=td, members=itertools.chain(output_files, pubtator_files)) + 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=td, members=itertools.chain(output_files,pubtator_files)) # glob/read through output_files and print file data output_filepaths = sorted(glob.glob(os.path.join(td, diff --git a/src/deepdive/udf/load_sentences.py b/src/deepdive/udf/load_sentences.py index 6d2f827..66e2df7 100755 --- a/src/deepdive/udf/load_sentences.py +++ b/src/deepdive/udf/load_sentences.py @@ -68,7 +68,26 @@ def main(conf, current_chunk, total_chunks): pubtator_files = [] # extract pubtator and corenlp output files into tempdir - tar.extractall(path=td, members=itertools.chain(output_files, pubtator_files)) + 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=td, members=itertools.chain(output_files,pubtator_files)) # glob/read through output_files and print file data output_filepaths = sorted(glob.glob(os.path.join(td,