From 67efce843b4a46068fd0bfb71965fc5e4b494674 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sat, 1 Oct 2022 13:25:12 +0000 Subject: [PATCH 1/2] Adding tarfile member sanitization to extractall() --- userbot/plugins/archive.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/userbot/plugins/archive.py b/userbot/plugins/archive.py index 7c95365..f74a3f5 100644 --- a/userbot/plugins/archive.py +++ b/userbot/plugins/archive.py @@ -475,7 +475,26 @@ async def _(event): ms = (end - start).seconds await mone.edit("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) + + + safe_extract(tar_file, path=extracted) # tf = tarfile.open(downloaded_file_name) # tf.extractall(path=extracted) # tf.close() From 19b90a3c5d4df9e41914f648eff8ee201dbd6a5d Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Tue, 4 Oct 2022 05:04:34 +0000 Subject: [PATCH 2/2] Adding numeric_owner as keyword arguement --- userbot/plugins/archive.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/userbot/plugins/archive.py b/userbot/plugins/archive.py index f74a3f5..fda6c43 100644 --- a/userbot/plugins/archive.py +++ b/userbot/plugins/archive.py @@ -491,7 +491,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(tar_file, path=extracted)