2 # -*- coding: utf-8 -*-
3 # This test can be run stand-alone with something like:
4 # > PYTHONPATH=. python2 openerp/tests/test_misc.py
5 ##############################################################################
7 # OpenERP, Open Source Business Applications
8 # Copyright (c) 2012-TODAY OpenERP S.A. <http://openerp.com>
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU Affero General Public License as
12 # published by the Free Software Foundation, either version 3 of the
13 # License, or (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU Affero General Public License for more details.
20 # You should have received a copy of the GNU Affero General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 ##############################################################################
27 from lxml import etree
29 from openerp.tests import test_mail_examples
30 from openerp.tools import html_sanitize, html_email_clean, append_content_to_html, plaintext2html, email_split
33 class TestSanitizer(unittest2.TestCase):
34 """ Test the html sanitizer that filters html to remove unwanted attributes """
36 def test_basic_sanitizer(self):
38 ("yop", "<p>yop</p>"), # simple
39 ("lala<p>yop</p>xxx", "<p>lala</p><p>yop</p>xxx"), # trailing text
40 ("Merci à l'intérêt pour notre produit.nous vous contacterons bientôt. Merci",
41 u"<p>Merci à l'intérêt pour notre produit.nous vous contacterons bientôt. Merci</p>"), # unicode
43 for content, expected in cases:
44 html = html_sanitize(content)
45 self.assertEqual(html, expected, 'html_sanitize is broken')
47 def test_evil_malicious_code(self):
48 # taken from https://www.owasp.org/index.php/XSS_Filter_Evasion_Cheat_Sheet#Tests
50 ("<IMG SRC=javascript:alert('XSS')>"), # no quotes and semicolons
51 ("<IMG SRC=javascript:alert('XSS')>"), # UTF-8 Unicode encoding
52 ("<IMG SRC=javascript:alert('XSS')>"), # hex encoding
53 ("<IMG SRC=\"jav
ascript:alert('XSS');\">"), # embedded carriage return
54 ("<IMG SRC=\"jav
ascript:alert('XSS');\">"), # embedded newline
55 ("<IMG SRC=\"jav ascript:alert('XSS');\">"), # embedded tab
56 ("<IMG SRC=\"jav	ascript:alert('XSS');\">"), # embedded encoded tab
57 ("<IMG SRC=\"  javascript:alert('XSS');\">"), # spaces and meta-characters
58 ("<IMG SRC=\"javascript:alert('XSS')\""), # half-open html
59 ("<IMG \"\"\"><SCRIPT>alert(\"XSS\")</SCRIPT>\">"), # malformed tag
60 ("<SCRIPT/XSS SRC=\"http://ha.ckers.org/xss.js\"></SCRIPT>"), # non-alpha-non-digits
61 ("<SCRIPT/SRC=\"http://ha.ckers.org/xss.js\"></SCRIPT>"), # non-alpha-non-digits
62 ("<<SCRIPT>alert(\"XSS\");//<</SCRIPT>"), # extraneous open brackets
63 ("<SCRIPT SRC=http://ha.ckers.org/xss.js?< B >"), # non-closing script tags
64 ("<INPUT TYPE=\"IMAGE\" SRC=\"javascript:alert('XSS');\">"), # input image
65 ("<BODY BACKGROUND=\"javascript:alert('XSS')\">"), # body image
66 ("<IMG DYNSRC=\"javascript:alert('XSS')\">"), # img dynsrc
67 ("<IMG LOWSRC=\"javascript:alert('XSS')\">"), # img lowsrc
68 ("<TABLE BACKGROUND=\"javascript:alert('XSS')\">"), # table
69 ("<TABLE><TD BACKGROUND=\"javascript:alert('XSS')\">"), # td
70 ("<DIV STYLE=\"background-image: url(javascript:alert('XSS'))\">"), # div background
71 ("<DIV STYLE=\"background-image:\0075\0072\006C\0028'\006a\0061\0076\0061\0073\0063\0072\0069\0070\0074\003a\0061\006c\0065\0072\0074\0028.1027\0058.1053\0053\0027\0029'\0029\">"), # div background with unicoded exploit
72 ("<DIV STYLE=\"background-image: url(javascript:alert('XSS'))\">"), # div background + extra characters
73 ("<IMG SRC='vbscript:msgbox(\"XSS\")'>"), # VBscrip in an image
74 ("<BODY ONLOAD=alert('XSS')>"), # event handler
75 ("<BR SIZE=\"&{alert('XSS')}\>"), # & javascript includes
76 ("<LINK REL=\"stylesheet\" HREF=\"javascript:alert('XSS');\">"), # style sheet
77 ("<LINK REL=\"stylesheet\" HREF=\"http://ha.ckers.org/xss.css\">"), # remote style sheet
78 ("<STYLE>@import'http://ha.ckers.org/xss.css';</STYLE>"), # remote style sheet 2
79 ("<META HTTP-EQUIV=\"Link\" Content=\"<http://ha.ckers.org/xss.css>; REL=stylesheet\">"), # remote style sheet 3
80 ("<STYLE>BODY{-moz-binding:url(\"http://ha.ckers.org/xssmoz.xml#xss\")}</STYLE>"), # remote style sheet 4
81 ("<IMG STYLE=\"xss:expr/*XSS*/ession(alert('XSS'))\">"), # style attribute using a comment to break up expression
84 html = html_sanitize(content)
85 self.assertNotIn('javascript', html, 'html_sanitize did not remove a malicious javascript')
86 self.assertTrue('ha.ckers.org' not in html or 'http://ha.ckers.org/xss.css' in html, 'html_sanitize did not remove a malicious code in %s (%s)' % (content, html))
88 content = "<!--[if gte IE 4]><SCRIPT>alert('XSS');</SCRIPT><![endif]-->" # down-level hidden block
89 self.assertEquals(html_sanitize(content, silent=False), '')
92 sanitized_html = html_sanitize(test_mail_examples.MISC_HTML_SOURCE)
93 for tag in ['<div', '<b', '<i', '<u', '<strike', '<li', '<blockquote', '<a href']:
94 self.assertIn(tag, sanitized_html, 'html_sanitize stripped too much of original html')
95 for attr in ['javascript']:
96 self.assertNotIn(attr, sanitized_html, 'html_sanitize did not remove enough unwanted attributes')
98 emails = [("Charles <charles.bidule@truc.fr>", "Charles <charles.bidule@truc.fr>"),
99 ("Dupuis <'tr/-: ${dupuis#$'@truc.baz.fr>", "Dupuis <'tr/-: ${dupuis#$'@truc.baz.fr>"),
100 ("Technical <service/technical+2@open.com>", "Technical <service/technical+2@open.com>"),
101 ("Div nico <div-nico@open.com>", "Div nico <div-nico@open.com>")]
103 self.assertIn(email[1], html_sanitize(email[0]), 'html_sanitize stripped emails of original html')
105 def test_edi_source(self):
106 html = html_sanitize(test_mail_examples.EDI_LIKE_HTML_SOURCE)
107 self.assertIn('div style="font-family: \'Lucica Grande\', Ubuntu, Arial, Verdana, sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: #FFF;', html,
108 'html_sanitize removed valid style attribute')
109 self.assertIn('<span style="color: #222; margin-bottom: 5px; display: block; ">', html,
110 'html_sanitize removed valid style attribute')
111 self.assertIn('img class="oe_edi_paypal_button" src="https://www.paypal.com/en_US/i/btn/btn_paynowCC_LG.gif"', html,
112 'html_sanitize removed valid img')
113 self.assertNotIn('</body></html>', html, 'html_sanitize did not remove extra closing tags')
116 class TestCleaner(unittest2.TestCase):
117 """ Test the email cleaner function that filters the content of incoming emails """
119 def test_00_basic_text(self):
120 """ html_email_clean test for signatures """
123 """This is Sparta!\n--\nAdministrator\n+9988776655""",
125 ['Administrator', '9988776655']
127 """<p>--\nAdministrator</p>""",
129 ['--', 'Administrator']
131 """<p>This is Sparta!\n---\nAdministrator</p>""",
133 ['---', 'Administrator']
135 """<p>--<br>Administrator</p>""",
139 """<p>This is Sparta!<br/>--<br>Administrator</p>""",
143 """This is Sparta!\n>Ah bon ?\nCertes\n> Chouette !\nClair""",
144 ['This is Sparta!', 'Certes', 'Clair'],
145 ['Ah bon', 'Chouette']
148 for test, in_lst, out_lst in test_data:
149 new_html = html_email_clean(test, remove=True)
151 self.assertIn(text, new_html, 'html_email_cleaner wrongly removed content')
153 self.assertNotIn(text, new_html, 'html_email_cleaner did not remove unwanted content')
155 def test_05_shorten(self):
156 # TEST: shorten length
160 <p>Hello, <span>Raoul</span>
166 # shorten at 'H' of Hello -> should shorten after Hello,
167 html = html_email_clean(test_str, shorten=True, max_length=1, remove=True)
168 self.assertIn('Hello,', html, 'html_email_cleaner: shorten error or too short')
169 self.assertNotIn('Raoul', html, 'html_email_cleaner: shorten error or too long')
170 self.assertIn('read more', html, 'html_email_cleaner: shorten error about read more inclusion')
171 # shorten at 'are' -> should shorten after are
172 html = html_email_clean(test_str, shorten=True, max_length=17, remove=True)
173 self.assertIn('Hello,', html, 'html_email_cleaner: shorten error or too short')
174 self.assertIn('Raoul', html, 'html_email_cleaner: shorten error or too short')
175 self.assertIn('are', html, 'html_email_cleaner: shorten error or too short')
176 self.assertNotIn('pretty', html, 'html_email_cleaner: shorten error or too long')
177 self.assertNotIn('Really', html, 'html_email_cleaner: shorten error or too long')
178 self.assertIn('read more', html, 'html_email_cleaner: shorten error about read more inclusion')
180 # TEST: shorten in quote
181 test_str = '''<div> Blahble
183 <blockquote>This is a quote
184 <span>And this is quite a long quote, after all.</span>
187 # shorten in the quote
188 html = html_email_clean(test_str, shorten=True, max_length=25, remove=True)
189 self.assertIn('Blahble', html, 'html_email_cleaner: shorten error or too short')
190 self.assertIn('bluih', html, 'html_email_cleaner: shorten error or too short')
191 self.assertIn('blouh', html, 'html_email_cleaner: shorten error or too short')
192 self.assertNotIn('quote', html, 'html_email_cleaner: shorten error or too long')
193 self.assertIn('read more', html, 'html_email_cleaner: shorten error about read more inclusion')
194 # shorten in second word
195 html = html_email_clean(test_str, shorten=True, max_length=9, remove=True)
196 self.assertIn('Blahble', html, 'html_email_cleaner: shorten error or too short')
197 self.assertIn('bluih', html, 'html_email_cleaner: shorten error or too short')
198 self.assertNotIn('blouh', html, 'html_email_cleaner: shorten error or too short')
199 self.assertNotIn('quote', html, 'html_email_cleaner: shorten error or too long')
200 self.assertIn('read more', html, 'html_email_cleaner: shorten error about read more inclusion')
201 # shorten waaay too large
202 html = html_email_clean(test_str, shorten=True, max_length=900, remove=True)
203 self.assertIn('Blahble', html, 'html_email_cleaner: shorten error or too short')
204 self.assertIn('bluih', html, 'html_email_cleaner: shorten error or too short')
205 self.assertIn('blouh', html, 'html_email_cleaner: shorten error or too short')
206 self.assertNotIn('quote', html, 'html_email_cleaner: shorten error or too long')
208 def test_10_email_text(self):
209 """ html_email_clean test for text-based emails """
210 new_html = html_email_clean(test_mail_examples.TEXT_1, remove=True)
211 for ext in test_mail_examples.TEXT_1_IN:
212 self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed not quoted content')
213 for ext in test_mail_examples.TEXT_1_OUT:
214 self.assertNotIn(ext, new_html, 'html_email_cleaner did not erase signature / quoted content')
216 new_html = html_email_clean(test_mail_examples.TEXT_2, remove=True)
217 for ext in test_mail_examples.TEXT_2_IN:
218 self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed not quoted content')
219 for ext in test_mail_examples.TEXT_2_OUT:
220 self.assertNotIn(ext, new_html, 'html_email_cleaner did not erase signature / quoted content')
222 def test_20_email_html(self):
223 new_html = html_email_clean(test_mail_examples.HTML_1, remove=True)
224 for ext in test_mail_examples.HTML_1_IN:
225 self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed not quoted content')
226 for ext in test_mail_examples.HTML_1_OUT:
227 self.assertNotIn(ext, new_html, 'html_email_cleaner did not erase signature / quoted content')
229 new_html = html_email_clean(test_mail_examples.HTML_2, remove=True)
230 for ext in test_mail_examples.HTML_2_IN:
231 self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed not quoted content')
232 for ext in test_mail_examples.HTML_2_OUT:
233 self.assertNotIn(ext, new_html, 'html_email_cleaner did not erase signature / quoted content')
235 # --- MAIL ORIGINAL --- -> can't parse this one currently, too much language-dependent
236 # new_html = html_email_clean(test_mail_examples.HTML_3, remove=False)
237 # for ext in test_mail_examples.HTML_3_IN:
238 # self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed not quoted content')
239 # for ext in test_mail_examples.HTML_3_OUT:
240 # self.assertNotIn(ext, new_html, 'html_email_cleaner did not erase signature / quoted content')
242 def test_30_email_msoffice(self):
243 new_html = html_email_clean(test_mail_examples.MSOFFICE_1, remove=True)
244 for ext in test_mail_examples.MSOFFICE_1_IN:
245 self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed not quoted content')
246 for ext in test_mail_examples.MSOFFICE_1_OUT:
247 self.assertNotIn(ext, new_html, 'html_email_cleaner did not erase signature / quoted content')
249 new_html = html_email_clean(test_mail_examples.MSOFFICE_2, remove=True)
250 for ext in test_mail_examples.MSOFFICE_2_IN:
251 self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed not quoted content')
252 for ext in test_mail_examples.MSOFFICE_2_OUT:
253 self.assertNotIn(ext, new_html, 'html_email_cleaner did not erase signature / quoted content')
255 new_html = html_email_clean(test_mail_examples.MSOFFICE_3, remove=True)
256 for ext in test_mail_examples.MSOFFICE_3_IN:
257 self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed not quoted content')
258 for ext in test_mail_examples.MSOFFICE_3_OUT:
259 self.assertNotIn(ext, new_html, 'html_email_cleaner did not erase signature / quoted content')
261 def test_40_email_hotmail(self):
262 new_html = html_email_clean(test_mail_examples.HOTMAIL_1, remove=True)
263 for ext in test_mail_examples.HOTMAIL_1_IN:
264 self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed not quoted content')
265 for ext in test_mail_examples.HOTMAIL_1_OUT:
266 self.assertNotIn(ext, new_html, 'html_email_cleaner did not erase signature / quoted content')
268 def test_50_email_gmail(self):
269 new_html = html_email_clean(test_mail_examples.GMAIL_1, remove=True)
270 for ext in test_mail_examples.GMAIL_1_IN:
271 self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed not quoted content')
272 for ext in test_mail_examples.GMAIL_1_OUT:
273 self.assertNotIn(ext, new_html, 'html_email_cleaner did not erase signature / quoted content')
275 def test_60_email_thunderbird(self):
276 new_html = html_email_clean(test_mail_examples.THUNDERBIRD_1, remove=True)
277 for ext in test_mail_examples.THUNDERBIRD_1_IN:
278 self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed not quoted content')
279 for ext in test_mail_examples.THUNDERBIRD_1_OUT:
280 self.assertNotIn(ext, new_html, 'html_email_cleaner did not erase signature / quoted content')
282 def test_70_read_more_and_shorten(self):
284 'oe_expand_container_class': 'span_class',
285 'oe_expand_container_content': 'Herbert Einstein',
286 'oe_expand_separator_node': 'br_lapin',
287 'oe_expand_a_class': 'a_class',
288 'oe_expand_a_content': 'read mee',
290 new_html = html_email_clean(test_mail_examples.OERP_WEBSITE_HTML_1, remove=True, shorten=True, max_length=100, expand_options=expand_options)
291 for ext in test_mail_examples.OERP_WEBSITE_HTML_1_IN:
292 self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed not quoted content')
293 for ext in test_mail_examples.OERP_WEBSITE_HTML_1_OUT:
294 self.assertNotIn(ext, new_html, 'html_email_cleaner did not erase overlimit content')
295 for ext in ['<span class="span_class">Herbert Einstein<br_lapin></br_lapin><a href="#" class="a_class">read mee</a></span>']:
296 self.assertIn(ext, new_html, 'html_email_cleaner wrongly take into account specific expand options')
298 new_html = html_email_clean(test_mail_examples.OERP_WEBSITE_HTML_2, remove=True, shorten=True, max_length=200, expand_options=expand_options, protect_sections=False)
299 for ext in test_mail_examples.OERP_WEBSITE_HTML_2_IN:
300 self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed not quoted content')
301 for ext in test_mail_examples.OERP_WEBSITE_HTML_2_OUT:
302 self.assertNotIn(ext, new_html, 'html_email_cleaner did not erase overlimit content')
303 for ext in ['<span class="span_class">Herbert Einstein<br_lapin></br_lapin><a href="#" class="a_class">read mee</a></span>']:
304 self.assertIn(ext, new_html, 'html_email_cleaner wrongly take into account specific expand options')
306 new_html = html_email_clean(test_mail_examples.OERP_WEBSITE_HTML_2, remove=True, shorten=True, max_length=200, expand_options=expand_options, protect_sections=True)
307 for ext in test_mail_examples.OERP_WEBSITE_HTML_2_IN:
308 self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed not quoted content')
309 for ext in test_mail_examples.OERP_WEBSITE_HTML_2_OUT:
310 self.assertNotIn(ext, new_html, 'html_email_cleaner did not erase overlimit content')
312 '<span class="span_class">Herbert Einstein<br_lapin></br_lapin><a href="#" class="a_class">read mee</a></span>',
313 'tasks using the gantt chart and control deadlines']:
314 self.assertIn(ext, new_html, 'html_email_cleaner wrongly take into account specific expand options')
316 def test_70_read_more(self):
317 new_html = html_email_clean(test_mail_examples.BUG1, remove=True, shorten=True, max_length=100)
318 for ext in test_mail_examples.BUG_1_IN:
319 self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed valid content')
320 for ext in test_mail_examples.BUG_1_OUT:
321 self.assertNotIn(ext, new_html, 'html_email_cleaner did not removed invalid content')
323 new_html = html_email_clean(test_mail_examples.BUG2, remove=True, shorten=True, max_length=250)
324 for ext in test_mail_examples.BUG_2_IN:
325 self.assertIn(ext, new_html, 'html_email_cleaner wrongly removed valid content')
326 for ext in test_mail_examples.BUG_2_OUT:
327 self.assertNotIn(ext, new_html, 'html_email_cleaner did not removed invalid content')
329 def test_90_misc(self):
330 # False boolean for text must return empty string
331 new_html = html_email_clean(False)
332 self.assertEqual(new_html, False, 'html_email_cleaner did change a False in an other value.')
334 # Message with xml and doctype tags don't crash
335 new_html = html_email_clean(u'<?xml version="1.0" encoding="iso-8859-1"?>\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n <head>\n <title>404 - Not Found</title>\n </head>\n <body>\n <h1>404 - Not Found</h1>\n </body>\n</html>\n')
336 self.assertNotIn('encoding', new_html, 'html_email_cleaner did not remove correctly encoding attributes')
339 class TestHtmlTools(unittest2.TestCase):
340 """ Test some of our generic utility functions about html """
342 def test_plaintext2html(self):
344 ("First \nSecond \nThird\n \nParagraph\n\r--\nSignature paragraph", 'div',
345 "<div><p>First <br/>Second <br/>Third</p><p>Paragraph</p><p>--<br/>Signature paragraph</p></div>"),
346 ("First<p>It should be escaped</p>\nSignature", False,
347 "<p>First<p>It should be escaped</p><br/>Signature</p>")
349 for content, container_tag, expected in cases:
350 html = plaintext2html(content, container_tag)
351 self.assertEqual(html, expected, 'plaintext2html is broken')
353 def test_append_to_html(self):
355 ('<!DOCTYPE...><HTML encoding="blah">some <b>content</b></HtMl>', '--\nYours truly', True, True, False,
356 '<!DOCTYPE...><html encoding="blah">some <b>content</b>\n<pre>--\nYours truly</pre>\n</html>'),
357 ('<!DOCTYPE...><HTML encoding="blah">some <b>content</b></HtMl>', '--\nYours truly', True, False, False,
358 '<!DOCTYPE...><html encoding="blah">some <b>content</b>\n<p>--<br/>Yours truly</p>\n</html>'),
359 ('<html><body>some <b>content</b></body></html>', '<!DOCTYPE...>\n<html><body>\n<p>--</p>\n<p>Yours truly</p>\n</body>\n</html>', False, False, False,
360 '<html><body>some <b>content</b>\n\n\n<p>--</p>\n<p>Yours truly</p>\n\n\n</body></html>'),
362 for html, content, plaintext_flag, preserve_flag, container_tag, expected in test_samples:
363 self.assertEqual(append_content_to_html(html, content, plaintext_flag, preserve_flag, container_tag), expected, 'append_content_to_html is broken')
365 class TestEmailTools(unittest2.TestCase):
366 """ Test some of our generic utility functions for emails """
368 def test_email_split(self):
370 ("John <12345@gmail.com>", ['12345@gmail.com']), # regular form
371 ("d@x; 1@2", ['d@x', '1@2']), # semi-colon + extra space
372 ("'(ss)' <123@gmail.com>, 'foo' <foo@bar>", ['123@gmail.com','foo@bar']), # comma + single-quoting
373 ('"john@gmail.com"<johnny@gmail.com>', ['johnny@gmail.com']), # double-quoting
374 ('"<jg>" <johnny@gmail.com>', ['johnny@gmail.com']), # double-quoting with brackets
376 for text, expected in cases:
377 self.assertEqual(email_split(text), expected, 'email_split is broken')
379 if __name__ == '__main__':