From fa918c89c36c7c0b175767ba17eef21581af3df5 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sat, 7 Jan 2023 15:24:43 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- tests/integration/test_install_twists.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_install_twists.py b/tests/integration/test_install_twists.py index f3e88a41cd..5b62a0aaaf 100644 --- a/tests/integration/test_install_twists.py +++ b/tests/integration/test_install_twists.py @@ -146,7 +146,26 @@ def test_local_package(pipenv_instance_private_pypi, pip_src_dir, testsroot): import tarfile with tarfile.open(copy_to, "r:gz") as tgz: - tgz.extractall(path=p.path) + 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(tgz, path=p.path) c = p.pipenv(f"install -e {package}") assert c.returncode == 0 assert all(