plugin XEP-0054: reject image without MIME type if it's not PNG

This commit is contained in:
Goffi 2019-07-14 16:43:04 +02:00
commit 4743b1c840

View file

@ -234,12 +234,13 @@ class XEP_0054(object):
try:
mime_type = unicode(photo_elt.elements(NS_VCARD, "TYPE").next())
except StopIteration:
log.warning(u"no MIME type found, assuming image/png")
mime_type = u"image/png"
mime_type = None
else:
if not mime_type:
log.warning(u"empty MIME type, assuming image/png")
mime_type = u"image/png"
# MIME type not know, we'll only support PNG files
# TODO: autodetection using e.g. "magic" module
# (https://pypi.org/project/python-magic/)
mime_type = None
elif mime_type not in ("image/gif", "image/jpeg", "image/png"):
if mime_type == "image/x-png":
# XXX: this old MIME type is still used by some clients
@ -270,6 +271,15 @@ class XEP_0054(object):
if not buf:
log.warning(u"empty avatar for {jid}".format(jid=entity_jid.full()))
raise Failure(exceptions.NotFound())
if mime_type is None:
log.warning(_(u"no MIME type found for {entity}'s avatar, assuming image/png")
.format(entity=entity_jid.full()))
if buf[:8] != b'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a':
log.warning(u"this is not a PNG file, ignoring it")
raise Failure(exceptions.DataError())
else:
mime_type = u"image/png"
log.debug(_(u"Decoding binary"))
decoded = b64decode(buf)
del buf