MediaWiki  REL1_19
SanitizerTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class SanitizerTest extends MediaWikiTestCase {
00004 
00005         function setUp() {
00006                 AutoLoader::loadClass( 'Sanitizer' );
00007         }
00008 
00009         function testDecodeNamedEntities() {
00010                 $this->assertEquals(
00011                         "\xc3\xa9cole",
00012                         Sanitizer::decodeCharReferences( '&eacute;cole' ),
00013                         'decode named entities'
00014                 );
00015         }
00016 
00017         function testDecodeNumericEntities() {
00018                 $this->assertEquals(
00019                         "\xc4\x88io bonas dans l'\xc3\xa9cole!",
00020                         Sanitizer::decodeCharReferences( "&#x108;io bonas dans l'&#233;cole!" ),
00021                         'decode numeric entities'
00022                 );
00023         }
00024 
00025         function testDecodeMixedEntities() {
00026                 $this->assertEquals(
00027                         "\xc4\x88io bonas dans l'\xc3\xa9cole!",
00028                         Sanitizer::decodeCharReferences( "&#x108;io bonas dans l'&eacute;cole!" ),
00029                         'decode mixed numeric/named entities'
00030                 );
00031         }
00032 
00033         function testDecodeMixedComplexEntities() {
00034                 $this->assertEquals(
00035                         "\xc4\x88io bonas dans l'\xc3\xa9cole! (mais pas &#x108;io dans l'&eacute;cole)",
00036                         Sanitizer::decodeCharReferences(
00037                                 "&#x108;io bonas dans l'&eacute;cole! (mais pas &amp;#x108;io dans l'&#38;eacute;cole)"
00038                         ),
00039                         'decode mixed complex entities'
00040                 );
00041         }
00042 
00043         function testInvalidAmpersand() {
00044                 $this->assertEquals(
00045                         'a & b',
00046                         Sanitizer::decodeCharReferences( 'a & b' ),
00047                         'Invalid ampersand'
00048                 );
00049         }
00050 
00051         function testInvalidEntities() {
00052                 $this->assertEquals(
00053                         '&foo;',
00054                         Sanitizer::decodeCharReferences( '&foo;' ),
00055                         'Invalid named entity'
00056                 );
00057         }
00058 
00059         function testInvalidNumberedEntities() {
00060                 $this->assertEquals( UTF8_REPLACEMENT, Sanitizer::decodeCharReferences( "&#88888888888888;" ), 'Invalid numbered entity' );
00061         }
00062 
00063         function testSelfClosingTag() {
00064                 $GLOBALS['wgUseTidy'] = false;
00065                 $this->assertEquals(
00066                         '<div>Hello world</div>',
00067                         Sanitizer::removeHTMLtags( '<div>Hello world</div />' ),
00068                         'Self-closing closing div'
00069                 );
00070         }
00071         
00072         function testDecodeTagAttributes() {
00073                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=bar' ), array( 'foo' => 'bar' ), 'Unquoted attribute' );
00074                 $this->assertEquals( Sanitizer::decodeTagAttributes( '    foo   =   bar    ' ), array( 'foo' => 'bar' ), 'Spaced attribute' );
00075                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo="bar"' ), array( 'foo' => 'bar' ), 'Double-quoted attribute' );
00076                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=\'bar\'' ), array( 'foo' => 'bar' ), 'Single-quoted attribute' );
00077                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=\'bar\'   baz="foo"' ), array( 'foo' => 'bar', 'baz' => 'foo' ), 'Several attributes' );
00078                 
00079                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=\'bar\'   baz="foo"' ), array( 'foo' => 'bar', 'baz' => 'foo' ), 'Several attributes' );
00080                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=\'bar\'   baz="foo"' ), array( 'foo' => 'bar', 'baz' => 'foo' ), 'Several attributes' );
00081                 
00082                 $this->assertEquals( Sanitizer::decodeTagAttributes( ':foo=\'bar\'' ), array( ':foo' => 'bar' ), 'Leading :' );
00083                 $this->assertEquals( Sanitizer::decodeTagAttributes( '_foo=\'bar\'' ), array( '_foo' => 'bar' ), 'Leading _' );
00084                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'Foo=\'bar\'' ), array( 'foo' => 'bar' ), 'Leading capital' );
00085                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'FOO=BAR' ), array( 'foo' => 'BAR' ), 'Attribute keys are normalized to lowercase' );
00086                 
00087                 # Invalid beginning
00088                 $this->assertEquals( Sanitizer::decodeTagAttributes( '-foo=bar' ), array(), 'Leading - is forbidden' );
00089                 $this->assertEquals( Sanitizer::decodeTagAttributes( '.foo=bar' ), array(), 'Leading . is forbidden' );
00090                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo-bar=bar' ), array( 'foo-bar' => 'bar' ), 'A - is allowed inside the attribute' );
00091                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo-=bar' ), array( 'foo-' => 'bar' ), 'A - is allowed inside the attribute' );
00092                 
00093                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo.bar=baz' ), array( 'foo.bar' => 'baz' ), 'A . is allowed inside the attribute' );
00094                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo.=baz' ), array( 'foo.' => 'baz' ), 'A . is allowed as last character' );
00095                 
00096                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo6=baz' ), array( 'foo6' => 'baz' ), 'Numbers are allowed' );
00097                 
00098                 # This bit is more relaxed than XML rules, but some extensions use it, like ProofreadPage (see bug 27539)
00099                 $this->assertEquals( Sanitizer::decodeTagAttributes( '1foo=baz' ), array( '1foo' => 'baz' ), 'Leading numbers are allowed' );
00100                 
00101                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo$=baz' ), array(), 'Symbols are not allowed' );
00102                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo@=baz' ), array(), 'Symbols are not allowed' );
00103                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo~=baz' ), array(), 'Symbols are not allowed' );
00104                 
00105                 
00106                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=1[#^`*%w/(' ), array( 'foo' => '1[#^`*%w/(' ), 'All kind of characters are allowed as values' );
00107                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo="1[#^`*%\'w/("' ), array( 'foo' => '1[#^`*%\'w/(' ), 'Double quotes are allowed if quoted by single quotes' );
00108                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=\'1[#^`*%"w/(\'' ), array( 'foo' => '1[#^`*%"w/(' ), 'Single quotes are allowed if quoted by double quotes' );
00109                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=&amp;&quot;' ), array( 'foo' => '&"' ), 'Special chars can be provided as entities' );
00110                 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=&foobar;' ), array( 'foo' => '&foobar;' ), 'Entity-like items are accepted' );
00111         }
00112 
00113         function testDeprecatedAttributes() {
00114                 $GLOBALS['wgCleanupPresentationalAttributes'] = true;
00115                 $this->assertEquals( Sanitizer::fixTagAttributes( 'clear="left"', 'br' ), ' style="clear: left;"', 'Deprecated attributes are converted to styles when enabled.' );
00116                 $this->assertEquals( Sanitizer::fixTagAttributes( 'clear="all"', 'br' ), ' style="clear: both;"', 'clear=all is converted to clear: both; not clear: all;' );
00117                 $this->assertEquals( Sanitizer::fixTagAttributes( 'CLEAR="ALL"', 'br' ), ' style="clear: both;"', 'clear=ALL is not treated differently from clear=all' );
00118                 $this->assertEquals( Sanitizer::fixTagAttributes( 'width="100"', 'td' ), ' style="width: 100px;"', 'Numeric sizes use pixels instead of numbers.' );
00119                 $this->assertEquals( Sanitizer::fixTagAttributes( 'width="100%"', 'td' ), ' style="width: 100%;"', 'Units are allowed in sizes.' );
00120                 $this->assertEquals( Sanitizer::fixTagAttributes( 'WIDTH="100%"', 'td' ), ' style="width: 100%;"', 'Uppercase WIDTH is treated as lowercase width.' );
00121                 $this->assertEquals( Sanitizer::fixTagAttributes( 'WiDTh="100%"', 'td' ), ' style="width: 100%;"', 'Mixed case does not break WiDTh.' );
00122                 $this->assertEquals( Sanitizer::fixTagAttributes( 'nowrap="true"', 'td' ), ' style="white-space: nowrap;"', 'nowrap attribute is output as white-space: nowrap; not something else.' );
00123                 $this->assertEquals( Sanitizer::fixTagAttributes( 'nowrap=""', 'td' ), ' style="white-space: nowrap;"', 'nowrap="" is considered true, not false' );
00124                 $this->assertEquals( Sanitizer::fixTagAttributes( 'NOWRAP="true"', 'td' ), ' style="white-space: nowrap;"', 'nowrap attribute works when uppercase.' );
00125                 $this->assertEquals( Sanitizer::fixTagAttributes( 'NoWrAp="true"', 'td' ), ' style="white-space: nowrap;"', 'nowrap attribute works when mixed-case.' );
00126                 $GLOBALS['wgCleanupPresentationalAttributes'] = false;
00127                 $this->assertEquals( Sanitizer::fixTagAttributes( 'clear="left"', 'br' ), ' clear="left"', 'Deprecated attributes are not converted to styles when enabled.' );
00128         }
00129 
00133         function testCssCommentsChecking( $expected, $css, $message = '' ) {
00134                 $this->assertEquals(
00135                         $expected,
00136                         Sanitizer::checkCss( $css ),
00137                         $message
00138                 );
00139         }
00140 
00141         function provideCssCommentsFixtures() {
00143                 return array(
00144                         array( ' ', '/**/' ),
00145                         array( ' ', '/****/' ),
00146                         array( ' ', '/* comment */' ),
00147                         array( ' ', "\\2f\\2a foo \\2a\\2f",
00148                                 'Backslash-escaped comments must be stripped (bug 28450)' ),
00149                         array( '', '/* unfinished comment structure',
00150                                 'Remove anything after a comment-start token' ),
00151                         array( '', "\\2f\\2a unifinished comment'",
00152                                 'Remove anything after a backslash-escaped comment-start token' ),
00153                 );
00154         }
00155 }
00156