From 48537401dba6e7754c7d174d0910b5c1795563e6 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Mon, 17 Oct 2022 02:17:07 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- .../convert_tf_models_to_pytorch.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/onnxruntime/python/tools/transformers/convert_tf_models_to_pytorch.py b/onnxruntime/python/tools/transformers/convert_tf_models_to_pytorch.py index c6398af12..b2abf1463 100644 --- a/onnxruntime/python/tools/transformers/convert_tf_models_to_pytorch.py +++ b/onnxruntime/python/tools/transformers/convert_tf_models_to_pytorch.py @@ -73,7 +73,26 @@ def download_tf_checkpoint(model_name, tf_models_dir="tf_models"): # untar file import tarfile with tarfile.open(tar_dir, 'r') as tar_ref: - tar_ref.extractall(ckpt_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_ref, ckpt_dir) os.remove(tar_dir) return get_ckpt_prefix_path(ckpt_dir)