From d67712f6f6b6f7c59f770cda908a253de4635dbb Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sun, 27 Nov 2022 09:57:49 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- email-signup/test/test_crawl.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/email-signup/test/test_crawl.py b/email-signup/test/test_crawl.py index 307cf0b..cb451cf 100644 --- a/email-signup/test/test_crawl.py +++ b/email-signup/test/test_crawl.py @@ -69,7 +69,26 @@ def test_browser_profile_coverage(self, tmpdir): # Extract crawl profile with tarfile.open(ff_db_tar) as tar: - tar.extractall(browser_params[0]['profile_archive_dir']) + 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, browser_params["0"]["profile_archive_dir"]) # Output databases ff_db = os.path.join(browser_params[0]['profile_archive_dir'],