Mercurial > libervia-backend
comparison src/test/test_plugin_text_syntaxes.py @ 694:4284b6ad8aa3
tests: plugin text syntaxes sanitisation tests
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 12 Nov 2013 14:16:11 +0100 |
parents | |
children | 9810f22ba733 |
comparison
equal
deleted
inserted
replaced
693:65b30bc7f1b3 | 694:4284b6ad8aa3 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT: a jabber client | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 """ Plugin text syntaxes tests """ | |
21 | |
22 from sat.test import helpers | |
23 from sat.plugins import plugin_misc_text_syntaxes | |
24 | |
25 | |
26 class SanitisationTest(helpers.SatTestCase): | |
27 | |
28 def setUp(self): | |
29 self.host = helpers.FakeSAT() | |
30 self.text_syntaxes = plugin_misc_text_syntaxes.TextSyntaxes(self.host) | |
31 | |
32 | |
33 def test_xhtml_sanitise(self): | |
34 evil_html = """ | |
35 <html> | |
36 <head> | |
37 <script type="text/javascript" src="evil-site"></script> | |
38 <link rel="alternate" type="text/rss" src="evil-rss"> | |
39 <style> | |
40 body {background-image: url(javascript:do_evil)}; | |
41 div {color: expression(evil)}; | |
42 </style> | |
43 </head> | |
44 <body onload="evil_function()"> | |
45 <!-- I am interpreted for EVIL! --> | |
46 <a href="javascript:evil_function()">a link</a> | |
47 <a href="#" onclick="evil_function()">another link</a> | |
48 <p onclick="evil_function()">a paragraph</p> | |
49 <div style="display: none">secret EVIL!</div> | |
50 <object> of EVIL! </object> | |
51 <iframe src="evil-site"></iframe> | |
52 <form action="evil-site"> | |
53 Password: <input type="password" name="password"> | |
54 </form> | |
55 <blink>annoying EVIL!</blink> | |
56 <a href="evil-site">spam spam SPAM!</a> | |
57 <image src="evil!"> | |
58 </body> | |
59 </html>""" # example from lxml: /usr/share/doc/python-lxml-doc/html/lxmlhtml.html#cleaning-up-html | |
60 | |
61 expected = """<div> | |
62 <style>/* deleted */</style> | |
63 <body> | |
64 <a href="">a link</a> | |
65 <a href="#">another link</a> | |
66 <p>a paragraph</p> | |
67 <div style="">secret EVIL!</div> | |
68 of EVIL! | |
69 Password: | |
70 annoying EVIL! | |
71 <a href="evil-site">spam spam SPAM!</a> | |
72 <img src="evil!"> | |
73 </img></body> | |
74 </div>""" | |
75 | |
76 d = self.text_syntaxes.clean_xhtml(evil_html) | |
77 d.addCallback(self.assertEqualXML, expected, ignore_blank=True) | |
78 return d | |
79 | |
80 | |
81 def test_styles_sanitise(self): | |
82 evil_html = """<p style='display: None; test: blah; background: url(: alert()); color: blue;'>test <strong>retest</strong><br><span style="background-color: (alert('bouh')); titi; color: #cf2828; font-size: 3px; direction: !important; color: red; color: red !important; font-size: 100px !important; font-size: 100px ! important; font-size: 100%; font-size: 100ox; font-size: 100px; font-size: 100;;;; font-size: 100 %; color: 100 px 1.7em; color: rgba(0, 0, 0, 0.1); color: rgb(35,79,255); background-color: no-repeat; background-color: :alert(1); color: (alert('XSS')); color: (window.location='http://example.org/'); color: url(:window.location='http://example.org/'); "> toto </span></p>""" | |
83 | |
84 expected = """<p style="color: blue">test <strong>retest</strong><br/><span style="color: #cf2828; font-size: 3px; color: red; color: red !important; font-size: 100px !important; font-size: 100%; font-size: 100px; font-size: 100; font-size: 100 %; color: rgba(0, 0, 0, 0.1); color: rgb(35,79,255); background-color: no-repeat"> toto </span></p>""" | |
85 | |
86 d = self.text_syntaxes.clean_xhtml(evil_html) | |
87 d.addCallback(self.assertEqualXML, expected) | |
88 return d |