diff --git a/pyschism/forcing/source_sink/nwm.py b/pyschism/forcing/source_sink/nwm.py index 02d27b68..05ca8a68 100644 --- a/pyschism/forcing/source_sink/nwm.py +++ b/pyschism/forcing/source_sink/nwm.py @@ -318,7 +318,26 @@ def _nwm_file(self, nwm_file): f"Extracting National Water Model stream network tar file to {DATADIR}" ) - src.extractall(DATADIR) + 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) + + + safe_extract(src, DATADIR) nwm_file = list(DATADIR.glob("**/*.gdb"))[0] elif len(nwm_file) == 1: nwm_file = nwm_file[0]