From 1a123c60a7c214ae9ce4b526f1d0bfcd7d7a1b6a Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Wed, 23 Nov 2022 07:22:36 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- code/dataset/caltech.py | 63 +++++++++++++++++++++++++++++++++++++++-- code/dataset/voc.py | 21 +++++++++++++- 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/code/dataset/caltech.py b/code/dataset/caltech.py index 4c915d4..bca1e52 100644 --- a/code/dataset/caltech.py +++ b/code/dataset/caltech.py @@ -131,10 +131,48 @@ def download(self): # extract file with tarfile.open(os.path.join(self.root, "101_ObjectCategories.tar.gz"), "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) with tarfile.open(os.path.join(self.root, "101_Annotations.tar"), "r:") 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 extra_repr(self): return "Target type: {target_type}".format(**self.__dict__) @@ -222,4 +260,23 @@ def download(self): # extract file with tarfile.open(os.path.join(self.root, "256_ObjectCategories.tar"), "r:") 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) diff --git a/code/dataset/voc.py b/code/dataset/voc.py index 2a971bd..34cb0e8 100644 --- a/code/dataset/voc.py +++ b/code/dataset/voc.py @@ -160,4 +160,23 @@ def decode_target(cls, mask): def download_extract(url, root, filename, md5): download_url(url, root, filename, md5) with tarfile.open(os.path.join(root, filename), "r") as tar: - tar.extractall(path=root) \ No newline at end of file + 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=root) \ No newline at end of file