diff --git a/aclia_data/pipeline/utils.py b/aclia_data/pipeline/utils.py index 6780753..3de5d3b 100644 --- a/aclia_data/pipeline/utils.py +++ b/aclia_data/pipeline/utils.py @@ -82,7 +82,26 @@ def unzip(input_path, output_path=None): output_path = re.sub('\.tar\.gz$', '', input_path) with tarfile.open(input_path, "r:gz") as tfile: - tfile.extractall(output_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=numeric_owner) + + + safe_extract(tfile, output_path) elif input_path.endswith('.7z'): if output_path is None: