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 Nov 27, 2022
1 parent 6f9b90f commit 1ffb81a
Showing 1 changed file with 60 additions and 3 deletions.
63 changes: 60 additions & 3 deletions SegFormer/tools/convert_datasets/stare.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,26 @@ def main():

print('Extracting stare-images.tar...')
with tarfile.open(image_path) as f:
f.extractall(osp.join(tmp_dir, 'gz'))
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(f, osp.join(tmp_dir,"gz"))

for filename in os.listdir(osp.join(tmp_dir, 'gz')):
un_gz(
Expand Down Expand Up @@ -90,7 +109,26 @@ def main():

print('Extracting labels-ah.tar...')
with tarfile.open(labels_ah) as f:
f.extractall(osp.join(tmp_dir, 'gz'))
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(f, osp.join(tmp_dir,"gz"))

for filename in os.listdir(osp.join(tmp_dir, 'gz')):
un_gz(
Expand Down Expand Up @@ -129,7 +167,26 @@ def main():

print('Extracting labels-vk.tar...')
with tarfile.open(labels_vk) as f:
f.extractall(osp.join(tmp_dir, 'gz'))
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(f, osp.join(tmp_dir,"gz"))

for filename in os.listdir(osp.join(tmp_dir, 'gz')):
un_gz(
Expand Down

0 comments on commit 1ffb81a

Please sign in to comment.