/usr/lib/python3/dist-packages/genshi/filters/tests/test_html.py is in python3-genshi 0.7-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | # -*- 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_textarea_multiple(self):
# Ensure that the subsequent textarea doesn't get the data from the
# first
html = HTML("""<form><p>
<textarea name="foo"></textarea>
<textarea name="bar"></textarea>
</p></form>""") | HTMLFormFiller(data={'foo': 'Some text'})
self.assertEquals("""<form><p>
<textarea name="foo">Some text</textarea>
<textarea name="bar"/>
</p></form>""", html.render())
def test_fill_textarea_preserve_original(self):
html = HTML("""<form><p>
<textarea name="foo"></textarea>
<textarea name="bar">Original value</textarea>
</p></form>""") | HTMLFormFiller(data={'foo': 'Some text'})
self.assertEquals("""<form><p>
<textarea name="foo">Some text</textarea>
<textarea name="bar">Original value</textarea>
</p></form>""", html.render())
def test_fill_input_checkbox_single_value_auto_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>""", encoding='ascii')
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>""", encoding='ascii')
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_input_radio_empty_string(self):
html = HTML("""<form><p>
<input type="radio" name="foo" value="" />
</p></form>""")
self.assertEquals("""<form><p>
<input type="radio" name="foo" value="" checked="checked"/>
</p></form>""", (html | HTMLFormFiller(data={'foo': ''})).render())
def test_fill_input_radio_multi_empty_string(self):
html = HTML("""<form><p>
<input type="radio" name="foo" value="" />
</p></form>""")
self.assertEquals("""<form><p>
<input type="radio" name="foo" value="" checked="checked"/>
</p></form>""", (html | HTMLFormFiller(data={'foo': ['']})).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="ö">foo</option>
</select>
</form>""") | HTMLFormFiller(data={'foo': 'ö'})
self.assertEquals("""<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())
def StyleSanitizer():
safe_attrs = HTMLSanitizer.SAFE_ATTRS | frozenset(['style'])
return HTMLSanitizer(safe_attrs=safe_attrs)
class HTMLSanitizerTestCase(unittest.TestCase):
def assert_parse_error_or_equal(self, expected, exploit,
allow_strip=False):
try:
html = HTML(exploit)
except ParseError:
return
sanitized_html = (html | HTMLSanitizer()).render()
if not sanitized_html and allow_strip:
return
self.assertEquals(expected, sanitized_html)
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&</a>')
self.assertEquals('<a href="#">fo&</a>',
(html | HTMLSanitizer()).render())
html = HTML('<a href="#"><foo></a>')
self.assertEquals('<a href="#"><foo></a>',
(html | HTMLSanitizer()).render())
def test_sanitize_entityref_text(self):
html = HTML('<a href="#">foö</a>')
self.assertEquals('<a href="#">foö</a>',
(html | HTMLSanitizer()).render(encoding=None))
def test_sanitize_escape_attr(self):
html = HTML('<div title="<foo>"></div>')
self.assertEquals('<div title="<foo>"/>',
(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('&junk;', (html | HTMLSanitizer()).render())
@unittest.skip('http://genshi.edgewall.org/ticket/500#comment:3')
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())
src = '<SCR\0IPT>alert("foo")</SCR\0IPT>'
self.assert_parse_error_or_equal('<SCR\x00IPT>alert("foo")', src,
allow_strip=True)
src = '<SCRIPT&XYZ SRC="http://example.com/"></SCRIPT>'
self.assert_parse_error_or_equal('<SCRIPT&XYZ; '
'SRC="http://example.com/">', src,
allow_strip=True)
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 = StyleSanitizer()
# 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(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 = StyleSanitizer()
# 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 (not parsed)
src = '<IMG SRC=`javascript:alert("RSnake says, \'foo\'")`>'
self.assert_parse_error_or_equal('<img/>', src)
# Protocol encoded using UTF-8 numeric entities
html = HTML('<IMG SRC=\'javascri'
'pt: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=\'java'
'script'
':alert("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=\'javascri'
'pt: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	ascript:alert("foo");\'>')
self.assertEquals('<img/>', (html | HTMLSanitizer()).render())
def test_sanitize_expression(self):
html = HTML(r'<div style="top:expression(alert())">XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
def test_capital_expression(self):
html = HTML(r'<div style="top:EXPRESSION(alert())">XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
def test_sanitize_url_with_javascript(self):
html = HTML('<div style="background-image:url(javascript:alert())">'
'XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
def test_sanitize_capital_url_with_javascript(self):
html = HTML('<div style="background-image:URL(javascript:alert())">'
'XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
def test_sanitize_unicode_escapes(self):
html = HTML(r'<div style="top:exp\72 ess\000069 on(alert())">'
r'XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
def test_sanitize_backslash_without_hex(self):
html = HTML(r'<div style="top:e\xp\ression(alert())">XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
html = HTML(r'<div style="top:e\\xp\\ression(alert())">XSS</div>')
self.assertEqual(r'<div style="top:e\\xp\\ression(alert())">'
'XSS</div>',
str(html | StyleSanitizer()))
def test_sanitize_unsafe_props(self):
html = HTML('<div style="POSITION:RELATIVE">XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
html = HTML('<div style="behavior:url(test.htc)">XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
html = HTML('<div style="-ms-behavior:url(test.htc) url(#obj)">'
'XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
html = HTML("""<div style="-o-link:'javascript:alert(1)';"""
"""-o-link-source:current">XSS</div>""")
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
html = HTML("""<div style="-moz-binding:url(xss.xbl)">XSS</div>""")
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
def test_sanitize_negative_margin(self):
html = HTML('<div style="margin-top:-9999px">XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
html = HTML('<div style="margin:0 -9999px">XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
def test_sanitize_css_hack(self):
html = HTML('<div style="*position:static">XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
html = HTML('<div style="_margin:-10px">XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
def test_sanitize_property_name(self):
html = HTML('<div style="display:none;border-left-color:red;'
'user_defined:1;-moz-user-selct:-moz-all">prop</div>')
self.assertEqual('<div style="display:none; border-left-color:red'
'">prop</div>',
str(html | StyleSanitizer()))
def test_sanitize_unicode_expression(self):
# Fullwidth small letters
html = HTML('<div style="top:expression(alert())">'
'XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
# Fullwidth capital letters
html = HTML('<div style="top:EXPRESSION(alert())">'
'XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
# IPA extensions
html = HTML('<div style="top:expʀessɪoɴ(alert())">'
'XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
def test_sanitize_unicode_url(self):
# IPA extensions
html = HTML('<div style="background-image:uʀʟ(javascript:alert())">'
'XSS</div>')
self.assertEqual('<div>XSS</div>', str(html | StyleSanitizer()))
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')
|