Skip to content

Commit 0d0f9be

Browse files
Adding tarfile member sanitization to extractall()
1 parent 257e8b4 commit 0d0f9be

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/compas/__init__.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,26 @@ def get_bunny(localstorage=None):
303303
urlretrieve(url, destination)
304304

305305
with tarfile.open(destination) as file:
306-
file.extractall(localstorage)
306+
def is_within_directory(directory, target):
307+
308+
abs_directory = os.path.abspath(directory)
309+
abs_target = os.path.abspath(target)
310+
311+
prefix = os.path.commonprefix([abs_directory, abs_target])
312+
313+
return prefix == abs_directory
314+
315+
def safe_extract(tar, path=".", members=None, *, numeric_owner=False):
316+
317+
for member in tar.getmembers():
318+
member_path = os.path.join(path, member.name)
319+
if not is_within_directory(path, member_path):
320+
raise Exception("Attempted Path Traversal in Tar File")
321+
322+
tar.extractall(path, members, numeric_owner)
323+
324+
325+
safe_extract(file, localstorage)
307326

308327
os.remove(destination)
309328

0 commit comments

Comments
 (0)