diff --git a/examples/librispeech/download.py b/examples/librispeech/download.py index a72921fb..906879ee 100644 --- a/examples/librispeech/download.py +++ b/examples/librispeech/download.py @@ -19,7 +19,26 @@ def download_and_extract(in_file, out_dir): # Download and extract zip file. urllib.request.urlretrieve(file_url, filename=out_file) with tarfile.open(out_file) as tf: - tf.extractall(path=out_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(tf, path=out_dir) # Remove zip file after use os.remove(out_file)