From a7e01a0bc1de408ac4fbe37f3b96fc52095303e1 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sun, 11 Dec 2022 03:37:55 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../benchmark_runner/benchmark_builder.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/modules/javafx.web/src/main/native/Tools/Scripts/webkitpy/benchmark_runner/benchmark_builder.py b/modules/javafx.web/src/main/native/Tools/Scripts/webkitpy/benchmark_runner/benchmark_builder.py index a32ec7c4e..4d7378e35 100644 --- a/modules/javafx.web/src/main/native/Tools/Scripts/webkitpy/benchmark_runner/benchmark_builder.py +++ b/modules/javafx.web/src/main/native/Tools/Scripts/webkitpy/benchmark_runner/benchmark_builder.py @@ -75,7 +75,26 @@ def _fetch_remote_archive(self, archive_url): archive.extractall(self._dest) elif archive_type == 'tar.gz': with tarfile.open(archive_path, 'r:gz') as archive: - archive.extractall(self._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=numeric_owner) + + + safe_extract(archive, self._dest) unarchived_files = filter(lambda name: not name.startswith('.'), os.listdir(self._dest)) if len(unarchived_files) == 1: