From baa4572312206bad4387185fea04f45a2eddf791 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Thu, 29 Sep 2022 22:48:21 +0000 Subject: [PATCH 1/2] Adding tarfile member sanitization to extractall() --- test/benchmarks/testcfg.py | 21 ++++++++++++++++++++- test/mozilla/testcfg.py | 21 ++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/test/benchmarks/testcfg.py b/test/benchmarks/testcfg.py index 6607bef8cc..e801850f60 100644 --- a/test/benchmarks/testcfg.py +++ b/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) + + + safe_extract(tar) with open(revision_file, "w") as f: f.write(revision) return diff --git a/test/mozilla/testcfg.py b/test/mozilla/testcfg.py index 70a7ac663c..e0bb34207c 100644 --- a/test/mozilla/testcfg.py +++ b/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) + + + safe_extract(tar) with open(versionfile, "w") as f: f.write(MOZILLA_VERSION) os.chdir(old_cwd) From e36e03360a6bc14b4e696ab0ca9468fcf7933ef7 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Mon, 3 Oct 2022 20:01:43 +0000 Subject: [PATCH 2/2] Adding numeric_owner as keyword arguement --- test/benchmarks/testcfg.py | 2 +- test/mozilla/testcfg.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/benchmarks/testcfg.py b/test/benchmarks/testcfg.py index e801850f60..11b4980f63 100644 --- a/test/benchmarks/testcfg.py +++ b/test/benchmarks/testcfg.py @@ -163,7 +163,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(tar) diff --git a/test/mozilla/testcfg.py b/test/mozilla/testcfg.py index e0bb34207c..93b6b01334 100644 --- a/test/mozilla/testcfg.py +++ b/test/mozilla/testcfg.py @@ -156,7 +156,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(tar)