diff --git a/python/run_benchmarks.py b/python/run_benchmarks.py index 0dbc672..d4e1523 100755 --- a/python/run_benchmarks.py +++ b/python/run_benchmarks.py @@ -67,7 +67,26 @@ def decompress_data(archive_path, output_dir): if HAVE_LZMA: with tarfile.open(archive_path, mode='r:xz') as xz_file: - xz_file.extractall(path=output_dir) + 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(xz_file, path=output_dir) else: tar_xz_cmd = ['tar', 'xJf', archive_path, '-C', output_dir]