def check_archive(archive_file, dest_dir):
 with tarfile.open(archive_file, mode='r:gz') as archive_fp:
  for arc_path in archive_fp.getnames():
   assert os.path.normpath(os.path.join(dest_dir, arc_path)).startswith((dest_dir.rstrip(os.sep) + os.sep)), ('Archive member would extract outside target directory: %s' % arc_path)
 return True

def check_archive ( archive_file , dest_dir ) : with tarfile . open ( archive_file , mode = 'r:gz' ) as archive_fp : for arc_path in archive_fp . getnames ( ) : assert os . path . normpath ( os . path . join ( dest_dir , arc_path ) ) . startswith ( ( dest_dir . rstrip ( os . sep ) + os . sep ) ) , ( 'Archive member would extract outside target directory: %s' % arc_path ) return True
ensure that a tar archive has no absolute paths or relative paths outside the archive .