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):