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])