From 3cd4bd8faad53273dde3e49c0c8bee19173426b7 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Sat, 26 Nov 2022 21:49:44 +0000 Subject: [PATCH] Adding tarfile member sanitization to extractall() --- benchmarks/sigmod21-reproducibility/tuplex.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/benchmarks/sigmod21-reproducibility/tuplex.py b/benchmarks/sigmod21-reproducibility/tuplex.py index 9c53f2ab9..5a6ea7b3c 100755 --- a/benchmarks/sigmod21-reproducibility/tuplex.py +++ b/benchmarks/sigmod21-reproducibility/tuplex.py @@ -4310,7 +4310,26 @@ def plot(target, output_path, input_path): # extract tar file logging.info('Extracting experiment data...') with tarfile.open(DEFAULT_RESULT_PATH + '.tar.gz') as tf: - tf.extractall() + 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) logging.info('Extraction done!') os.makedirs(output_path, exist_ok=True)