File: html.py

package info (click to toggle)
genshi 0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,612 kB
  • sloc: python: 15,882; ansic: 557; xml: 45; cs: 31; makefile: 15
file content (494 lines) | stat: -rw-r--r-- 21,299 bytes parent folder | download
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# -*- coding: utf-8 -*-
#
# Copyright (C) 2006-2009 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://genshi.edgewall.org/wiki/License.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://genshi.edgewall.org/log/.

import doctest
import unittest

from genshi.input import HTML, ParseError
from genshi.filters.html import HTMLFormFiller, HTMLSanitizer
from genshi.template import MarkupTemplate

class HTMLFormFillerTestCase(unittest.TestCase):

    def test_fill_input_text_no_value(self):
        html = HTML("""<form><p>
          <input type="text" name="foo" />
        </p></form>""") | HTMLFormFiller()
        self.assertEquals("""<form><p>
          <input type="text" name="foo"/>
        </p></form>""", html.render())

    def test_fill_input_text_single_value(self):
        html = HTML("""<form><p>
          <input type="text" name="foo" />
        </p></form>""") | HTMLFormFiller(data={'foo': 'bar'})
        self.assertEquals("""<form><p>
          <input type="text" name="foo" value="bar"/>
        </p></form>""", html.render())

    def test_fill_input_text_multi_value(self):
        html = HTML("""<form><p>
          <input type="text" name="foo" />
        </p></form>""") | HTMLFormFiller(data={'foo': ['bar']})
        self.assertEquals("""<form><p>
          <input type="text" name="foo" value="bar"/>
        </p></form>""", html.render())

    def test_fill_input_hidden_no_value(self):
        html = HTML("""<form><p>
          <input type="hidden" name="foo" />
        </p></form>""") | HTMLFormFiller()
        self.assertEquals("""<form><p>
          <input type="hidden" name="foo"/>
        </p></form>""", html.render())

    def test_fill_input_hidden_single_value(self):
        html = HTML("""<form><p>
          <input type="hidden" name="foo" />
        </p></form>""") | HTMLFormFiller(data={'foo': 'bar'})
        self.assertEquals("""<form><p>
          <input type="hidden" name="foo" value="bar"/>
        </p></form>""", html.render())

    def test_fill_input_hidden_multi_value(self):
        html = HTML("""<form><p>
          <input type="hidden" name="foo" />
        </p></form>""") | HTMLFormFiller(data={'foo': ['bar']})
        self.assertEquals("""<form><p>
          <input type="hidden" name="foo" value="bar"/>
        </p></form>""", html.render())

    def test_fill_textarea_no_value(self):
        html = HTML("""<form><p>
          <textarea name="foo"></textarea>
        </p></form>""") | HTMLFormFiller()
        self.assertEquals("""<form><p>
          <textarea name="foo"/>
        </p></form>""", html.render())

    def test_fill_textarea_single_value(self):
        html = HTML("""<form><p>
          <textarea name="foo"></textarea>
        </p></form>""") | HTMLFormFiller(data={'foo': 'bar'})
        self.assertEquals("""<form><p>
          <textarea name="foo">bar</textarea>
        </p></form>""", html.render())

    def test_fill_textarea_multi_value(self):
        html = HTML("""<form><p>
          <textarea name="foo"></textarea>
        </p></form>""") | HTMLFormFiller(data={'foo': ['bar']})
        self.assertEquals("""<form><p>
          <textarea name="foo">bar</textarea>
        </p></form>""", html.render())

    def test_fill_input_checkbox_no_value(self):
        html = HTML("""<form><p>
          <input type="checkbox" name="foo" />
        </p></form>""") | HTMLFormFiller()
        self.assertEquals("""<form><p>
          <input type="checkbox" name="foo"/>
        </p></form>""", html.render())

    def test_fill_input_checkbox_single_value_auto(self):
        html = HTML("""<form><p>
          <input type="checkbox" name="foo" />
        </p></form>""")
        self.assertEquals("""<form><p>
          <input type="checkbox" name="foo"/>
        </p></form>""", (html | HTMLFormFiller(data={'foo': ''})).render())
        self.assertEquals("""<form><p>
          <input type="checkbox" name="foo" checked="checked"/>
        </p></form>""", (html | HTMLFormFiller(data={'foo': 'on'})).render())

    def test_fill_input_checkbox_single_value_defined(self):
        html = HTML("""<form><p>
          <input type="checkbox" name="foo" value="1" />
        </p></form>""")
        self.assertEquals("""<form><p>
          <input type="checkbox" name="foo" value="1" checked="checked"/>
        </p></form>""", (html | HTMLFormFiller(data={'foo': '1'})).render())
        self.assertEquals("""<form><p>
          <input type="checkbox" name="foo" value="1"/>
        </p></form>""", (html | HTMLFormFiller(data={'foo': '2'})).render())

    def test_fill_input_checkbox_multi_value_auto(self):
        html = HTML("""<form><p>
          <input type="checkbox" name="foo" />
        </p></form>""")
        self.assertEquals("""<form><p>
          <input type="checkbox" name="foo"/>
        </p></form>""", (html | HTMLFormFiller(data={'foo': []})).render())
        self.assertEquals("""<form><p>
          <input type="checkbox" name="foo" checked="checked"/>
        </p></form>""", (html | HTMLFormFiller(data={'foo': ['on']})).render())

    def test_fill_input_checkbox_multi_value_defined(self):
        html = HTML("""<form><p>
          <input type="checkbox" name="foo" value="1" />
        </p></form>""")
        self.assertEquals("""<form><p>
          <input type="checkbox" name="foo" value="1" checked="checked"/>
        </p></form>""", (html | HTMLFormFiller(data={'foo': ['1']})).render())
        self.assertEquals("""<form><p>
          <input type="checkbox" name="foo" value="1"/>
        </p></form>""", (html | HTMLFormFiller(data={'foo': ['2']})).render())

    def test_fill_input_radio_no_value(self):
        html = HTML("""<form><p>
          <input type="radio" name="foo" />
        </p></form>""") | HTMLFormFiller()
        self.assertEquals("""<form><p>
          <input type="radio" name="foo"/>
        </p></form>""", html.render())

    def test_fill_input_radio_single_value(self):
        html = HTML("""<form><p>
          <input type="radio" name="foo" value="1" />
        </p></form>""")
        self.assertEquals("""<form><p>
          <input type="radio" name="foo" value="1" checked="checked"/>
        </p></form>""", (html | HTMLFormFiller(data={'foo': '1'})).render())
        self.assertEquals("""<form><p>
          <input type="radio" name="foo" value="1"/>
        </p></form>""", (html | HTMLFormFiller(data={'foo': '2'})).render())

    def test_fill_input_radio_multi_value(self):
        html = HTML("""<form><p>
          <input type="radio" name="foo" value="1" />
        </p></form>""")
        self.assertEquals("""<form><p>
          <input type="radio" name="foo" value="1" checked="checked"/>
        </p></form>""", (html | HTMLFormFiller(data={'foo': ['1']})).render())
        self.assertEquals("""<form><p>
          <input type="radio" name="foo" value="1"/>
        </p></form>""", (html | HTMLFormFiller(data={'foo': ['2']})).render())

    def test_fill_select_no_value_auto(self):
        html = HTML("""<form><p>
          <select name="foo">
            <option>1</option>
            <option>2</option>
            <option>3</option>
          </select>
        </p></form>""") | HTMLFormFiller()
        self.assertEquals("""<form><p>
          <select name="foo">
            <option>1</option>
            <option>2</option>
            <option>3</option>
          </select>
        </p></form>""", html.render())

    def test_fill_select_no_value_defined(self):
        html = HTML("""<form><p>
          <select name="foo">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
          </select>
        </p></form>""") | HTMLFormFiller()
        self.assertEquals("""<form><p>
          <select name="foo">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
          </select>
        </p></form>""", html.render())

    def test_fill_select_single_value_auto(self):
        html = HTML("""<form><p>
          <select name="foo">
            <option>1</option>
            <option>2</option>
            <option>3</option>
          </select>
        </p></form>""") | HTMLFormFiller(data={'foo': '1'})
        self.assertEquals("""<form><p>
          <select name="foo">
            <option selected="selected">1</option>
            <option>2</option>
            <option>3</option>
          </select>
        </p></form>""", html.render())

    def test_fill_select_single_value_defined(self):
        html = HTML("""<form><p>
          <select name="foo">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
          </select>
        </p></form>""") | HTMLFormFiller(data={'foo': '1'})
        self.assertEquals("""<form><p>
          <select name="foo">
            <option value="1" selected="selected">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
          </select>
        </p></form>""", html.render())

    def test_fill_select_multi_value_auto(self):
        html = HTML("""<form><p>
          <select name="foo" multiple>
            <option>1</option>
            <option>2</option>
            <option>3</option>
          </select>
        </p></form>""") | HTMLFormFiller(data={'foo': ['1', '3']})
        self.assertEquals("""<form><p>
          <select name="foo" multiple="multiple">
            <option selected="selected">1</option>
            <option>2</option>
            <option selected="selected">3</option>
          </select>
        </p></form>""", html.render())

    def test_fill_select_multi_value_defined(self):
        html = HTML("""<form><p>
          <select name="foo" multiple>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
          </select>
        </p></form>""") | HTMLFormFiller(data={'foo': ['1', '3']})
        self.assertEquals("""<form><p>
          <select name="foo" multiple="multiple">
            <option value="1" selected="selected">1</option>
            <option value="2">2</option>
            <option value="3" selected="selected">3</option>
          </select>
        </p></form>""", html.render())

    def test_fill_option_segmented_text(self):
        html = MarkupTemplate("""<form>
          <select name="foo">
            <option value="1">foo $x</option>
          </select>
        </form>""").generate(x=1) | HTMLFormFiller(data={'foo': '1'})
        self.assertEquals("""<form>
          <select name="foo">
            <option value="1" selected="selected">foo 1</option>
          </select>
        </form>""", html.render())

    def test_fill_option_segmented_text_no_value(self):
        html = MarkupTemplate("""<form>
          <select name="foo">
            <option>foo $x bar</option>
          </select>
        </form>""").generate(x=1) | HTMLFormFiller(data={'foo': 'foo 1 bar'})
        self.assertEquals("""<form>
          <select name="foo">
            <option selected="selected">foo 1 bar</option>
          </select>
        </form>""", html.render())

    def test_fill_option_unicode_value(self):
        html = HTML("""<form>
          <select name="foo">
            <option value="&ouml;">foo</option>
          </select>
        </form>""") | HTMLFormFiller(data={'foo': u'ö'})
        self.assertEquals(u"""<form>
          <select name="foo">
            <option value="ö" selected="selected">foo</option>
          </select>
        </form>""", html.render(encoding=None))

    def test_fill_input_password_disabled(self):
        html = HTML("""<form><p>
          <input type="password" name="pass" />
        </p></form>""") | HTMLFormFiller(data={'pass': 'bar'})
        self.assertEquals("""<form><p>
          <input type="password" name="pass"/>
        </p></form>""", html.render())

    def test_fill_input_password_enabled(self):
        html = HTML("""<form><p>
          <input type="password" name="pass" />
        </p></form>""") | HTMLFormFiller(data={'pass': '1234'}, passwords=True)
        self.assertEquals("""<form><p>
          <input type="password" name="pass" value="1234"/>
        </p></form>""", html.render())


class HTMLSanitizerTestCase(unittest.TestCase):

    def test_sanitize_unchanged(self):
        html = HTML('<a href="#">fo<br />o</a>')
        self.assertEquals('<a href="#">fo<br/>o</a>',
                          (html | HTMLSanitizer()).render())
        html = HTML('<a href="#with:colon">foo</a>')
        self.assertEquals('<a href="#with:colon">foo</a>',
                          (html | HTMLSanitizer()).render())

    def test_sanitize_escape_text(self):
        html = HTML('<a href="#">fo&amp;</a>')
        self.assertEquals('<a href="#">fo&amp;</a>',
                          (html | HTMLSanitizer()).render())
        html = HTML('<a href="#">&lt;foo&gt;</a>')
        self.assertEquals('<a href="#">&lt;foo&gt;</a>',
                          (html | HTMLSanitizer()).render())

    def test_sanitize_entityref_text(self):
        html = HTML('<a href="#">fo&ouml;</a>')
        self.assertEquals(u'<a href="#">foö</a>',
                          (html | HTMLSanitizer()).render(encoding=None))

    def test_sanitize_escape_attr(self):
        html = HTML('<div title="&lt;foo&gt;"></div>')
        self.assertEquals('<div title="&lt;foo&gt;"/>',
                          (html | HTMLSanitizer()).render())

    def test_sanitize_close_empty_tag(self):
        html = HTML('<a href="#">fo<br>o</a>')
        self.assertEquals('<a href="#">fo<br/>o</a>',
                          (html | HTMLSanitizer()).render())

    def test_sanitize_invalid_entity(self):
        html = HTML('&junk;')
        self.assertEquals('&amp;junk;', (html | HTMLSanitizer()).render())

    def test_sanitize_remove_script_elem(self):
        html = HTML('<script>alert("Foo")</script>')
        self.assertEquals('', (html | HTMLSanitizer()).render())
        html = HTML('<SCRIPT SRC="http://example.com/"></SCRIPT>')
        self.assertEquals('', (html | HTMLSanitizer()).render())

        # Before Python 2.7.3, HTMLParser() raises a ParseError
        try:
            html = HTML('<SCR\0IPT>alert("foo")</SCR\0IPT>')
        except ParseError:
            pass
        else:
            self.assertEquals('&lt;SCR\x00IPT&gt;alert("foo")',
                              (html | HTMLSanitizer()).render())

        # Before Python 2.7.3, HTMLParser() raises a ParseError
        try:
            html = HTML('<SCRIPT&XYZ SRC="http://example.com/"></SCRIPT>')
        except ParseError:
            pass
        else:
            self.assertEquals('&lt;SCRIPT&amp;XYZ; SRC="http://example.com/"&gt;',
                              (html | HTMLSanitizer()).render())

    def test_sanitize_remove_onclick_attr(self):
        html = HTML('<div onclick=\'alert("foo")\' />')
        self.assertEquals('<div/>', (html | HTMLSanitizer()).render())

    def test_sanitize_remove_input_password(self):
        html = HTML('<form><input type="password" /></form>')
        self.assertEquals('<form/>', (html | HTMLSanitizer()).render())

    def test_sanitize_remove_comments(self):
        html = HTML('''<div><!-- conditional comment crap --></div>''')
        self.assertEquals('<div/>', (html | HTMLSanitizer()).render())

    def test_sanitize_remove_style_scripts(self):
        sanitizer = HTMLSanitizer(safe_attrs=HTMLSanitizer.SAFE_ATTRS | set(['style']))
        # Inline style with url() using javascript: scheme
        html = HTML('<DIV STYLE=\'background: url(javascript:alert("foo"))\'>')
        self.assertEquals('<div/>', (html | sanitizer).render())
        # Inline style with url() using javascript: scheme, using control char
        html = HTML('<DIV STYLE=\'background: url(&#1;javascript:alert("foo"))\'>')
        self.assertEquals('<div/>', (html | sanitizer).render())
        # Inline style with url() using javascript: scheme, in quotes
        html = HTML('<DIV STYLE=\'background: url("javascript:alert(foo)")\'>')
        self.assertEquals('<div/>', (html | sanitizer).render())
        # IE expressions in CSS not allowed
        html = HTML('<DIV STYLE=\'width: expression(alert("foo"));\'>')
        self.assertEquals('<div/>', (html | sanitizer).render())
        html = HTML('<DIV STYLE=\'width: e/**/xpression(alert("foo"));\'>')
        self.assertEquals('<div/>', (html | sanitizer).render())
        html = HTML('<DIV STYLE=\'background: url(javascript:alert("foo"));'
                                 'color: #fff\'>')
        self.assertEquals('<div style="color: #fff"/>',
                          (html | sanitizer).render())
        # Inline style with url() using javascript: scheme, using unicode
        # escapes
        html = HTML('<DIV STYLE=\'background: \\75rl(javascript:alert("foo"))\'>')
        self.assertEquals('<div/>', (html | sanitizer).render())
        html = HTML('<DIV STYLE=\'background: \\000075rl(javascript:alert("foo"))\'>')
        self.assertEquals('<div/>', (html | sanitizer).render())
        html = HTML('<DIV STYLE=\'background: \\75 rl(javascript:alert("foo"))\'>')
        self.assertEquals('<div/>', (html | sanitizer).render())
        html = HTML('<DIV STYLE=\'background: \\000075 rl(javascript:alert("foo"))\'>')
        self.assertEquals('<div/>', (html | sanitizer).render())
        html = HTML('<DIV STYLE=\'background: \\000075\r\nrl(javascript:alert("foo"))\'>')
        self.assertEquals('<div/>', (html | sanitizer).render())

    def test_sanitize_remove_style_phishing(self):
        sanitizer = HTMLSanitizer(safe_attrs=HTMLSanitizer.SAFE_ATTRS | set(['style']))
        # The position property is not allowed
        html = HTML('<div style="position:absolute;top:0"></div>')
        self.assertEquals('<div style="top:0"/>', (html | sanitizer).render())
        # Normal margins get passed through
        html = HTML('<div style="margin:10px 20px"></div>')
        self.assertEquals('<div style="margin:10px 20px"/>',
                          (html | sanitizer).render())
        # But not negative margins
        html = HTML('<div style="margin:-1000px 0 0"></div>')
        self.assertEquals('<div/>', (html | sanitizer).render())
        html = HTML('<div style="margin-left:-2000px 0 0"></div>')
        self.assertEquals('<div/>', (html | sanitizer).render())
        html = HTML('<div style="margin-left:1em 1em 1em -4000px"></div>')
        self.assertEquals('<div/>', (html | sanitizer).render())

    def test_sanitize_remove_src_javascript(self):
        html = HTML('<img src=\'javascript:alert("foo")\'>')
        self.assertEquals('<img/>', (html | HTMLSanitizer()).render())
        # Case-insensitive protocol matching
        html = HTML('<IMG SRC=\'JaVaScRiPt:alert("foo")\'>')
        self.assertEquals('<img/>', (html | HTMLSanitizer()).render())
        # Grave accents. Before Python 2.7.3, HTMLParser() raises a ParseError
        try:
            html = HTML('<IMG SRC=`javascript:alert("RSnake says, \'foo\'")`>')
        except ParseError:
            pass
        else:
            self.assertEquals('<img/>', (html | HTMLSanitizer()).render())
        # Protocol encoded using UTF-8 numeric entities
        html = HTML('<IMG SRC=\'&#106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;'
                    '&#112;&#116;&#58;alert("foo")\'>')
        self.assertEquals('<img/>', (html | HTMLSanitizer()).render())
        # Protocol encoded using UTF-8 numeric entities without a semicolon
        # (which is allowed because the max number of digits is used)
        html = HTML('<IMG SRC=\'&#0000106&#0000097&#0000118&#0000097'
                    '&#0000115&#0000099&#0000114&#0000105&#0000112&#0000116'
                    '&#0000058alert("foo")\'>')
        self.assertEquals('<img/>', (html | HTMLSanitizer()).render())
        # Protocol encoded using UTF-8 numeric hex entities without a semicolon
        # (which is allowed because the max number of digits is used)
        html = HTML('<IMG SRC=\'&#x6A&#x61&#x76&#x61&#x73&#x63&#x72&#x69'
                    '&#x70&#x74&#x3A;alert("foo")\'>')
        self.assertEquals('<img/>', (html | HTMLSanitizer()).render())
        # Embedded tab character in protocol
        html = HTML('<IMG SRC=\'jav\tascript:alert("foo");\'>')
        self.assertEquals('<img/>', (html | HTMLSanitizer()).render())
        # Embedded tab character in protocol, but encoded this time
        html = HTML('<IMG SRC=\'jav&#x09;ascript:alert("foo");\'>')
        self.assertEquals('<img/>', (html | HTMLSanitizer()).render())


def suite():
    suite = unittest.TestSuite()
    suite.addTest(doctest.DocTestSuite(HTMLFormFiller.__module__))
    suite.addTest(unittest.makeSuite(HTMLFormFillerTestCase, 'test'))
    suite.addTest(unittest.makeSuite(HTMLSanitizerTestCase, 'test'))
    return suite


if __name__ == '__main__':
    unittest.main(defaultTest='suite')