[PATCH] Upstream patch - 09052023

This commit is contained in:
Parthiv Patel
2023-05-09 08:36:58 +00:00
parent e3d4dea7bb
commit 63eea6c2ac
5 changed files with 79 additions and 5 deletions

View File

@@ -439,9 +439,9 @@ class AccountEdiFormat(models.Model):
content = base64.b64decode(attachment.with_context(bin_size=False).datas)
to_process = []
# XML attachments received by mail have a 'text/plain' mimetype.
# Therefore, if content start with '<?xml', it is considered as XML.
is_text_plain_xml = 'text/plain' in attachment.mimetype and content.startswith(b'<?xml')
# XML attachments received by mail have a 'text/plain' mimetype (cfr. context key: 'attachments_mime_plainxml')
# Therefore, if content start with '<?xml', or if the filename ends with '.xml', it is considered as XML.
is_text_plain_xml = 'text/plain' in attachment.mimetype and (content.startswith(b'<?xml') or attachment.name.endswith('.xml'))
if 'pdf' in attachment.mimetype:
to_process.extend(self._decode_pdf(attachment.name, content))
elif attachment.mimetype.endswith('/xml') or is_text_plain_xml:

View File

@@ -40,7 +40,7 @@
<field name="arch" type="xml">
<form string="Work Entry" >
<header>
<field name="state" widget="statusbar" options="{'clickable': '1'}" statusbar_visible="draft,validated,conflict"/>
<field name="state" widget="statusbar" readonly="1" statusbar_visible="draft,validated,conflict"/>
</header>
<sheet>
<div class="oe_title">

View File

@@ -1745,7 +1745,10 @@ class MailThread(models.AbstractModel):
continue
if isinstance(content, str):
encoding = info and info.get('encoding')
content = content.encode(encoding or 'utf-8')
try:
content = content.encode(encoding or "utf-8")
except UnicodeEncodeError:
content = content.encode("utf-8")
elif isinstance(content, EmailMessage):
content = content.as_bytes()
elif content is None:

View File

@@ -276,6 +276,56 @@ SGVsbG8gd29ybGQK
--Apple-Mail=_9331E12B-8BD2-4EC7-B53E-01F3FBEC9227--
"""
MAIL_MULTIPART_INVALID_ENCODING = """Return-Path: <whatever-2a840@postmaster.twitter.com>
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"
<div dir="ltr">Dear customer,<div><br></div><div>Please find attached the Peppol Bis 3 attachment of your invoice (with an encoding error in the address)</div><div><br></div><div>Cheers,</div></div>
--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: <f_lgxgdqx40>
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

View File

@@ -1486,6 +1486,27 @@ class TestMailgateway(TestMailCommon):
if encoding not in ['', 'UTF-8']:
self.assertNotEqual(file_content, attachment.raw.decode('utf-8'))
# --------------------------------------------------
# Corner cases / Bugs during message process
# --------------------------------------------------
def test_message_process_file_encoding_ascii(self):
""" Incoming email containing an xml attachment with unknown characters (<28>) 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_main_attachment_id.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 <20> 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<73><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>e" is used to create the attachment.
# This explains the multiple "<22>" in the attachment.
self.assertIn("Chauss<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>e de Bruxelles", record.message_main_attachment_id.raw.decode())
class TestMailThreadCC(TestMailCommon):