From c9f12f619318578a7a707c354fe715e3f9b5b73c Mon Sep 17 00:00:00 2001 From: Florent de Labarre <florent.mirieu@gmail.com> Date: Thu, 26 Aug 2021 08:12:35 +0000 Subject: [PATCH] [FIX] mail: handle wrong Final-Recipient header in incoming emails 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/odoo#76159 Closes odoo/odoo#75618 Co-Authored-By: Thibault Delavallee <tde@odoo.com> --- addons/mail/models/mail_thread.py | 4 +- addons/test_mail/data/test_mail_data.py | 50 +++++++++++++++++++++ addons/test_mail/tests/test_mail_gateway.py | 5 +++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index d4adaff3382a..518a2f5edad7 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -1343,7 +1343,9 @@ class MailThread(models.AbstractModel): 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)]) diff --git a/addons/test_mail/data/test_mail_data.py b/addons/test_mail/data/test_mail_data.py index d6e388469308..9b7856732bf1 100644 --- a/addons/test_mail/data/test_mail_data.py +++ b/addons/test_mail/data/test_mail_data.py @@ -883,3 +883,53 @@ OyI+T2RvbzwvYT4uCjwvcD4KPC9kaXY+CiAgICAgICAg --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--""" diff --git a/addons/test_mail/tests/test_mail_gateway.py b/addons/test_mail/tests/test_mail_gateway.py index ea0ef7c62b25..055c1eb3253e 100644 --- a/addons/test_mail/tests/test_mail_gateway.py +++ b/addons/test_mail/tests/test_mail_gateway.py @@ -55,6 +55,11 @@ class TestEmailParsing(TestMailCommon): 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') -- GitLab