Change exception into a warning

This commit is contained in:
hugovk 2014-05-26 16:25:15 +03:00
parent b7e5c27696
commit 35f1f4d8fa

View File

@ -36,10 +36,7 @@ class _imaging_not_installed:
raise ImportError("The _imaging C module is not installed") raise ImportError("The _imaging C module is not installed")
class ImageIsTooBigError(Exception): MAX_IMAGE_PIXELS = 6000 * 6000 - 1 # FIXME: Pick sensible limit
pass
ARBITARY_LARGE_LIMIT = 6000 * 6000 - 1 # FIXME: Pick sensible limit
try: try:
# give Tk a chance to set up the environment, in case we're # give Tk a chance to set up the environment, in case we're
@ -2106,17 +2103,21 @@ _fromarray_typemap[((1, 1), _ENDIAN + "i4")] = ("I", "I")
_fromarray_typemap[((1, 1), _ENDIAN + "f4")] = ("F", "F") _fromarray_typemap[((1, 1), _ENDIAN + "f4")] = ("F", "F")
def _compression_bomb_check(im, maximum_pixels): def _compression_bomb_check(size):
if maximum_pixels is None: if MAX_IMAGE_PIXELS is None:
return return
pixels = im.size[0] * im.size[1] pixels = size[0] * size[1]
if im.size[0] * im.size[1] > maximum_pixels: if pixels > MAX_IMAGE_PIXELS:
raise ImageIsTooBigError("Image size exceeds limit") warnings.warn(
"Image size (%d pixels) exceeds limit of %d pixels, "
"could be decompression bomb DOS attack." %
(pixels, MAX_IMAGE_PIXELS),
RuntimeWarning)
def open(fp, mode="r", maximum_pixels=ARBITARY_LARGE_LIMIT): def open(fp, mode="r"):
""" """
Opens and identifies the given image file. Opens and identifies the given image file.
@ -2156,7 +2157,7 @@ def open(fp, mode="r", maximum_pixels=ARBITARY_LARGE_LIMIT):
fp.seek(0) fp.seek(0)
# return factory(fp, filename) # return factory(fp, filename)
im = factory(fp, filename) im = factory(fp, filename)
_compression_bomb_check(im, maximum_pixels) _compression_bomb_check(im.size)
return im return im
except (SyntaxError, IndexError, TypeError): except (SyntaxError, IndexError, TypeError):
#import traceback #import traceback
@ -2172,7 +2173,7 @@ def open(fp, mode="r", maximum_pixels=ARBITARY_LARGE_LIMIT):
fp.seek(0) fp.seek(0)
# return factory(fp, filename) # return factory(fp, filename)
im = factory(fp, filename) im = factory(fp, filename)
_compression_bomb_check(im, maximum_pixels) _compression_bomb_check(im.size)
return im return im
except (SyntaxError, IndexError, TypeError): except (SyntaxError, IndexError, TypeError):
#import traceback #import traceback