Skip to content

Commit

Permalink
[FIX] mail: correct errors in b64 images handling
Browse files Browse the repository at this point in the history
Rev 666b594 introduced an autodetection
mechanism for transforming base64 embedded images inside emails into
attachment with a Content-ID URL.

The patch suffered from several issues:

- the base64 detection regex failed to detect base64 data that is folded
  over multiple lines - which happens to be the default in many Python
  versions when a binary field is read, or when using the base64 codec
  to encode a string.
  (Those cases where left untouched and the image was therefore missing)
- there was a logic error in the code when the same image was present
  multiple times, leading to an exception (`name` variable was
  referenced before being assigned)

This patch addresses both issues.

Fixes OPW-815073
  • Loading branch information
odony committed Feb 15, 2018
1 parent 7792755 commit f4bc3ac
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions addons/mail/models/mail_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


_logger = logging.getLogger(__name__)
_image_dataurl = re.compile(r'(data:image/[a-z]+?);base64,([a-z0-9+/]{3,}=*)([\'"])', re.I)
_image_dataurl = re.compile(r'(data:image/[a-z]+?);base64,([a-z0-9+/\n]{3,}=*)\n*([\'"])', re.I)


class Message(models.Model):
Expand Down Expand Up @@ -757,8 +757,9 @@ def base64_to_boundary(match):
'res_model': 'mail.message',
})
values['attachment_ids'].append((4, attachment.id))
data_to_url[key] = '/web/image/%s' % attachment.id
return '%s%s alt="%s"' % (data_to_url[key], match.group(3), name)
data_to_url[key] = (name, '/web/image/%s' % attachment.id)
alt, url = data_to_url[key]
return '%s%s alt="%s"' % (url, match.group(3), alt)
values['body'] = _image_dataurl.sub(base64_to_boundary, values['body'])

# delegate creation of tracking after the create as sudo to avoid access rights issues
Expand Down

0 comments on commit f4bc3ac

Please sign in to comment.