From 9cfacc792500509ae1387a5f75e79b230f5702b1 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sat, 1 Oct 2022 07:53:23 +0000 Subject: [PATCH 1/2] Adding tarfile member sanitization to extractall() --- tools/convert_datasets/stare.py | 63 +++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/tools/convert_datasets/stare.py b/tools/convert_datasets/stare.py index 6238d62..4cea4a8 100644 --- a/tools/convert_datasets/stare.py +++ b/tools/convert_datasets/stare.py @@ -55,7 +55,26 @@ def main(): print('Extracting stare-images.tar...') with tarfile.open(image_path) as f: - f.extractall(osp.join(tmp_dir, 'gz')) + 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) + + + safe_extract(f, osp.join(tmp_dir,"gz")) for filename in os.listdir(osp.join(tmp_dir, 'gz')): un_gz( @@ -90,7 +109,26 @@ def main(): print('Extracting labels-ah.tar...') with tarfile.open(labels_ah) as f: - f.extractall(osp.join(tmp_dir, 'gz')) + 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) + + + safe_extract(f, osp.join(tmp_dir,"gz")) for filename in os.listdir(osp.join(tmp_dir, 'gz')): un_gz( @@ -129,7 +167,26 @@ def main(): print('Extracting labels-vk.tar...') with tarfile.open(labels_vk) as f: - f.extractall(osp.join(tmp_dir, 'gz')) + 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) + + + safe_extract(f, osp.join(tmp_dir,"gz")) for filename in os.listdir(osp.join(tmp_dir, 'gz')): un_gz( From 3f9b7e022f272721014818ff0a7cbcc8c0fc6935 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Tue, 4 Oct 2022 03:37:33 +0000 Subject: [PATCH 2/2] Adding numeric_owner as keyword arguement --- tools/convert_datasets/stare.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/convert_datasets/stare.py b/tools/convert_datasets/stare.py index 4cea4a8..58de7ca 100644 --- a/tools/convert_datasets/stare.py +++ b/tools/convert_datasets/stare.py @@ -71,7 +71,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(f, osp.join(tmp_dir,"gz")) @@ -125,7 +125,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(f, osp.join(tmp_dir,"gz")) @@ -183,7 +183,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(f, osp.join(tmp_dir,"gz"))