Skip to content

Commit

Permalink
[FIX] mail: handle wrong Final-Recipient header in incoming emails
Browse files Browse the repository at this point in the history
Some emails are wrongly formatted mainly due to old servers. If Final-Recipient
header is void or wrongly encoded it currently crashes. This fix ensure there
is no crash, even if bounce detection could be incomplete.

Task-2641572
PR odoo#76159
Closes odoo#75618

Co-Authored-By: Thibault Delavallee <tde@odoo.com>
  • Loading branch information
fmdl and tde-banana-odoo committed Sep 13, 2021
1 parent 5919fbb commit c9f12f6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
4 changes: 3 additions & 1 deletion addons/mail/models/mail_thread.py
Expand Up @@ -1343,7 +1343,9 @@ def _message_parse_extract_bounce(self, email_message, message_dict):
if dsn_part and len(dsn_part.get_payload()) > 1:
dsn = dsn_part.get_payload()[1]
final_recipient_data = tools.decode_message_header(dsn, 'Final-Recipient')
bounced_email = tools.email_normalize(final_recipient_data.split(';', 1)[1].strip())
# old servers may hold void or invalid Final-Recipient header
if final_recipient_data and ";" in final_recipient_data:
bounced_email = tools.email_normalize(final_recipient_data.split(';', 1)[1].strip())
if bounced_email:
bounced_partner = self.env['res.partner'].sudo().search([('email_normalized', '=', bounced_email)])

Expand Down
50 changes: 50 additions & 0 deletions addons/test_mail/data/test_mail_data.py
Expand Up @@ -883,3 +883,53 @@
--92726A5F09.1555335666/mail2.test.ironsky--
"""

MAIL_NO_FINAL_RECIPIENT = """\
Return-Path: <bounce-md_9656353.6125275c.v1-f28f7746389e45f0bfbf9faefe9e0dc8@mandrillapp.com>
Delivered-To: catchall@xxxx.xxxx
Received: from in58.mail.ovh.net (unknown [10.101.4.58])
by vr46.mail.ovh.net (Postfix) with ESMTP id 4GvFsq2QLYz1t0N7r
for <catchall@xxxx.xxxx>; Tue, 24 Aug 2021 17:07:43 +0000 (UTC)
Received-SPF: Softfail (mailfrom) identity=mailfrom; client-ip=46.105.72.169; helo=40.mo36.mail-out.ovh.net; envelope-from=bounce-md_9656353.6125275c.v1-f28f7746389e45f0bfbf9faefe9e0dc8@mandrillapp.com; receiver=catchall@xxxx.xxxx
Authentication-Results: in58.mail.ovh.net;
dkim=pass (1024-bit key; unprotected) header.d=mandrillapp.com header.i=bounces-noreply@mandrillapp.com header.b="TDzUcdJs";
dkim=pass (1024-bit key) header.d=mandrillapp.com header.i=@mandrillapp.com header.b="MyjddTY5";
dkim-atps=neutral
Delivered-To: xxxx.xxxx-{email_to}
Authentication-Results: in62.mail.ovh.net;
dkim=pass (1024-bit key; unprotected) header.d=mandrillapp.com header.i=bounces-noreply@mandrillapp.com header.b="TDzUcdJs";
dkim=pass (1024-bit key) header.d=mandrillapp.com header.i=@mandrillapp.com header.b="MyjddTY5";
dkim-atps=neutral
From: MAILER-DAEMON <bounces-noreply@mandrillapp.com>
Subject: Undelivered Mail Returned to Sender
To: {email_to}
X-Report-Abuse: Please forward a copy of this message, including all headers, to abuse@mandrill.com
X-Report-Abuse: You can also report abuse here: http://mandrillapp.com/contact/abuse?id=9656353.f28f7746389e45f0bfbf9faefe9e0dc8
X-Mandrill-User: md_9656353
Feedback-ID: 9656353:9656353.20210824:md
Message-Id: <9656353.20210824170740.6125275cf21879.17950539@mail9.us4.mandrillapp.com>
Date: Tue, 24 Aug 2021 17:07:40 +0000
MIME-Version: 1.0
Content-Type: multipart/report; boundary="_av-UfLe6y6qxNo54-urtAxbJQ"
--_av-UfLe6y6qxNo54-urtAxbJQ
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
--- The following addresses had delivery problems ---
<{email_from}> (5.7.1 <{email_from}>: Recipient address rejected: Access denied)
--_av-UfLe6y6qxNo54-urtAxbJQ
Content-Type: message/delivery-status
Content-Transfer-Encoding: 7bit
Original-Recipient: <{email_from}>
Action: failed
Diagnostic-Code: smtp; 554 5.7.1 <{email_from}>: Recipient address rejected: Access denied
Remote-MTA: 10.245.192.40
--_av-UfLe6y6qxNo54-urtAxbJQ--"""
5 changes: 5 additions & 0 deletions addons/test_mail/tests/test_mail_gateway.py
Expand Up @@ -55,6 +55,11 @@ def test_message_parse_body(self):
res = self.env['mail.thread'].message_parse(self.from_string(test_mail_data.MAIL_MULTIPART_WEIRD_FILENAME))
self.assertEqual(res['attachments'][0][0], '62_@;,][)=.(ÇÀÉ.txt')

def test_message_parse_bugs(self):
""" Various corner cases or message parsing """
# message without Final-Recipient
self.env['mail.thread'].message_parse(self.from_string(test_mail_data.MAIL_NO_FINAL_RECIPIENT))

def test_message_parse_eml(self):
# Test that the parsing of mail with embedded emails as eml(msg) which generates empty attachments, can be processed.
mail = self.format(test_mail_data.MAIL_EML_ATTACHMENT, email_from='"Sylvie Lelitre" <test.sylvie.lelitre@agrolait.com>', to='generic@test.com')
Expand Down

0 comments on commit c9f12f6

Please sign in to comment.