diff --git a/workflows/train/pytorch/cifar-distributed/job.py b/workflows/train/pytorch/cifar-distributed/job.py index 0ee0d26e..d534ae51 100644 --- a/workflows/train/pytorch/cifar-distributed/job.py +++ b/workflows/train/pytorch/cifar-distributed/job.py @@ -37,7 +37,26 @@ os.makedirs(data_root, exist_ok=True) urllib.request.urlretrieve(url, filepath) with tarfile.open(filepath, "r:gz") as tar: - tar.extractall(path=data_root) + 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, path=data_root) os.remove(filepath) # delete tar.gz file after extraction # create azureml dataset