From 6eda7dbc64ad7b2680a8b2259189564483227cc3 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Mon, 28 Nov 2022 12:51:17 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- tutorials/FRDEEP.py | 42 ++++++++++++++++++++++++++++++++++++++++-- tutorials/htru1.py | 21 ++++++++++++++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/tutorials/FRDEEP.py b/tutorials/FRDEEP.py index bda9477..fa1e932 100644 --- a/tutorials/FRDEEP.py +++ b/tutorials/FRDEEP.py @@ -164,7 +164,26 @@ def download(self): # 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) def __repr__(self): fmt_str = 'Dataset ' + self.__class__.__name__ + '\n' @@ -331,7 +350,26 @@ def download(self): # 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) def __repr__(self): fmt_str = 'Dataset ' + self.__class__.__name__ + '\n' diff --git a/tutorials/htru1.py b/tutorials/htru1.py index b048ece..0e69db4 100644 --- a/tutorials/htru1.py +++ b/tutorials/htru1.py @@ -155,7 +155,26 @@ def download(self): # 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) def __repr__(self): fmt_str = 'Dataset ' + self.__class__.__name__ + '\n'