From 97c34299723ec3e7e8921ab5e42b2e78e5199d3f Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Tue, 20 Dec 2022 19:32:21 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- imc/scripts/predict.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/imc/scripts/predict.py b/imc/scripts/predict.py index ceaad76..fa1236c 100644 --- a/imc/scripts/predict.py +++ b/imc/scripts/predict.py @@ -113,7 +113,29 @@ def get_ilastik(lib_dir: Path, version: str = "1.3.3post2") -> Path: download_file(base_url + file, lib_dir / file) print("Extracting ilastik archive.") with tarfile.open(lib_dir / file, "r:bz2") as tar: - tar.extractall(lib_dir) + + import os + + 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, lib_dir) (lib_dir / file).unlink() return f