Skip to content

Commit

Permalink
Adding tarfile member sanitization to extractall() (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
TrellixVulnTeam authored May 30, 2023
1 parent 7fc5314 commit 04295bf
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion amlb/datasets/fileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,24 @@ def unarchive_file(path, dest_folder=None):
zf.extractall(path=dest_folder)
elif tarfile.is_tarfile(path):
with tarfile.open(path) as tf:
tf.extractall(path=dest_folder)
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(tf, path=dest_folder)
return dest

0 comments on commit 04295bf

Please sign in to comment.