From 8462f96b4ff3b6d91ff63b504d0b13657af0e28b Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Thu, 27 Oct 2022 14:45:08 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../tools/ci/python_packages/gitlab_api.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/esp-idf-4.0.1/tools/ci/python_packages/gitlab_api.py b/esp-idf-4.0.1/tools/ci/python_packages/gitlab_api.py index e7eced28..b017b5c8 100644 --- a/esp-idf-4.0.1/tools/ci/python_packages/gitlab_api.py +++ b/esp-idf-4.0.1/tools/ci/python_packages/gitlab_api.py @@ -143,7 +143,26 @@ def download_archive(self, ref, destination, project_id=None): with tarfile.open(temp_file.name, "r") as archive_file: root_name = archive_file.getnames()[0] - archive_file.extractall(destination) + 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(archive_file, destination) return os.path.join(os.path.realpath(destination), root_name)