diff --git a/userbot/plugins/archive.py b/userbot/plugins/archive.py index 54bdb71..b39a43b 100644 --- a/userbot/plugins/archive.py +++ b/userbot/plugins/archive.py @@ -391,7 +391,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() + 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) await mone.edit(f"unzipped and stored to `{downloaded_file_name[:-4]}`") os.remove(downloaded_file_name)