diff --git "a/PyTorch\346\241\206\346\236\266/pytorch-master/caffe2/python/trt/test_trt.py" "b/PyTorch\346\241\206\346\236\266/pytorch-master/caffe2/python/trt/test_trt.py" index 6f9426d6..39c01ec6 100644 --- "a/PyTorch\346\241\206\346\236\266/pytorch-master/caffe2/python/trt/test_trt.py" +++ "b/PyTorch\346\241\206\346\236\266/pytorch-master/caffe2/python/trt/test_trt.py" @@ -69,7 +69,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/PyTorch\346\241\206\346\236\266/pytorch-master/scripts/model_zoo/update-caffe2-models.py" "b/PyTorch\346\241\206\346\236\266/pytorch-master/scripts/model_zoo/update-caffe2-models.py" index e9a5f28c..1723560c 100644 --- "a/PyTorch\346\241\206\346\236\266/pytorch-master/scripts/model_zoo/update-caffe2-models.py" +++ "b/PyTorch\346\241\206\346\236\266/pytorch-master/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/PyTorch\346\241\206\346\236\266/pytorch-master/scripts/model_zoo/update-models-from-caffe2.py" "b/PyTorch\346\241\206\346\236\266/pytorch-master/scripts/model_zoo/update-models-from-caffe2.py" index f3b485f4..fd2a3623 100644 --- "a/PyTorch\346\241\206\346\236\266/pytorch-master/scripts/model_zoo/update-models-from-caffe2.py" +++ "b/PyTorch\346\241\206\346\236\266/pytorch-master/scripts/model_zoo/update-models-from-caffe2.py" @@ -85,7 +85,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): diff --git "a/PyTorch\346\241\206\346\236\266/vision-master/torchvision/datasets/sbu.py" "b/PyTorch\346\241\206\346\236\266/vision-master/torchvision/datasets/sbu.py" index 6c8ad156..6a02af97 100644 --- "a/PyTorch\346\241\206\346\236\266/vision-master/torchvision/datasets/sbu.py" +++ "b/PyTorch\346\241\206\346\236\266/vision-master/torchvision/datasets/sbu.py" @@ -100,7 +100,26 @@ def download(self) -> None: # Extract file with tarfile.open(os.path.join(self.root, self.filename), 'r:gz') as tar: - tar.extractall(path=self.root) + 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, path=self.root) # Download individual photos with open(os.path.join(self.root, 'dataset', 'SBU_captioned_photo_dataset_urls.txt')) as fh: