diff --git a/odin/fuel/audio_data.py b/odin/fuel/audio_data.py index 2b8e7414..d36e7962 100644 --- a/odin/fuel/audio_data.py +++ b/odin/fuel/audio_data.py @@ -351,7 +351,26 @@ def load_command(self): outdir=save_path, verbose=True) with tarfile.open(zip_path, 'r:gz') as tar: - tar.extractall(audio_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(tar, audio_path) # ====== processing the audio file list ====== # audio_files = [i for i in audio_files if '_background_noise_' not in i] with open(os.path.join(audio_path, 'validation_list.txt'), 'r') as f: diff --git a/odin/utils/net_utils.py b/odin/utils/net_utils.py index cbcf162d..d0bf949a 100644 --- a/odin/utils/net_utils.py +++ b/odin/utils/net_utils.py @@ -97,7 +97,26 @@ def _progress(count, block_size, total_size): if '.tar.gz' in filepath: with tarfile.open(filepath, 'r:gz') as f: print("Extracting files ...") - f.extractall(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(f, path) # .zip elif '.zip' in filepath: # TODO