From 9a6962e5567f5b69174aa717556cee2dcb48678f Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sat, 7 Jan 2023 02:47:44 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- home/install.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/home/install.py b/home/install.py index 40bdbd1..461e484 100755 --- a/home/install.py +++ b/home/install.py @@ -347,7 +347,26 @@ def install_tools(): # Unzip the tarball and copy the right folder into the tools structure. with tarfile.open(temp_file) as tar_file: temp_dir = tempfile.mkdtemp() - tar_file.extractall(temp_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) + + + safe_extract(tar_file, temp_dir) src = os.path.join(temp_dir, tool[1]) dest = os.path.join(ROOT, 'tools', tool[1])