diff --git a/tests/integration/test_install_twists.py b/tests/integration/test_install_twists.py index f3e88a41..5b62a0aa 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(