Skip to content

Commit

Permalink
Adding tarfile member sanitization to extractall()
Browse files Browse the repository at this point in the history
  • Loading branch information
TrellixVulnTeam committed Oct 20, 2022
1 parent 080402e commit 38fa62d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
21 changes: 20 additions & 1 deletion audiomate/corpus/io/voxforge.py
Expand Up @@ -109,7 +109,26 @@ def extract_files(file_paths, target_path):

for file_path in file_paths:
with tarfile.open(file_path, 'r') as archive:
archive.extractall(target_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(archive, target_path)

file_name = os.path.splitext(os.path.basename(file_path))[0]
extracted.append(os.path.join(target_path, file_name))
Expand Down
24 changes: 23 additions & 1 deletion audiomate/utils/download.py
Expand Up @@ -139,4 +139,26 @@ def extract_tar(tar_path, target_folder):
``target_folder``.
"""
with tarfile.open(tar_path, 'r') as archive:
archive.extractall(target_folder)

import os

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(archive, target_folder)

0 comments on commit 38fa62d

Please sign in to comment.