Skip to content

Commit

Permalink
[FIX] base: put parsing of an email 'from' into a try except block
Browse files Browse the repository at this point in the history
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
Nans Lefebvre committed May 3, 2019
1 parent 4dc8318 commit 7ffedec
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions odoo/addons/base/ir/ir_mail_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 7ffedec

Please sign in to comment.