From 15dec95e5bb7c90a74b9fc78a6f3e0d309ab8136 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Fri, 25 Nov 2022 21:00:45 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- gpt_2_simple/gpt_2.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/gpt_2_simple/gpt_2.py b/gpt_2_simple/gpt_2.py index 3b7a96c..5105504 100644 --- a/gpt_2_simple/gpt_2.py +++ b/gpt_2_simple/gpt_2.py @@ -540,7 +540,26 @@ def copy_checkpoint_from_gdrive(run_name='run1', copy_folder=False): shutil.copyfile("/content/drive/My Drive/" + file_path, file_path) with tarfile.open(file_path, 'r') 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) def copy_file_to_gdrive(file_path):