Skip to content
Permalink
Browse files

[FIX] base: put parsing of an email 'from' into a try except block

The "from" of an email is expected to follow the regex
"name <address>(, name <address>)*".
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 "🐧, <a@a.com>" 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 #33145

Signed-off-by: Nans Lefebvre (len) <len@odoo.com>
  • Loading branch information...
len-odoo committed Jan 9, 2019
1 parent 4dc8318 commit 7ffedecf82b595e62e94bd7ba4048c8deda952c3
Showing with 10 additions and 2 deletions.
  1. +10 −2 odoo/addons/base/ir/ir_mail_server.py
@@ -119,10 +119,18 @@ def encode_addr(addr):
# 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 <addr>),* 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):

0 comments on commit 7ffedec

Please sign in to comment.
You can’t perform that action at this time.