From 2386a960978fab596b9a93f120598c58f248a902 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sun, 8 Jan 2023 01:47:34 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- tools/check-list.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tools/check-list.py b/tools/check-list.py index 9921ebd23..7cdb6cb23 100755 --- a/tools/check-list.py +++ b/tools/check-list.py @@ -555,7 +555,26 @@ def main(): # Check the package contents. try: with tarfile.open('./package.tgz', 'r:gz') as t: - t.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) + + + safe_extract(t) except (IOError, OSError, tarfile.TarError): print('Failed to untar package for "{}": {}' .format(_id, package['architecture']))