From 7ffedecf82b595e62e94bd7ba4048c8deda952c3 Mon Sep 17 00:00:00 2001 From: Nans Lefebvre Date: Wed, 9 Jan 2019 14:40:20 +0000 Subject: [PATCH] [FIX] base: put parsing of an email 'from' into a try except block MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The "from" of an email is expected to follow the regex "name
(, name
)*". We simply split the from on the commas "," and try to interpret each piece as a (name, address) pair. If a "from" contains a name that is not quoted, and contains special characters before the comma, then we would try to encode that name into an email address, and fail on a UnicodeEncodeError. E.g. the following "🐧, " would be parsed as [('', '🐧'),('', 'a@a.com')] thus Odoo would crash on trying to use 🐧 as email address. With this commit we would just mess up the name in these situations, by ignoring failures. opw 1923429 closes odoo/odoo#33145 Signed-off-by: Nans Lefebvre (len) --- odoo/addons/base/ir/ir_mail_server.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/odoo/addons/base/ir/ir_mail_server.py b/odoo/addons/base/ir/ir_mail_server.py index 454e2dc0780..4314b9b6015 100644 --- a/odoo/addons/base/ir/ir_mail_server.py +++ b/odoo/addons/base/ir/ir_mail_server.py @@ -119,10 +119,18 @@ def encode_rfc2822_address_header(header_text): # Header as a string, using an unlimited line length.", the old one # was "A synonym for Header.encode()." so call encode() directly? name = Header(pycompat.to_text(name)).encode() - return formataddr((name, email)) + # if the from does not follow the (name ),* convention, we might + # try to encode meaningless strings as address, as getaddresses is naive + # note it would also fail on real addresses with non-ascii characters + try: + return formataddr((name, email)) + except UnicodeEncodeError: + _logger.warning(_('Failed to encode the address %s\n' + 'from mail header:\n%s') % addr, header_text) + return "" addresses = getaddresses([pycompat.to_native(ustr(header_text))]) - return COMMASPACE.join(encode_addr(a) for a in addresses) + return COMMASPACE.join(a for a in (encode_addr(addr) for addr in addresses) if a) class IrMailServer(models.Model):