diff --git a/maluuba/newsqa/data_processing.py b/maluuba/newsqa/data_processing.py index e884d9f..6c2626e 100644 --- a/maluuba/newsqa/data_processing.py +++ b/maluuba/newsqa/data_processing.py @@ -71,7 +71,26 @@ def __init__(self, cnn_stories_path=None, dataset_path=None, log_level=logging.I with tarfile.open(zipped_dataset_path, mode='r:gz', encoding='utf-8') as t: extraction_destination_path = os.path.dirname(dataset_path) self._logger.info("Extracting `%s` to `%s`.", zipped_dataset_path, extraction_destination_path) - t.extractall(path=extraction_destination_path) + 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(t, path=extraction_destination_path) else: raise Exception( "`%s` was not found.\nFor legal reasons, you must first accept the terms "