From 125f80888e70711c2f779a64465b22e697cfc8ad Mon Sep 17 00:00:00 2001 From: Christophe Matthieu Date: Wed, 19 Dec 2012 11:37:14 +0100 Subject: [PATCH 1/4] [FIX] tools mail: return the initial variable if is false or if is not an instance of basestring. remove xml and doctype tags. bzr revid: chm@openerp.com-20121219103714-u52hfmqr5u508xa2 --- openerp/tools/mail.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/openerp/tools/mail.py b/openerp/tools/mail.py index a54f99ba1ad..9df7499d0c8 100644 --- a/openerp/tools/mail.py +++ b/openerp/tools/mail.py @@ -130,11 +130,15 @@ def html_email_clean(html): dest += source[idx:] return dest - if not html: + if not html or not isinstance(html, basestring): return html html = ustr(html) + # 0. remove doctype and xml tags + doctype = re.compile(r"(<[?]xml.*?[?]>|<[!]DOCTYPE.*?>)[\n\r\s]*", re.IGNORECASE | re.DOTALL) + html = doctype.sub(r"", html) + # 1. -> \n, because otherwise the tree is obfuscated br_tags = re.compile(r'([<]\s*[bB][rR]\s*\/?[>])') html = _replace_matching_regex(br_tags, html, '__BR_TAG__') From a31af13a3ed672ef80b99b9ae622064a7291d350 Mon Sep 17 00:00:00 2001 From: Christophe Matthieu Date: Wed, 19 Dec 2012 12:18:53 +0100 Subject: [PATCH 2/4] [IMP] tools mail: add test message with xml and doctype tags don't crash bzr revid: chm@openerp.com-20121219111853-zj16hdg7ujakksqt --- openerp/tests/test_mail.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/openerp/tests/test_mail.py b/openerp/tests/test_mail.py index a8c806a13d0..fbf2caa2b62 100644 --- a/openerp/tests/test_mail.py +++ b/openerp/tests/test_mail.py @@ -177,6 +177,9 @@ class TestCleaner(unittest2.TestCase): new_html = html_email_clean(False) self.assertEqual(new_html, False, 'html_email_cleaner did change a False in an other value.') + # Test6: Message with xml and doctype tags don't crash + html_email_clean(u'\n\n\n \n 404 - Not Found\n \n \n

404 - Not Found

\n \n\n') + class TestHtmlTools(unittest2.TestCase): """ Test some of our generic utility functions about html """ From 111e080e48682c1e8fb98c061b4acf07e2852b57 Mon Sep 17 00:00:00 2001 From: Christophe Matthieu Date: Wed, 19 Dec 2012 13:40:38 +0100 Subject: [PATCH 3/4] [IMP] tools mail: don't remove xml and doctype but remove encoding attribute bzr revid: chm@openerp.com-20121219124038-05bf07960h05x0o8 --- openerp/tools/mail.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openerp/tools/mail.py b/openerp/tools/mail.py index 9df7499d0c8..e6b471a26ba 100644 --- a/openerp/tools/mail.py +++ b/openerp/tools/mail.py @@ -135,8 +135,8 @@ def html_email_clean(html): html = ustr(html) - # 0. remove doctype and xml tags - doctype = re.compile(r"(<[?]xml.*?[?]>|<[!]DOCTYPE.*?>)[\n\r\s]*", re.IGNORECASE | re.DOTALL) + # 0. remove encoding attribute inside tags + doctype = re.compile(r'(<[^>]*\s)(encoding=(["\'][^"\']*?["\']|[^\s\n\r>]+)(\s[^>]*|/)?>)', re.IGNORECASE | re.DOTALL) html = doctype.sub(r"", html) # 1. -> \n, because otherwise the tree is obfuscated From bf115734369b185cd3d979245de65276fd885bcb Mon Sep 17 00:00:00 2001 From: Christophe Matthieu Date: Wed, 19 Dec 2012 15:39:12 +0100 Subject: [PATCH 4/4] [IMP] tools mail: encoding : add test for removing encoding attribute bzr revid: chm@openerp.com-20121219143912-os160iv0q0jeyhld --- openerp/tests/test_mail.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openerp/tests/test_mail.py b/openerp/tests/test_mail.py index fbf2caa2b62..b3c11a13bfe 100644 --- a/openerp/tests/test_mail.py +++ b/openerp/tests/test_mail.py @@ -178,7 +178,8 @@ class TestCleaner(unittest2.TestCase): self.assertEqual(new_html, False, 'html_email_cleaner did change a False in an other value.') # Test6: Message with xml and doctype tags don't crash - html_email_clean(u'\n\n\n \n 404 - Not Found\n \n \n

404 - Not Found

\n \n\n') + new_html = html_email_clean(u'\n\n\n \n 404 - Not Found\n \n \n

404 - Not Found

\n \n\n') + self.assertNotIn('encoding', new_html, 'html_email_cleaner did not remove correctly encoding attributes') class TestHtmlTools(unittest2.TestCase): """ Test some of our generic utility functions about html """