Skip to content

Commit

Permalink
Adding tarfile member sanitization to extractall() (#878)
Browse files Browse the repository at this point in the history
  • Loading branch information
TrellixVulnTeam authored Nov 20, 2022
1 parent adcc76e commit e01aa2c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion bin/lib/cdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,26 @@ def __unpack_tar(self):
# unpack tar contents
logger.debug('unpacking "%s" into "%s"', self.tar_file_path, self.tmpdir)
with tarfile.open(self.tar_file_path) as tar:
tar.extractall(self.tmpdir)
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(tar, self.tmpdir)

return list(get_directory_contents(self.tmpdir))

Expand Down

0 comments on commit e01aa2c

Please sign in to comment.