Also prevent ImageMagick identify routine from touching images other than the core types

This commit is contained in:
Kovid Goyal 2016-05-04 10:32:16 +05:30
parent 816063041c
commit 4adb20ebb4
2 changed files with 8 additions and 4 deletions

View File

@ -235,6 +235,12 @@ class Image(_magick.Image): # {{{
_magick.Image.quantize(self, number_colors, colorspace, treedepth, dither,
measure_error)
def identify(self, data):
fmt = what(None, data)
if fmt not in {'gif', 'png', 'jpeg', 'jpeg2000', 'webp'}:
raise ValueError('Unsupported image format: %s' % fmt)
return _magick.Image.identify(self, data)
def trim(self, fuzz):
try:
_magick.Image.remove_border(self, fuzz)

View File

@ -141,9 +141,6 @@ def identify_data(data):
(width, height, format)
or raises an Exception if data is not an image.
'''
if data.startswith(b'<?xml'):
# ImageMagick segfaults when trying to identify SVG images
raise ValueError('Identifying svg images is not supported')
img = Image()
img.identify(data)
width, height = img.size
@ -156,7 +153,8 @@ def identify(path):
(width, height, format)
or raises an Exception.
'''
data = open(path, 'rb').read()
with lopen(path, 'rb') as f:
data = f.read()
return identify_data(data)
def add_borders_to_image(img_data, left=0, top=0, right=0, bottom=0,