3 class SanitizerTest
extends MediaWikiTestCase
{
5 protected function setUp() {
8 AutoLoader
::loadClass( 'Sanitizer' );
11 function testDecodeNamedEntities() {
14 Sanitizer
::decodeCharReferences( 'école' ),
15 'decode named entities'
19 function testDecodeNumericEntities() {
21 "\xc4\x88io bonas dans l'\xc3\xa9cole!",
22 Sanitizer
::decodeCharReferences( "Ĉio bonas dans l'école!" ),
23 'decode numeric entities'
27 function testDecodeMixedEntities() {
29 "\xc4\x88io bonas dans l'\xc3\xa9cole!",
30 Sanitizer
::decodeCharReferences( "Ĉio bonas dans l'école!" ),
31 'decode mixed numeric/named entities'
35 function testDecodeMixedComplexEntities() {
37 "\xc4\x88io bonas dans l'\xc3\xa9cole! (mais pas Ĉio dans l'école)",
38 Sanitizer
::decodeCharReferences(
39 "Ĉio bonas dans l'école! (mais pas Ĉio dans l'école)"
41 'decode mixed complex entities'
45 function testInvalidAmpersand() {
48 Sanitizer
::decodeCharReferences( 'a & b' ),
53 function testInvalidEntities() {
56 Sanitizer
::decodeCharReferences( '&foo;' ),
57 'Invalid named entity'
61 function testInvalidNumberedEntities() {
62 $this->assertEquals( UTF8_REPLACEMENT
, Sanitizer
::decodeCharReferences( "�" ), 'Invalid numbered entity' );
66 * @cover Sanitizer::removeHTMLtags
67 * @dataProvider provideHtml5Tags
69 * @param String $tag Name of an HTML5 element (ie: 'video')
70 * @param Boolean $escaped Wheter sanitizer let the tag in or escape it (ie: '<video>')
72 function testRemovehtmltagsOnHtml5Tags( $tag, $escaped ) {
73 $this->setMwGlobals( array(
80 $this->assertEquals( "<$tag>",
81 Sanitizer
::removeHTMLtags( "<$tag>" )
84 $this->assertEquals( "<$tag></$tag>\n",
85 Sanitizer
::removeHTMLtags( "<$tag>" )
93 function provideHtml5Tags() {
94 $ESCAPED = true; # We want tag to be escaped
95 $VERBATIM = false; # We want to keep the tag
97 array( 'data', $VERBATIM ),
98 array( 'mark', $VERBATIM ),
99 array( 'time', $VERBATIM ),
100 array( 'video', $ESCAPED ),
104 function testSelfClosingTag() {
105 $this->setMwGlobals( array(
110 '<div>Hello world</div>',
111 Sanitizer
::removeHTMLtags( '<div>Hello world</div />' ),
112 'Self-closing closing div'
116 function testDecodeTagAttributes() {
117 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=bar' ), array( 'foo' => 'bar' ), 'Unquoted attribute' );
118 $this->assertEquals( Sanitizer
::decodeTagAttributes( ' foo = bar ' ), array( 'foo' => 'bar' ), 'Spaced attribute' );
119 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo="bar"' ), array( 'foo' => 'bar' ), 'Double-quoted attribute' );
120 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=\'bar\'' ), array( 'foo' => 'bar' ), 'Single-quoted attribute' );
121 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=\'bar\' baz="foo"' ), array( 'foo' => 'bar', 'baz' => 'foo' ), 'Several attributes' );
123 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=\'bar\' baz="foo"' ), array( 'foo' => 'bar', 'baz' => 'foo' ), 'Several attributes' );
124 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=\'bar\' baz="foo"' ), array( 'foo' => 'bar', 'baz' => 'foo' ), 'Several attributes' );
126 $this->assertEquals( Sanitizer
::decodeTagAttributes( ':foo=\'bar\'' ), array( ':foo' => 'bar' ), 'Leading :' );
127 $this->assertEquals( Sanitizer
::decodeTagAttributes( '_foo=\'bar\'' ), array( '_foo' => 'bar' ), 'Leading _' );
128 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'Foo=\'bar\'' ), array( 'foo' => 'bar' ), 'Leading capital' );
129 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'FOO=BAR' ), array( 'foo' => 'BAR' ), 'Attribute keys are normalized to lowercase' );
132 $this->assertEquals( Sanitizer
::decodeTagAttributes( '-foo=bar' ), array(), 'Leading - is forbidden' );
133 $this->assertEquals( Sanitizer
::decodeTagAttributes( '.foo=bar' ), array(), 'Leading . is forbidden' );
134 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo-bar=bar' ), array( 'foo-bar' => 'bar' ), 'A - is allowed inside the attribute' );
135 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo-=bar' ), array( 'foo-' => 'bar' ), 'A - is allowed inside the attribute' );
137 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo.bar=baz' ), array( 'foo.bar' => 'baz' ), 'A . is allowed inside the attribute' );
138 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo.=baz' ), array( 'foo.' => 'baz' ), 'A . is allowed as last character' );
140 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo6=baz' ), array( 'foo6' => 'baz' ), 'Numbers are allowed' );
142 # This bit is more relaxed than XML rules, but some extensions use it, like ProofreadPage (see bug 27539)
143 $this->assertEquals( Sanitizer
::decodeTagAttributes( '1foo=baz' ), array( '1foo' => 'baz' ), 'Leading numbers are allowed' );
145 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo$=baz' ), array(), 'Symbols are not allowed' );
146 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo@=baz' ), array(), 'Symbols are not allowed' );
147 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo~=baz' ), array(), 'Symbols are not allowed' );
150 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=1[#^`*%w/(' ), array( 'foo' => '1[#^`*%w/(' ), 'All kind of characters are allowed as values' );
151 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo="1[#^`*%\'w/("' ), array( 'foo' => '1[#^`*%\'w/(' ), 'Double quotes are allowed if quoted by single quotes' );
152 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=\'1[#^`*%"w/(\'' ), array( 'foo' => '1[#^`*%"w/(' ), 'Single quotes are allowed if quoted by double quotes' );
153 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=&"' ), array( 'foo' => '&"' ), 'Special chars can be provided as entities' );
154 $this->assertEquals( Sanitizer
::decodeTagAttributes( 'foo=&foobar;' ), array( 'foo' => '&foobar;' ), 'Entity-like items are accepted' );
158 * @dataProvider provideDeprecatedAttributes
160 function testDeprecatedAttributesUnaltered( $inputAttr, $inputEl ) {
162 $this->assertEquals( " $inputAttr", Sanitizer
::fixTagAttributes( $inputAttr, $inputEl ) );
165 public static function provideDeprecatedAttributes() {
167 array( 'clear="left"', 'br' ),
168 array( 'clear="all"', 'br' ),
169 array( 'width="100"', 'td' ),
170 array( 'nowrap="true"', 'td' ),
171 array( 'nowrap=""', 'td' ),
172 array( 'align="right"', 'td' ),
173 array( 'align="center"', 'table' ),
174 array( 'align="left"', 'tr' ),
175 array( 'align="center"', 'div' ),
176 array( 'align="left"', 'h1' ),
177 array( 'align="left"', 'span' ),
182 * @dataProvider provideCssCommentsFixtures
184 function testCssCommentsChecking( $expected, $css, $message = '' ) {
187 Sanitizer
::checkCss( $css ),
192 public static function provideCssCommentsFixtures() {
193 /** array( <expected>, <css>, [message] ) */
195 array( ' ', '/**/' ),
196 array( ' ', '/****/' ),
197 array( ' ', '/* comment */' ),
198 array( ' ', "\\2f\\2a foo \\2a\\2f",
199 'Backslash-escaped comments must be stripped (bug 28450)' ),
200 array( '', '/* unfinished comment structure',
201 'Remove anything after a comment-start token' ),
202 array( '', "\\2f\\2a unifinished comment'",
203 'Remove anything after a backslash-escaped comment-start token' ),
204 array( '/* insecure input */', 'filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'asdf.png\',sizingMethod=\'scale\');'),
205 array( '/* insecure input */', '-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'asdf.png\',sizingMethod=\'scale\')";'),
206 array( '/* insecure input */', 'width: expression(1+1);'),
207 array( '/* insecure input */', 'background-image: image(asdf.png);'),
208 array( '/* insecure input */', 'background-image: -webkit-image(asdf.png);'),
209 array( '/* insecure input */', 'background-image: -moz-image(asdf.png);'),
210 array( '/* insecure input */', 'background-image: image-set("asdf.png" 1x, "asdf.png" 2x);'),
211 array( '/* insecure input */', 'background-image: -webkit-image-set("asdf.png" 1x, "asdf.png" 2x);'),
212 array( '/* insecure input */', 'background-image: -moz-image-set("asdf.png" 1x, "asdf.png" 2x);'),