diff --git a/hellbot/plugins/archiver.py b/hellbot/plugins/archiver.py index 5d6fab0..7459d45 100644 --- a/hellbot/plugins/archiver.py +++ b/hellbot/plugins/archiver.py @@ -521,7 +521,26 @@ async def _(event): "Stored the tar to `{}` in {} seconds.".format(downloaded_file_name, ms) ) with tarfile.TarFile.open(downloaded_file_name, "r") as tar_file: - tar_file.extractall(path=extracted) + 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_file, path=extracted) # tf = tarfile.open(downloaded_file_name) # tf.extractall(path=extracted) # tf.close()