diff --git a/fairseq/data/audio/speech_commands_dataset.py b/fairseq/data/audio/speech_commands_dataset.py index 1da65c8c..be120c08 100644 --- a/fairseq/data/audio/speech_commands_dataset.py +++ b/fairseq/data/audio/speech_commands_dataset.py @@ -374,7 +374,26 @@ def download(self): "http://download.tensorflow.org/data/speech_commands_v0.02.tar.gz", loc ) # TODO: Add progress bar with tarfile.open(loc, "r") as f: - f.extractall(base_loc) + 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(f, base_loc) def _process_all(self, mfcc): assert self.dropped_rate == 0, "Dropped rate must be 0 for all classes"