From f1d5e93c906d28f1e952f05bfacf1ad1a628d2a9 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Wed, 23 Nov 2022 03:05:23 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../test_multiple_images_preprocessing.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/external/coremltools_wrap/coremltools/coremltools/test/neural_network/test_multiple_images_preprocessing.py b/src/external/coremltools_wrap/coremltools/coremltools/test/neural_network/test_multiple_images_preprocessing.py index c7ec533b07..13e83d090a 100644 --- a/src/external/coremltools_wrap/coremltools/coremltools/test/neural_network/test_multiple_images_preprocessing.py +++ b/src/external/coremltools_wrap/coremltools/coremltools/test/neural_network/test_multiple_images_preprocessing.py @@ -27,7 +27,26 @@ def extract_tarfile(input_filename, dest_dir): with tarfile.open(input_filename, "r:gz") as tar: - tar.extractall(dest_dir) + 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, dest_dir) def load_mlmodel(model_path):