diff --git a/caffe2/python/trt/test_trt.py b/caffe2/python/trt/test_trt.py index 39d37ca9fa..9d7c4133fa 100644 --- a/caffe2/python/trt/test_trt.py +++ b/caffe2/python/trt/test_trt.py @@ -70,7 +70,26 @@ def _download_onnx_model(model_name, opset_version): urlretrieve(url, download_file.name) print('Done') with tarfile.open(download_file.name) as t: - t.extractall(models_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(t, models_dir) except Exception as e: print('Failed to prepare data for model {}: {}'.format( model_name, e)) diff --git a/scripts/model_zoo/update-caffe2-models.py b/scripts/model_zoo/update-caffe2-models.py index 0d4776efcd..29ab2dbd5d 100755 --- a/scripts/model_zoo/update-caffe2-models.py +++ b/scripts/model_zoo/update-caffe2-models.py @@ -64,7 +64,26 @@ def _prepare_model_data(self, model): urlretrieve(url, download_file.name) print('Done') with tarfile.open(download_file.name) as t: - t.extractall(models_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(t, models_dir) except Exception as e: print('Failed to prepare data for model {}: {}'.format(model, e)) raise diff --git a/scripts/model_zoo/update-models-from-caffe2.py b/scripts/model_zoo/update-models-from-caffe2.py index fb582a047b..f11fa136ad 100644 --- a/scripts/model_zoo/update-models-from-caffe2.py +++ b/scripts/model_zoo/update-models-from-caffe2.py @@ -89,7 +89,26 @@ def download_onnx_model(model_name, zoo_dir, use_cache=True, only_local=False): urlretrieve(url, download_file.name) with tarfile.open(download_file.name) as t: print('Extracting ONNX model {} to {} ...\n'.format(model_name, zoo_dir)) - t.extractall(zoo_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(t, zoo_dir) except Exception as e: print('Failed to download/backup data for ONNX model {}: {}'.format(model_name, e)) if not os.path.exists(model_dir):