diff --git a/python/misc/meshes.py b/python/misc/meshes.py index 9e19909..d607656 100644 --- a/python/misc/meshes.py +++ b/python/misc/meshes.py @@ -165,7 +165,26 @@ def bunny(): urllib.request.urlretrieve(url, bunny_path + ".tar.gz") print("extract bunny mesh") with tarfile.open(bunny_path + ".tar.gz") as tar: - tar.extractall(path=os.path.dirname(bunny_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, path=os.path.dirname(bunny_path)) shutil.move( os.path.join( os.path.dirname(bunny_path), diff --git a/python/open3d_tutorial.py b/python/open3d_tutorial.py index c9daba8..fc23b9e 100644 --- a/python/open3d_tutorial.py +++ b/python/open3d_tutorial.py @@ -234,7 +234,26 @@ def get_bunny_mesh(): urllib.request.urlretrieve(url, bunny_path + ".tar.gz") print("extract bunny mesh") with tarfile.open(bunny_path + ".tar.gz") as tar: - tar.extractall(path=os.path.dirname(bunny_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, path=os.path.dirname(bunny_path)) shutil.move( os.path.join( os.path.dirname(bunny_path),