From 587cea10b51d2615dc642c6bc18a4295d342411a Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sat, 22 Oct 2022 15:46:09 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../certbot_compatibility_test/util.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/proxySTAR_V3/certbot/certbot-compatibility-test/certbot_compatibility_test/util.py b/proxySTAR_V3/certbot/certbot-compatibility-test/certbot_compatibility_test/util.py index af951aa6a..951a4d5b9 100644 --- a/proxySTAR_V3/certbot/certbot-compatibility-test/certbot_compatibility_test/util.py +++ b/proxySTAR_V3/certbot/certbot-compatibility-test/certbot_compatibility_test/util.py @@ -45,7 +45,26 @@ def extract_configs(configs, parent_dir): shutil.copytree(configs, config_dir, symlinks=True) elif tarfile.is_tarfile(configs): with tarfile.open(configs, "r") as tar: - tar.extractall(config_dir) + 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, config_dir) else: raise errors.Error("Unknown configurations file type")