From 5e847e94438091ae4ffabf6145e079fcc4c9f1db Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Mon, 17 Oct 2022 08:10:53 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- release.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/release.py b/release.py index 2d14d7f..554a823 100755 --- a/release.py +++ b/release.py @@ -68,7 +68,26 @@ def main(): os.unlink(os.path.join(ROOT, item.name)) with tarfile.open(tar_tmp.name, mode="r") as f: - f.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(f) logging.debug("Creating new commit") subprocess.check_call(["git", "add", *[pathlib.Path(file).name for file in RELEASE_FILES]], cwd=ROOT)