From fb9dad0683f0cf8d8d3ba09fc6ff12bc8b055791 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sun, 20 Nov 2022 14:05:11 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- mit_d3m/__init__.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/mit_d3m/__init__.py b/mit_d3m/__init__.py index 33bc498..3d8d071 100644 --- a/mit_d3m/__init__.py +++ b/mit_d3m/__init__.py @@ -78,7 +78,26 @@ def extract_dataset(src, dst): if not (os.path.exists(src) and tarfile.is_tarfile(src)): raise ValueError('Invalid source path: {}'.format(src)) with tarfile.open(src, 'r:gz') as tf: - tf.extractall(dst) + 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(tf, dst) def load_d3mds(dataset, root=DATA_PATH, force_download=False):