diff --git a/HPC/prep.py b/HPC/prep.py index 1174047..1639d35 100644 --- a/HPC/prep.py +++ b/HPC/prep.py @@ -63,7 +63,26 @@ def flights(small=None): print("- Extracting flight data... ", end='', flush=True) tar_path = os.path.join(data_dir, 'nycflights.tar.gz') with tarfile.open(tar_path, mode='r:gz') as flights: - flights.extractall('data/') + 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(flights, "data/") if small: for path in glob(os.path.join(data_dir, "nycflights", "*.csv")):