Skip to content

Commit a05fb74

Browse files
Adding tarfile member sanitization to extractall()
1 parent ca755af commit a05fb74

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

examples/python/misc/meshes.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,26 @@ def bunny():
159159
urllib.request.urlretrieve(url, bunny_path + ".tar.gz")
160160
print("extract bunny mesh")
161161
with tarfile.open(bunny_path + ".tar.gz") as tar:
162-
tar.extractall(path=os.path.dirname(bunny_path))
162+
def is_within_directory(directory, target):
163+
164+
abs_directory = os.path.abspath(directory)
165+
abs_target = os.path.abspath(target)
166+
167+
prefix = os.path.commonprefix([abs_directory, abs_target])
168+
169+
return prefix == abs_directory
170+
171+
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
172+
173+
for member in tar.getmembers():
174+
member_path = os.path.join(path, member.name)
175+
if not is_within_directory(path, member_path):
176+
raise Exception("Attempted Path Traversal in Tar File")
177+
178+
tar.extractall(path, members, numeric_owner=numeric_owner)
179+
180+
181+
safe_extract(tar, path=os.path.dirname(bunny_path))
163182
shutil.move(
164183
os.path.join(os.path.dirname(bunny_path), "bunny", "reconstruction", "bun_zipper.ply"),
165184
bunny_path,

0 commit comments

Comments
 (0)