4 * @todo Tests covering decodeCharReferences can be refactored into a single
5 * method and dataprovider.
7 class SanitizerTest
extends MediaWikiTestCase
{
10 * @covers Sanitizer::decodeCharReferences
12 public function testDecodeNamedEntities() {
15 Sanitizer
::decodeCharReferences( 'école' ),
16 'decode named entities'
21 * @covers Sanitizer::decodeCharReferences
23 public function testDecodeNumericEntities() {
25 "\xc4\x88io bonas dans l'\xc3\xa9cole!",
26 Sanitizer
::decodeCharReferences( "Ĉio bonas dans l'école!" ),
27 'decode numeric entities'
32 * @covers Sanitizer::decodeCharReferences
34 public function testDecodeMixedEntities() {
36 "\xc4\x88io bonas dans l'\xc3\xa9cole!",
37 Sanitizer
::decodeCharReferences( "Ĉio bonas dans l'école!" ),
38 'decode mixed numeric/named entities'
43 * @covers Sanitizer::decodeCharReferences
45 public function testDecodeMixedComplexEntities() {
47 "\xc4\x88io bonas dans l'\xc3\xa9cole! (mais pas Ĉio dans l'école)",
48 Sanitizer
::decodeCharReferences(
49 "Ĉio bonas dans l'école! (mais pas Ĉio dans l'école)"
51 'decode mixed complex entities'
56 * @covers Sanitizer::decodeCharReferences
58 public function testInvalidAmpersand() {
61 Sanitizer
::decodeCharReferences( 'a & b' ),
67 * @covers Sanitizer::decodeCharReferences
69 public function testInvalidEntities() {
72 Sanitizer
::decodeCharReferences( '&foo;' ),
73 'Invalid named entity'
78 * @covers Sanitizer::decodeCharReferences
80 public function testInvalidNumberedEntities() {
83 Sanitizer
::decodeCharReferences( "�" ),
84 'Invalid numbered entity'
89 * @covers Sanitizer::removeHTMLtags
90 * @dataProvider provideHtml5Tags
92 * @param string $tag Name of an HTML5 element (ie: 'video')
93 * @param bool $escaped Whether sanitizer let the tag in or escape it (ie: '<video>')
95 public function testRemovehtmltagsOnHtml5Tags( $tag, $escaped ) {
96 $this->setMwGlobals( array(
101 $this->assertEquals( "<$tag>",
102 Sanitizer
::removeHTMLtags( "<$tag>" )
105 $this->assertEquals( "<$tag></$tag>\n",
106 Sanitizer
::removeHTMLtags( "<$tag>" )
114 public static function provideHtml5Tags() {
115 $ESCAPED = true; # We want tag to be escaped
116 $VERBATIM = false; # We want to keep the tag
118 array( 'data', $VERBATIM ),
119 array( 'mark', $VERBATIM ),
120 array( 'time', $VERBATIM ),
121 array( 'video', $ESCAPED ),
125 function dataRemoveHTMLtags() {
127 // former testSelfClosingTag
129 '<div>Hello world</div />',
130 '<div>Hello world</div>',
131 'Self-closing closing div'
133 // Make sure special nested HTML5 semantics are not broken
134 // http://www.whatwg.org/html/text-level-semantics.html#the-kbd-element
136 '<kbd><kbd>Shift</kbd>+<kbd>F3</kbd></kbd>',
137 '<kbd><kbd>Shift</kbd>+<kbd>F3</kbd></kbd>',
140 // http://www.whatwg.org/html/text-level-semantics.html#the-sub-and-sup-elements
142 '<var>x<sub><var>i</var></sub></var>, <var>y<sub><var>i</var></sub></var>',
143 '<var>x<sub><var>i</var></sub></var>, <var>y<sub><var>i</var></sub></var>',
146 // http://www.whatwg.org/html/text-level-semantics.html#the-dfn-element
148 '<dfn><abbr title="Garage Door Opener">GDO</abbr></dfn>',
149 '<dfn><abbr title="Garage Door Opener">GDO</abbr></dfn>',
150 '<abbr> inside <dfn>',
156 * @dataProvider dataRemoveHTMLtags
157 * @covers Sanitizer::removeHTMLtags
159 public function testRemoveHTMLtags( $input, $output, $msg = null ) {
160 $GLOBALS['wgUseTidy'] = false;
161 $this->assertEquals( $output, Sanitizer
::removeHTMLtags( $input ), $msg );
165 * @dataProvider provideTagAttributesToDecode
166 * @covers Sanitizer::decodeTagAttributes
168 public function testDecodeTagAttributes( $expected, $attributes, $message = '' ) {
169 $this->assertEquals( $expected,
170 Sanitizer
::decodeTagAttributes( $attributes ),
175 public static function provideTagAttributesToDecode() {
177 array( array( 'foo' => 'bar' ), 'foo=bar', 'Unquoted attribute' ),
178 array( array( 'foo' => 'bar' ), ' foo = bar ', 'Spaced attribute' ),
179 array( array( 'foo' => 'bar' ), 'foo="bar"', 'Double-quoted attribute' ),
180 array( array( 'foo' => 'bar' ), 'foo=\'bar\'', 'Single-quoted attribute' ),
182 array( 'foo' => 'bar', 'baz' => 'foo' ),
183 'foo=\'bar\' baz="foo"',
187 array( 'foo' => 'bar', 'baz' => 'foo' ),
188 'foo=\'bar\' baz="foo"',
192 array( 'foo' => 'bar', 'baz' => 'foo' ),
193 'foo=\'bar\' baz="foo"',
196 array( array( ':foo' => 'bar' ), ':foo=\'bar\'', 'Leading :' ),
197 array( array( '_foo' => 'bar' ), '_foo=\'bar\'', 'Leading _' ),
198 array( array( 'foo' => 'bar' ), 'Foo=\'bar\'', 'Leading capital' ),
199 array( array( 'foo' => 'BAR' ), 'FOO=BAR', 'Attribute keys are normalized to lowercase' ),
202 array( array(), '-foo=bar', 'Leading - is forbidden' ),
203 array( array(), '.foo=bar', 'Leading . is forbidden' ),
204 array( array( 'foo-bar' => 'bar' ), 'foo-bar=bar', 'A - is allowed inside the attribute' ),
205 array( array( 'foo-' => 'bar' ), 'foo-=bar', 'A - is allowed inside the attribute' ),
206 array( array( 'foo.bar' => 'baz' ), 'foo.bar=baz', 'A . is allowed inside the attribute' ),
207 array( array( 'foo.' => 'baz' ), 'foo.=baz', 'A . is allowed as last character' ),
208 array( array( 'foo6' => 'baz' ), 'foo6=baz', 'Numbers are allowed' ),
210 # This bit is more relaxed than XML rules, but some extensions use
211 # it, like ProofreadPage (see bug 27539)
212 array( array( '1foo' => 'baz' ), '1foo=baz', 'Leading numbers are allowed' ),
213 array( array(), 'foo$=baz', 'Symbols are not allowed' ),
214 array( array(), 'foo@=baz', 'Symbols are not allowed' ),
215 array( array(), 'foo~=baz', 'Symbols are not allowed' ),
217 array( 'foo' => '1[#^`*%w/(' ),
219 'All kind of characters are allowed as values'
222 array( 'foo' => '1[#^`*%\'w/(' ),
223 'foo="1[#^`*%\'w/("',
224 'Double quotes are allowed if quoted by single quotes'
227 array( 'foo' => '1[#^`*%"w/(' ),
228 'foo=\'1[#^`*%"w/(\'',
229 'Single quotes are allowed if quoted by double quotes'
231 array( array( 'foo' => '&"' ), 'foo=&"', 'Special chars can be provided as entities' ),
232 array( array( 'foo' => '&foobar;' ), 'foo=&foobar;', 'Entity-like items are accepted' ),
237 * @dataProvider provideDeprecatedAttributes
238 * @covers Sanitizer::fixTagAttributes
240 public function testDeprecatedAttributesUnaltered( $inputAttr, $inputEl, $message = '' ) {
241 $this->assertEquals( " $inputAttr",
242 Sanitizer
::fixTagAttributes( $inputAttr, $inputEl ),
247 public static function provideDeprecatedAttributes() {
248 /** array( <attribute>, <element>, [message] ) */
250 array( 'clear="left"', 'br' ),
251 array( 'clear="all"', 'br' ),
252 array( 'width="100"', 'td' ),
253 array( 'nowrap="true"', 'td' ),
254 array( 'nowrap=""', 'td' ),
255 array( 'align="right"', 'td' ),
256 array( 'align="center"', 'table' ),
257 array( 'align="left"', 'tr' ),
258 array( 'align="center"', 'div' ),
259 array( 'align="left"', 'h1' ),
260 array( 'align="left"', 'p' ),
265 * @dataProvider provideCssCommentsFixtures
266 * @covers Sanitizer::checkCss
268 public function testCssCommentsChecking( $expected, $css, $message = '' ) {
269 $this->assertEquals( $expected,
270 Sanitizer
::checkCss( $css ),
275 public static function provideCssCommentsFixtures() {
276 /** array( <expected>, <css>, [message] ) */
278 // Valid comments spanning entire input
279 array( '/**/', '/**/' ),
280 array( '/* comment */', '/* comment */' ),
282 array( ' ', '/****/' ),
283 array( ' ', '/* /* */' ),
284 array( 'display: block;', "display:/* foo */block;" ),
285 array( 'display: block;', "display:\\2f\\2a foo \\2a\\2f block;",
286 'Backslash-escaped comments must be stripped (bug 28450)' ),
287 array( '', '/* unfinished comment structure',
288 'Remove anything after a comment-start token' ),
289 array( '', "\\2f\\2a unifinished comment'",
290 'Remove anything after a backslash-escaped comment-start token' ),
292 '/* insecure input */',
293 'filter: progid:DXImageTransform.Microsoft.AlphaImageLoader'
294 . '(src=\'asdf.png\',sizingMethod=\'scale\');'
297 '/* insecure input */',
298 '-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader'
299 . '(src=\'asdf.png\',sizingMethod=\'scale\')";'
301 array( '/* insecure input */', 'width: expression(1+1);' ),
302 array( '/* insecure input */', 'background-image: image(asdf.png);' ),
303 array( '/* insecure input */', 'background-image: -webkit-image(asdf.png);' ),
304 array( '/* insecure input */', 'background-image: -moz-image(asdf.png);' ),
305 array( '/* insecure input */', 'background-image: image-set("asdf.png" 1x, "asdf.png" 2x);' ),
307 '/* insecure input */',
308 'background-image: -webkit-image-set("asdf.png" 1x, "asdf.png" 2x);'
311 '/* insecure input */',
312 'background-image: -moz-image-set("asdf.png" 1x, "asdf.png" 2x);'
318 * Test for support or lack of support for specific attributes in the attribute whitelist.
320 public static function provideAttributeSupport() {
321 /** array( <attributes>, <expected>, <message> ) */
325 ' role="presentation"',
326 ' role="presentation"',
327 'Support for WAI-ARIA\'s role="presentation".'
329 array( 'div', ' role="main"', '', "Other WAI-ARIA roles are currently not supported." ),
334 * @dataProvider provideAttributeSupport
335 * @covers Sanitizer::fixTagAttributes
337 public function testAttributeSupport( $tag, $attributes, $expected, $message ) {
338 $this->assertEquals( $expected,
339 Sanitizer
::fixTagAttributes( $attributes, $tag ),