comparison sat/test/test_plugin_misc_text_syntaxes.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/test/test_plugin_misc_text_syntaxes.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT: a jabber client
5 # Copyright (C) 2009-2018 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 from twisted.trial.unittest import SkipTest
25 import re
26
27
28 class SanitisationTest(helpers.SatTestCase):
29
30 EVIL_HTML1 = """
31 <html>
32 <head>
33 <script type="text/javascript" src="evil-site"></script>
34 <link rel="alternate" type="text/rss" src="evil-rss">
35 <style>
36 body {background-image: url(javascript:do_evil)};
37 div {color: expression(evil)};
38 </style>
39 </head>
40 <body onload="evil_function()">
41 <!-- I am interpreted for EVIL! -->
42 <a href="javascript:evil_function()">a link</a>
43 <a href="#" onclick="evil_function()">another link</a>
44 <p onclick="evil_function()">a paragraph</p>
45 <div style="display: none">secret EVIL!</div>
46 <object> of EVIL! </object>
47 <iframe src="evil-site"></iframe>
48 <form action="evil-site">
49 Password: <input type="password" name="password">
50 </form>
51 <blink>annoying EVIL!</blink>
52 <a href="evil-site">spam spam SPAM!</a>
53 <image src="evil!">
54 </body>
55 </html>""" # example from lxml: /usr/share/doc/python-lxml-doc/html/lxmlhtml.html#cleaning-up-html
56
57 EVIL_HTML2 = """<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>"""
58
59 def setUp(self):
60 self.host = helpers.FakeSAT()
61 reload(plugin_misc_text_syntaxes) # reload the plugin to avoid conflict error
62 self.text_syntaxes = plugin_misc_text_syntaxes.TextSyntaxes(self.host)
63
64 def test_xhtml_sanitise(self):
65 expected = u"""<div>
66 <style>/* deleted */</style>
67 <body>
68 <a href="">a link</a>
69 <a href="#">another link</a>
70 <p>a paragraph</p>
71 <div style="">secret EVIL!</div>
72 of EVIL!
73 Password:
74 annoying EVIL!
75 <a href="evil-site">spam spam SPAM!</a>
76 <img src="evil!">
77 </img></body>
78 </div>"""
79
80 d = self.text_syntaxes.cleanXHTML(self.EVIL_HTML1)
81 d.addCallback(self.assertEqualXML, expected, ignore_blank=True)
82 return d
83
84 def test_styles_sanitise(self):
85 expected = u"""<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>"""
86
87 d = self.text_syntaxes.cleanXHTML(self.EVIL_HTML2)
88 d.addCallback(self.assertEqualXML, expected)
89 return d
90
91 def test_html2text(self):
92 """Check that html2text is not inserting \n in the middle of that link.
93 By default lines are truncated after the 79th characters."""
94 source = "<img src=\"http://sat.goffi.org/static/images/screenshots/libervia/libervia_discussions.png\" alt=\"sat\"/>"
95 expected = "![sat](http://sat.goffi.org/static/images/screenshots/libervia/libervia_discussions.png)"
96 try:
97 d = self.text_syntaxes.convert(source, self.text_syntaxes.SYNTAX_XHTML, self.text_syntaxes.SYNTAX_MARKDOWN)
98 except plugin_misc_text_syntaxes.UnknownSyntax:
99 raise SkipTest("Markdown syntax is not available.")
100 d.addCallback(self.assertEqual, expected)
101 return d
102
103 def test_removeXHTMLMarkups(self):
104 expected = u""" a link another link a paragraph secret EVIL! of EVIL! Password: annoying EVIL! spam spam SPAM! """
105 result = self.text_syntaxes._removeMarkups(self.EVIL_HTML1)
106 self.assertEqual(re.sub(r"\s+", " ", result).rstrip(), expected.rstrip())
107
108 expected = u"""test retest toto"""
109 result = self.text_syntaxes._removeMarkups(self.EVIL_HTML2)
110 self.assertEqual(re.sub(r"\s+", " ", result).rstrip(), expected.rstrip())
111