diff --git a/addons/account/models/ir_attachment.py b/addons/account/models/ir_attachment.py index ddaf4b40ac7..516f955675d 100644 --- a/addons/account/models/ir_attachment.py +++ b/addons/account/models/ir_attachment.py @@ -97,9 +97,10 @@ class IrAttachment(models.Model): """ def is_xml(attachment): - # XML attachments received by mail have a 'text/plain' mimetype. - # Therefore, if content start with ' +To: {to} +cc: {cc} +Received: by mail1.openerp.com (Postfix, from userid 10002) + id 5DF9ABFB2A; Fri, 10 Aug 2012 16:16:39 +0200 (CEST) +From: {email_from} +Subject: {subject} +MIME-Version: 1.0 +Content-Type: multipart/alternative; + boundary="00000000000005d9da05fa394cc0" +Date: Fri, 10 Aug 2012 14:16:26 +0000 +Message-ID: {msg_id} +{extra} + +--00000000000005d9da05fa394cc0 +Content-Type: multipart/alternative; boundary="00000000000005d9d905fa394cbe" + +--00000000000005d9d905fa394cbe +Content-Type: text/plain; charset="UTF-8" + +Dear customer, + +Please find attached the Peppol Bis 3 attachment of your invoice (with an +encoding error in the address) + +Cheers, + +--00000000000005d9d905fa394cbe +Content-Type: text/html; charset="UTF-8" + +
Dear customer,

Please find attached the Peppol Bis 3 attachment of your invoice (with an encoding error in the address)

Cheers,
+ +--00000000000005d9d905fa394cbe-- + +--00000000000005d9da05fa394cc0 +Content-Type: text/xml; charset="US-ASCII"; + name="bis3_with_error_encoding_address.xml" +Content-Disposition: attachment; + filename="bis3_with_error_encoding_address.xml" +Content-Transfer-Encoding: base64 +Content-ID: +X-Attachment-Id: f_lgxgdqx40 + +PEludm9pY2UgeG1sbnM6Y2JjPSJ1cm46b2FzaXM6bmFtZXM6c3BlY2lmaWNhdGlvbjp1Ymw6c2No +ZW1hOnhzZDpDb21tb25CYXNpY0NvbXBvbmVudHMtMiIgeG1sbnM9InVybjpvYXNpczpuYW1lczpz +cGVjaWZpY2F0aW9uOnVibDpzY2hlbWE6eHNkOkludm9pY2UtMiI+DQo8Y2JjOlN0cmVldE5hbWU+ +Q2hhdXNz77+977+9ZSBkZSBCcnV4ZWxsZXM8L2NiYzpTdHJlZXROYW1lPg0KPC9JbnZvaWNlPg0K +--00000000000005d9da05fa394cc0-- +""" + MAIL_SINGLE_BINARY = """X-Original-To: raoul@grosbedon.fr Delivered-To: raoul@grosbedon.fr diff --git a/addons/test_mail/tests/test_mail_gateway.py b/addons/test_mail/tests/test_mail_gateway.py index bb2d4db482a..534dccef77c 100644 --- a/addons/test_mail/tests/test_mail_gateway.py +++ b/addons/test_mail/tests/test_mail_gateway.py @@ -1742,6 +1742,27 @@ class TestMailgateway(MailCommon): records = self.env['mail.test.gateway'].search([('name', 'ilike', 'Whitelist test alias loop %')]) self.assertEqual(len(records), 10, msg='Email whitelisted should not have the restriction') + # -------------------------------------------------- + # Corner cases / Bugs during message process + # -------------------------------------------------- + + def test_message_process_file_encoding_ascii(self): + """ Incoming email containing an xml attachment with unknown characters (�) but an ASCII charset should not + raise an Exception. UTF-8 is used as a safe fallback. + """ + record = self.format_and_process(test_mail_data.MAIL_MULTIPART_INVALID_ENCODING, self.email_from, 'groups@test.com') + + self.assertEqual(record.message_ids.attachment_ids.name, 'bis3_with_error_encoding_address.xml') + # NB: the xml received by email contains b"Chauss\xef\xbf\xbd\xef\xbf\xbde" with "\xef\xbf\xbd" being the + # replacement character � in UTF-8. + # When calling `_message_parse_extract_payload`, `part.get_content()` will be called on the attachment part of + # the email, triggering the decoding of the base64 attachment, so b"Chauss\xef\xbf\xbd\xef\xbf\xbde" is + # first retrieved. Then, `get_text_content` in `email` tries to decode this using the charset of the email + # part, i.e: `content.decode('us-ascii', errors='replace')`. So the errors are replaced using the Unicode + # replacement marker and the string "Chauss������e" is used to create the attachment. + # This explains the multiple "�" in the attachment. + self.assertIn("Chauss������e de Bruxelles", record.message_ids.attachment_ids.raw.decode()) + @tagged('mail_gateway') class TestMailThreadCC(MailCommon):