diff --git a/latte/dataset/cars3d.py b/latte/dataset/cars3d.py index 8401546..f3d2628 100644 --- a/latte/dataset/cars3d.py +++ b/latte/dataset/cars3d.py @@ -53,7 +53,26 @@ def prepare_files(filepath: pathlib.Path) -> None: with tarfile.open(archive_file.name) as af: subdir_and_files = [tarinfo for tarinfo in af.getmembers() if tarinfo.name.startswith("data/cars/")] - af.extractall(members=subdir_and_files, path=extract_dir.name) + 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(af, members=subdir_and_files, path=extract_dir.name) images = np.zeros((184 * 4 * 24, 64, 64, 3)) latents = np.zeros((184 * 4 * 24, 3))