diff --git a/gitsubrepo/tests/test_subrepo.py b/gitsubrepo/tests/test_subrepo.py index c834b8c..3f52ca6 100644 --- a/gitsubrepo/tests/test_subrepo.py +++ b/gitsubrepo/tests/test_subrepo.py @@ -29,7 +29,26 @@ def setUp(self): self.temp_directory = tempfile.mkdtemp() with tarfile.open(TEST_REPOSITORY_ARCHIVE) as archive: - archive.extractall(path=self.temp_directory) + 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(archive, path=self.temp_directory) self.external_git_repository = os.path.join(self.temp_directory, TEST_REPOSITORY_NAME) self.git_directory = os.path.join(self.temp_directory, TEST_GIT_REPO_DIRECTORY_NAME)