From bbef408a074052cf550babb3d0f9cc668b18a947 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Tue, 29 Nov 2022 16:20:30 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- muffnn/core.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/muffnn/core.py b/muffnn/core.py index 274c7ff..33b31a2 100644 --- a/muffnn/core.py +++ b/muffnn/core.py @@ -229,7 +229,26 @@ def __setstate__(self, state): # Untar it. with tarfile.open(tarname, 'r') as tar: - tar.extractall(path=tmpdir) + 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=tmpdir) # And restore. self.graph_ = tf.Graph()