Mercurial > genshi > mirror
view markup/tests/filters.py @ 187:3932175304db stable-0.2.x 0.2.0
Prepare [milestone:0.2] release.
author | cmlenz |
---|---|
date | Tue, 22 Aug 2006 15:28:35 +0000 |
parents | d1ce85a7f296 |
children | 51d4101f49ca |
line wrap: on
line source
# -*- coding: utf-8 -*- # # Copyright (C) 2006 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://markup.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://markup.edgewall.org/log/. import doctest import unittest from markup.core import Stream from markup.input import HTML, ParseError from markup.filters import HTMLSanitizer class HTMLSanitizerTestCase(unittest.TestCase): def test_sanitize_unchanged(self): html = HTML('<a href="#">fo<br />o</a>') self.assertEquals(u'<a href="#">fo<br/>o</a>', unicode(html.filter(HTMLSanitizer()))) def test_sanitize_escape_text(self): html = HTML('<a href="#">fo&</a>') self.assertEquals(u'<a href="#">fo&</a>', unicode(html.filter(HTMLSanitizer()))) html = HTML('<a href="#"><foo></a>') self.assertEquals(u'<a href="#"><foo></a>', unicode(html.filter(HTMLSanitizer()))) def test_sanitize_entityref_text(self): html = HTML('<a href="#">foö</a>') self.assertEquals(u'<a href="#">foƶ</a>', unicode(html.filter(HTMLSanitizer()))) def test_sanitize_escape_attr(self): html = HTML('<div title="<foo>"></div>') self.assertEquals(u'<div title="<foo>"/>', unicode(html.filter(HTMLSanitizer()))) def test_sanitize_close_empty_tag(self): html = HTML('<a href="#">fo<br>o</a>') self.assertEquals(u'<a href="#">fo<br/>o</a>', unicode(html.filter(HTMLSanitizer()))) def test_sanitize_invalid_entity(self): html = HTML('&junk;') self.assertEquals('&junk;', unicode(html.filter(HTMLSanitizer()))) def test_sanitize_remove_script_elem(self): html = HTML('<script>alert("Foo")</script>') self.assertEquals(u'', unicode(html.filter(HTMLSanitizer()))) html = HTML('<SCRIPT SRC="http://example.com/"></SCRIPT>') self.assertEquals(u'', unicode(html.filter(HTMLSanitizer()))) self.assertRaises(ParseError, HTML, '<SCR\0IPT>alert("foo")</SCR\0IPT>') self.assertRaises(ParseError, HTML, '<SCRIPT&XYZ SRC="http://example.com/"></SCRIPT>') def test_sanitize_remove_onclick_attr(self): html = HTML('<div onclick=\'alert("foo")\' />') self.assertEquals(u'<div/>', unicode(html.filter(HTMLSanitizer()))) def test_sanitize_remove_style_scripts(self): # Inline style with url() using javascript: scheme html = HTML('<DIV STYLE=\'background: url(javascript:alert("foo"))\'>') self.assertEquals(u'<div/>', unicode(html.filter(HTMLSanitizer()))) # Inline style with url() using javascript: scheme, using control char html = HTML('<DIV STYLE=\'background: url(javascript:alert("foo"))\'>') self.assertEquals(u'<div/>', unicode(html.filter(HTMLSanitizer()))) # Inline style with url() using javascript: scheme, in quotes html = HTML('<DIV STYLE=\'background: url("javascript:alert(foo)")\'>') self.assertEquals(u'<div/>', unicode(html.filter(HTMLSanitizer()))) # IE expressions in CSS not allowed html = HTML('<DIV STYLE=\'width: expression(alert("foo"));\'>') self.assertEquals(u'<div/>', unicode(html.filter(HTMLSanitizer()))) html = HTML('<DIV STYLE=\'background: url(javascript:alert("foo"));' 'color: #fff\'>') self.assertEquals(u'<div style="color: #fff"/>', unicode(html.filter(HTMLSanitizer()))) def test_sanitize_remove_src_javascript(self): html = HTML('<img src=\'javascript:alert("foo")\'>') self.assertEquals(u'<img/>', unicode(html.filter(HTMLSanitizer()))) # Case-insensitive protocol matching html = HTML('<IMG SRC=\'JaVaScRiPt:alert("foo")\'>') self.assertEquals(u'<img/>', unicode(html.filter(HTMLSanitizer()))) # Grave accents (not parsed) self.assertRaises(ParseError, HTML, '<IMG SRC=`javascript:alert("RSnake says, \'foo\'")`>') # Protocol encoded using UTF-8 numeric entities html = HTML('<IMG SRC=\'javascri' 'pt:alert("foo")\'>') self.assertEquals(u'<img/>', unicode(html.filter(HTMLSanitizer()))) # 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(u'<img/>', unicode(html.filter(HTMLSanitizer()))) # 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(u'<img/>', unicode(html.filter(HTMLSanitizer()))) # Embedded tab character in protocol html = HTML('<IMG SRC=\'jav\tascript:alert("foo");\'>') self.assertEquals(u'<img/>', unicode(html.filter(HTMLSanitizer()))) # Embedded tab character in protocol, but encoded this time html = HTML('<IMG SRC=\'jav	ascript:alert("foo");\'>') self.assertEquals(u'<img/>', unicode(html.filter(HTMLSanitizer()))) def suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(HTMLSanitizerTestCase, 'test')) return suite if __name__ == '__main__': unittest.main(defaultTest='suite')