From 6413893e7f1b0fcbf7917eb340212e3fdc693e8f Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Mon, 24 Oct 2022 23:26:35 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- thirdparty/v8/src/test/benchmarks/testcfg.py | 21 +++++++++++++++++++- thirdparty/v8/src/test/mozilla/testcfg.py | 21 +++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/thirdparty/v8/src/test/benchmarks/testcfg.py b/thirdparty/v8/src/test/benchmarks/testcfg.py index c94a35f..f26e717 100644 --- a/thirdparty/v8/src/test/benchmarks/testcfg.py +++ b/thirdparty/v8/src/test/benchmarks/testcfg.py @@ -147,7 +147,26 @@ def _DownloadIfNecessary(self, url, revision, target_dir): archive_file = "downloaded_%s_%s.tar.gz" % (target_dir, revision) if os.path.exists(archive_file): with tarfile.open(archive_file, "r:gz") as tar: - tar.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=numeric_owner) + + + safe_extract(tar) with open(revision_file, "w") as f: f.write(revision) return diff --git a/thirdparty/v8/src/test/mozilla/testcfg.py b/thirdparty/v8/src/test/mozilla/testcfg.py index 70a7ac6..93b6b01 100644 --- a/thirdparty/v8/src/test/mozilla/testcfg.py +++ b/thirdparty/v8/src/test/mozilla/testcfg.py @@ -140,7 +140,26 @@ def DownloadData(self): archive_file = "downloaded_%s.tar.gz" % MOZILLA_VERSION if os.path.exists(archive_file): with tarfile.open(archive_file, "r:gz") as tar: - tar.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=numeric_owner) + + + safe_extract(tar) with open(versionfile, "w") as f: f.write(MOZILLA_VERSION) os.chdir(old_cwd)