diff --git a/pystall/core.py b/pystall/core.py index 7f7e808..efb0519 100644 --- a/pystall/core.py +++ b/pystall/core.py @@ -851,7 +851,26 @@ def extract(self): extract_path = self.location[:-7:] logging.info(f"Extracting Zip archive {self.location} to {extract_path}") with tarfile.open(name = self.location, mode = "r") as archive: - archive.extractall(extract_path) # Strip extension and extract to folder + 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(archive, extract_path) def install(self): """Extracts the .zip file and runs necessary installation steps. NOTE: Not yet implemented"""