[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 odoo/odoo#33145
Signed-off-by: Nans Lefebvre (len) <len@odoo.com>
This commit is contained in:
@@ -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 <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):
|
||||
|
||||
Reference in New Issue
Block a user