From e01aa2c0b4fc616cc02e8c8692c485d506d410a3 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam <112716341+TrellixVulnTeam@users.noreply.github.com> Date: Sun, 20 Nov 2022 11:29:34 -0600 Subject: [PATCH] Adding tarfile member sanitization to extractall() (#878) --- bin/lib/cdn.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/bin/lib/cdn.py b/bin/lib/cdn.py index 472f7c682..a1886000a 100644 --- a/bin/lib/cdn.py +++ b/bin/lib/cdn.py @@ -90,7 +90,26 @@ def __unpack_tar(self): # unpack tar contents logger.debug('unpacking "%s" into "%s"', self.tar_file_path, self.tmpdir) with tarfile.open(self.tar_file_path) as tar: - tar.extractall(self.tmpdir) + 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, self.tmpdir) return list(get_directory_contents(self.tmpdir))