From 3a81661ed2112d0d66914a149ff229d00148451d Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Fri, 28 Oct 2022 23:14:24 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- examples/batch_size_example.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/examples/batch_size_example.py b/examples/batch_size_example.py index 59418c0..faedb73 100644 --- a/examples/batch_size_example.py +++ b/examples/batch_size_example.py @@ -62,7 +62,26 @@ def main(_): urllib.request.urlretrieve(_MODEL_URL, _MODEL_TARBALL) print("Unpacking SavedModel from {} to {}".format(_MODEL_TARBALL, _TMP_DIR)) with tarfile.open(_MODEL_TARBALL) as t: - t.extractall(_TMP_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, _TMP_DIR) # Load the SavedModel tf_g = tf.Graph()