[FIX] mail: cannot create attachment of bad image

When receiving an email, an attachment is created for every base64
embedded image. We got an invalid image which caused the attachment
generation to fail with a traceback due to a base64 decode error.

The attachment creation has been wrapped into a try/catch in order to
remove problematic images.

opw-1987224

closes odoo/odoo#33620

Signed-off-by: Nans Lefebvre (len) <len@odoo.com>
This commit is contained in:
Julien Castiaux
2019-05-23 13:09:37 +00:00
parent c057708447
commit 4f0a00628f
+16 -10
View File
@@ -4,6 +4,7 @@
import logging
import re
from binascii import Error as binascii_error
from operator import itemgetter
from email.utils import formataddr
from openerp.http import request
@@ -962,16 +963,21 @@ class Message(models.Model):
key = match.group(2)
if not data_to_url.get(key):
name = match.group(4) if match.group(4) else 'image%s' % len(data_to_url)
attachment = Attachments.create({
'name': name,
'datas': match.group(2),
'datas_fname': name,
'res_model': values.get('model'),
'res_id': values.get('res_id'),
})
attachment.generate_access_token()
values['attachment_ids'].append((4, attachment.id))
data_to_url[key] = ['/web/image/%s?access_token=%s' % (attachment.id, attachment.access_token), name]
try:
attachment = Attachments.create({
'name': name,
'datas': match.group(2),
'datas_fname': name,
'res_model': values.get('model'),
'res_id': values.get('res_id'),
})
except binascii_error:
_logger.warning("Impossible to create an attachment out of badly formated base64 embedded image. Image has been removed.")
return match.group(3) # group(3) is the url ending single/double quote matched by the regexp
else:
attachment.generate_access_token()
values['attachment_ids'].append((4, attachment.id))
data_to_url[key] = ['/web/image/%s?access_token=%s' % (attachment.id, attachment.access_token), name]
return '%s%s alt="%s"' % (data_to_url[key][0], match.group(3), data_to_url[key][1])
values['body'] = _image_dataurl.sub(base64_to_boundary, tools.ustr(values['body']))