From 8f6edcc43a1eab697d90bbb87bc6cd85055e3baa Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Fri, 30 Sep 2022 23:49:53 +0000 Subject: [PATCH 1/2] Adding tarfile member sanitization to extractall() --- mrjob/util.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/mrjob/util.py b/mrjob/util.py index 589029e45..f1cc0bdae 100644 --- a/mrjob/util.py +++ b/mrjob/util.py @@ -346,7 +346,26 @@ def unarchive(archive_path, dest): """ if tarfile.is_tarfile(archive_path): with tarfile.open(archive_path, 'r') as archive: - archive.extractall(dest) + 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(archive, dest) elif is_zipfile(archive_path): with ZipFile(archive_path, 'r') as archive: for name in archive.namelist(): From 29e414da9a35ed6b1d88630fb799de6d1c6e0a35 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Tue, 4 Oct 2022 01:18:46 +0000 Subject: [PATCH 2/2] Adding numeric_owner as keyword arguement --- mrjob/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mrjob/util.py b/mrjob/util.py index f1cc0bdae..0b10012ca 100644 --- a/mrjob/util.py +++ b/mrjob/util.py @@ -362,7 +362,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(archive, dest)