From b7b968f967963bb96a2dd4177be89d81b9412f6e Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Wed, 5 Oct 2022 23:27:42 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- deps/v8/test/mozilla/testcfg.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/deps/v8/test/mozilla/testcfg.py b/deps/v8/test/mozilla/testcfg.py index 5aeac4cc6..1cda7be7d 100644 --- a/deps/v8/test/mozilla/testcfg.py +++ b/deps/v8/test/mozilla/testcfg.py @@ -137,7 +137,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)