From 121e3ea2194cf1fc302376a00b791773e05ede85 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Mon, 28 Nov 2022 09:50:27 +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/certbot-compatibility-test/certbot_compatibility_test/util.py b/certbot-compatibility-test/certbot_compatibility_test/util.py index 6051bbc2..07686154 100644 --- a/certbot-compatibility-test/certbot_compatibility_test/util.py +++ b/certbot-compatibility-test/certbot_compatibility_test/util.py @@ -46,7 +46,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")