From 4ceaf286e95b25cebe1c7590f5717822f877d555 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Wed, 23 Nov 2022 13:27:07 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../style/format/benchmarks/generate_smoke.py | 21 ++++++++++++++++++- .../format/tests/test_analyzer_integration.py | 21 ++++++++++++++++++- .../style/format/tests/test_quality_report.py | 21 ++++++++++++++++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/lookout/style/format/benchmarks/generate_smoke.py b/lookout/style/format/benchmarks/generate_smoke.py index 59bcf3844..65fc4e436 100644 --- a/lookout/style/format/benchmarks/generate_smoke.py +++ b/lookout/style/format/benchmarks/generate_smoke.py @@ -96,7 +96,26 @@ def generate_smoke_entry(inputpath: str, outputpath: str, force: bool = False) - outputpath) return 1 with tarfile.open(str(inputpath)) as tar: - tar.extractall(str(outputpath)) + 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, str(outputpath)) repopaths = [x for x in outputpath.iterdir() if x.is_dir()] log.info("Repos found: %s", ", ".join(r.stem for r in repopaths)) with open(str(outputpath / index_filename), "w") as index_file: diff --git a/lookout/style/format/tests/test_analyzer_integration.py b/lookout/style/format/tests/test_analyzer_integration.py index f86d46885..7ed2530c7 100644 --- a/lookout/style/format/tests/test_analyzer_integration.py +++ b/lookout/style/format/tests/test_analyzer_integration.py @@ -53,7 +53,26 @@ def setUpClass(cls): cls.jquery_dir = os.path.join(cls.base_dir, "jquery") # str() is needed for Python 3.5 with tarfile.open(str(parent / "jquery.tar.xz")) as tar: - tar.extractall(path=cls.base_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, path=cls.base_dir) @classmethod def tearDownClass(cls): diff --git a/lookout/style/format/tests/test_quality_report.py b/lookout/style/format/tests/test_quality_report.py index aa51553f3..4e69389cf 100644 --- a/lookout/style/format/tests/test_quality_report.py +++ b/lookout/style/format/tests/test_quality_report.py @@ -36,7 +36,26 @@ def setUpClass(cls): cls.jquery_dir = os.path.join(cls.base_dir, "jquery") # str() is needed for Python 3.5 with tarfile.open(str(parent_loc / "jquery.tar.xz")) as tar: - tar.extractall(path=cls.base_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, path=cls.base_dir) files = glob.glob(os.path.join(cls.jquery_dir, "**", "*"), recursive=True) assert len(files) == 15, len(files) cls.model_path = os.path.join(str(parent_loc), "model_jquery.asdf")