From a1f45aab0ecc2fc593f959725094acfe73bb21ac Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Thu, 20 May 2021 10:27:50 +0000 Subject: [PATCH] [FIX] core, mass_mailing: preserve markup status in prepend_html_content In `prepend_html_content`, if body `html_body` and `html_content` are `Markup` objects, the processing of `html_content` through `re.sub` will strip away the `Markup` flag, leading to the later concatenation of the body and content first escaping the content (to safely convert it to a `Markup`) before performing the concatenation proper. As a result, the `preview` of a `mailing.mailing` would be double-escaped, and the markup hiding it would instead be printed as part of the body. Assume that the type of `html_content` is str-compatible, and immediately rewrap the value after processing it through `re.sub`. This should do nothing if `html_content` was an actual `str`, and will re-flag it as a `Markup` if it was one. Also change the finding of the insertion point to use `re.search`, I don't understand why this uses `finditer`, it just makes the code more complicated. closes odoo/odoo#71090 Signed-off-by: Thibault Delavallee (tde) --- addons/test_mass_mailing/tests/test_mailing_test.py | 11 +++++++++++ odoo/tools/mail.py | 9 +++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/addons/test_mass_mailing/tests/test_mailing_test.py b/addons/test_mass_mailing/tests/test_mailing_test.py index 33c0419af07..d2bd2a96449 100644 --- a/addons/test_mass_mailing/tests/test_mailing_test.py +++ b/addons/test_mass_mailing/tests/test_mailing_test.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +import lxml.html from odoo.addons.test_mass_mailing.tests.common import TestMassMailCommon from odoo.tests.common import users @@ -28,6 +29,16 @@ class TestMailingTest(TestMassMailCommon): with self.mock_mail_gateway(): mailing_test.send_mail_test() + # not great but matches send_mail_test, maybe that should be a method + # on mailing_test? + record = self.env[mailing.mailing_model_real].search([], limit=1) + first_child = lxml.html.fromstring(self._mails.pop()['body']).xpath('//body/*[1]')[0] + self.assertEqual(first_child.tag, 'div') + self.assertIn('display:none', first_child.get('style'), + "the preview node should be hidden") + self.assertEqual(first_child.text.strip(), "Preview " + record.name, + "the preview node should contain the preview text") + # Test if bad jinja in the subject raises an error mailing.write({'subject': 'Subject ${object.name_id.id}'}) with self.mock_mail_gateway(), self.assertRaises(Exception): diff --git a/odoo/tools/mail.py b/odoo/tools/mail.py index bbc8a660451..bfbfddea275 100644 --- a/odoo/tools/mail.py +++ b/odoo/tools/mail.py @@ -433,14 +433,11 @@ def append_content_to_html(html, content, plaintext=True, preserve=False, contai def prepend_html_content(html_body, html_content): """Prepend some HTML content at the beginning of an other HTML content.""" - html_content = re.sub(r'(?i)(]*>)', '', html_content) + html_content = type(html_content)(re.sub(r'(?i)(]*>)', '', html_content)) html_content = html_content.strip() - insert_index = next(re.finditer(r']*>', html_body), None) - if insert_index is None: - insert_index = next(re.finditer(r']*>', html_body), None) - - insert_index = insert_index.end() if insert_index else 0 + body_match = re.search(r']*>', html_body) or re.search(r']*>', html_body) + insert_index = body_match.end() if body_match else 0 return html_body[:insert_index] + html_content + html_body[insert_index:]