From 3dbc4e76e085f202fad2c549c97150077036b330 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Mon, 3 Oct 2022 09:17:28 +0000 Subject: [PATCH 1/2] Adding tarfile member sanitization to extractall() --- python/misc/meshes.py | 21 ++++++++++++++++++++- python/open3d_tutorial.py | 21 ++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/python/misc/meshes.py b/python/misc/meshes.py index 9e19909..eb96da8 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) + + + 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..7bb0c1d 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) + + + safe_extract(tar, path=os.path.dirname(bunny_path)) shutil.move( os.path.join( os.path.dirname(bunny_path), From 45f7ad10a31ea6b218e6738aa127736c6890bd30 Mon Sep 17 00:00:00 2001 From: TrellixVulnTeam Date: Wed, 5 Oct 2022 00:45:12 +0000 Subject: [PATCH 2/2] Adding numeric_owner as keyword arguement --- python/misc/meshes.py | 2 +- python/open3d_tutorial.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/misc/meshes.py b/python/misc/meshes.py index eb96da8..d607656 100644 --- a/python/misc/meshes.py +++ b/python/misc/meshes.py @@ -181,7 +181,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(tar, path=os.path.dirname(bunny_path)) diff --git a/python/open3d_tutorial.py b/python/open3d_tutorial.py index 7bb0c1d..fc23b9e 100644 --- a/python/open3d_tutorial.py +++ b/python/open3d_tutorial.py @@ -250,7 +250,7 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False): if not is_within_directory(path, member_path): raise Exception("Attempted Path Traversal in Tar File") - tar.extractall(path, members, numeric_owner) + tar.extractall(path, members, numeric_owner=numeric_owner) safe_extract(tar, path=os.path.dirname(bunny_path))