forked from ptmono/python-scrubber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
154 lines (145 loc) · 8.28 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import BeautifulSoup
from scrubber import Scrubber, SelectiveScriptScrubber
class ScrubberTestCase(unittest.TestCase):
tests = (
( # Invalid HTML
"""<div notRealAttribute="value\n"onmouseover="\nexecuteMe();\n"foo="bar">\nI will execute here, too, if you mouse over me\n</div>""",
"" if BeautifulSoup.__version__.startswith('3.1') else """<div>\nI will execute here, too, if you mouse over me\n</div>"""
),
( # Autolink
"""www.example.com<br>""",
"""<a href="http://www.example.com" rel="nofollow">www.example.com</a><br />"""
),
( # No autolinking of existing links
"""<a href="http://www.example.com">Example</a>""",
"""<a href="http://www.example.com" rel="nofollow" class="external">Example</a>"""
),
( # No enocoding of pre-encoded urls during autolink:
"""http://www.example.com/aaa%20bbb/test%20test.jpg<br/>""",
"""<a href="http://www.example.com/aaa%20bbb/test%20test.jpg" rel="nofollow">http://www.example.com/aaa%20bbb/test%20test.jpg</a><br />"""
),
( # Strip scripts
"""<div xmlns="http://www.w3.org/1999/xhtml">safe<script type="text/javascript">location.href='http:/'+'/example.com/';</script> description</div>""",
"""<div>safe description</div>""",
),
( # Remove target from links
"""<a href="www.google.com" target="_new">Google</a>""",
"""<a href="http://www.google.com" rel="nofollow" class="external">Google</a>"""
),
( # General cleaning (remove <br clear="all">, ...)
"""<br clear="all">""",
"""<br />"""
),
( # Converting b and i to strong and em
"""<b>strong</b> <i>em</i>""",
"""<strong>strong</strong> <em>em</em>"""
),
( # Encoded script (decimal)
"""<span style="any: expression(window.location='http://example.org/')">safe</span>""",
"""<span>safe</span>"""
),
( # Encoded script (hex)
"""<span style="any: expression(window.location='http://example.org/')">safe</span>""",
"""<span>safe</span>"""
),
( # Test unicode
"""Mitä kuuluu""",
"""Mitä kuuluu"""
),
( # Test embed
"""<embed src='http://videomedia.ign.com/ev/ev.swf' flashvars='object_ID=949610&downloadURL=http://pspmovies.ign.com/psp/video/article/852/852594/patapon_021508_flvlowwide.flv&allownetworking="all"' type='application/x-shockwave-flash' width='433' height='360' ></embed>""",
"""<embed src="http://videomedia.ign.com/ev/ev.swf" flashvars='object_ID=949610&downloadURL=http://pspmovies.ign.com/psp/video/article/852/852594/patapon_021508_flvlowwide.flv&allownetworking="all"' type="application/x-shockwave-flash" width="433" height="360"></embed>"""
),
( # Test evil code
"""<img src=""http://www.a.com/a.jpg<script type=text/javascript src="http://1.2.3.4:81/xss.js">" /><<img src=""http://www.a.com/a.jpg</script>""",
""
),
( # Bad font tags
"""<font size=+0>test</font> <font>wowzers</font> <font></font> <font><p>foo</p><i>bar</i></font>""",
"""test wowzers <p>foo</p><em>bar</em>"""
),
( # Stripping empty attributed
"""<font style="">Foo</font> <span id="">Bar</span>""",
"""Foo <span>Bar</span>"""
),
( # a0 == nbsp
"""test\xa0www.this.com""",
"""test\xa0<a href="http://www.this.com" rel="nofollow">www.this.com</a>"""
),
( # Remove comments
"Foo <!-- bar -->",
"Foo "
),
( # Layered font tags
"""<div><font size=+0><font size=+0><a href="http://www.google.com">test</a></font><font>ing</font> 123</font> abc</div>""",
"""<div><a href="http://www.google.com" rel="nofollow" class="external">test</a>ing 123 abc</div>"""
),
( # Save contents of tags specified in 'disallowed_tags_save_content'
"<blink>Foo</blink>",
"Foo"
),
( # Character entities shouldn't get autolinked
"""http://www.google.com """,
"""<a href="http://www.google.com" rel="nofollow">http://www.google.com</a> """
),
( # Test unicode with autolinker
"""http://www.google.com/?q=mitä""",
"""<a href="http://www.google.com/?q=mit%C3%A4" rel="nofollow">http://www.google.com/?q=mit\xe4</a>""",
),
( # Test mailto: links
"""<a href="mailto:[email protected]">Mail Test</a>""",
"""<a href="mailto:[email protected]" rel="nofollow" class="external">Mail Test</a>"""
),
( # Test removing a node but keeping the contents
"""<html><head><title>Title</title></head><body><div><blink>Hello</blink> World!</blink></div></body></html>""",
"""<div>Hello World!</div>"""
),
( # Make keeping content for incomplete tags works
"<blink><br><br>",
"<br /><br />"
),
)
def setUp(self):
self.scrubber = Scrubber()
def testScrubber(self):
for html, expected in self.tests:
self.assertEqual(self.scrubber.scrub(html), expected)
class SelectiveScriptScrubberTestCase(unittest.TestCase):
tests = (
( # Allowed src, remove body
'<script type="text/javascript" src="http://www.statcounter.com/counter/counter_xhtml.js">fewfewfwe</script>',
'<script type="text/javascript" src="http://www.statcounter.com/counter/counter_xhtml.js"></script>'
),
( # Disallowed src
'<script type="text/javascript" src="http://www.example.com/evil.js">fewfewfwe</script>',
''
),
( # Allowed inline
'<script type="text/javascript">var sc_project=123;</script>',
True
),
( # Disallowed inline
'<script type="text/javascript">alert(5);</script>',
''
),
( # Stat counter
"""<!-- Start of StatCounter Code --><script type="text/javascript">\nvar sc_project=1234; \nvar sc_invisible=0; \nvar sc_partition=12; \nvar sc_security="1234a5"; \n</script><script src="http://www.statcounter.com/counter/counter_xhtml.js" type="text/javascript"></script><noscript><div class="statcounter"><a href="http://www.statcounter.com/" class="statcounter" rel="nofollow"><img src="http://c37.statcounter.com/1234/0/020062e8/0/" alt="hit counter" class="statcounter" /></a></div></noscript><!-- End of StatCounter Code -->""",
"""<script type="text/javascript">\nvar sc_project=1234; \nvar sc_invisible=0; \nvar sc_partition=12; \nvar sc_security="1234a5"; \n</script><script src="http://www.statcounter.com/counter/counter_xhtml.js" type="text/javascript"></script><noscript><div class="statcounter"><a href="http://www.statcounter.com/" class="statcounter" rel="nofollow"><img src="http://c37.statcounter.com/1234/0/020062e8/0/" alt="hit counter" class="statcounter" /></a></div></noscript>"""
),
( # Google calendar
"""<iframe src="http://www.google.com/calendar/embed?title=test&height=300&wkst=1&bgcolor=%23FFFFFF&ctz=America%2FLos_Angeles" style=" border-width:0 " width="300" height="300" frameborder="0" scrolling="no"></iframe>""",
True
),
)
def setUp(self):
self.scrubber = SelectiveScriptScrubber()
def testScrubber(self):
for html, expected in self.tests:
if expected is True:
expected = html
self.assertEqual(self.scrubber.scrub(html), expected)
if __name__ == '__main__':
unittest.main()