From e24357fec92d9ce0e16ca716de22f99e07b7ae4f Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Thu, 15 Dec 2022 15:04:39 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- tests/test_push.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/test_push.py b/tests/test_push.py index 95cf981..fa4db6b 100644 --- a/tests/test_push.py +++ b/tests/test_push.py @@ -32,7 +32,26 @@ def test_create_base64_bundle(bundle_dir): # uncompress the bundle with tarfile.open(tarfile_name) as bundle: - bundle.extractall(path=outpath) + 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(bundle, path=outpath) outfiles = os.listdir(outpath)