From 200eb6fdbd54e90b90f8ac43f438c51cee8cb0d9 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sun, 8 Jan 2023 11:16:44 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- shuttleBot.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/shuttleBot.py b/shuttleBot.py index 7100ead..d2bb756 100644 --- a/shuttleBot.py +++ b/shuttleBot.py @@ -33,7 +33,26 @@ file_path = 'checkpoint_run1.tar' 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) + + + safe_extract(tar) sess = gpt2.start_tf_sess() gpt2.load_gpt2(sess, run_name='run1') found = True