2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
/**
|
|
|
|
|
* @todo Tests covering decodeCharReferences can be refactored into a single
|
|
|
|
|
* method and dataprovider.
|
|
|
|
|
*/
|
2010-12-28 18:17:16 +00:00
|
|
|
class SanitizerTest extends MediaWikiTestCase {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
AutoLoader::loadClass( 'Sanitizer' );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Sanitizer::decodeCharReferences
|
|
|
|
|
*/
|
|
|
|
|
public function testDecodeNamedEntities() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
"\xc3\xa9cole",
|
|
|
|
|
Sanitizer::decodeCharReferences( 'école' ),
|
|
|
|
|
'decode named entities'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Sanitizer::decodeCharReferences
|
|
|
|
|
*/
|
|
|
|
|
public function testDecodeNumericEntities() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
"\xc4\x88io bonas dans l'\xc3\xa9cole!",
|
|
|
|
|
Sanitizer::decodeCharReferences( "Ĉio bonas dans l'école!" ),
|
|
|
|
|
'decode numeric entities'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Sanitizer::decodeCharReferences
|
|
|
|
|
*/
|
|
|
|
|
public function testDecodeMixedEntities() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
"\xc4\x88io bonas dans l'\xc3\xa9cole!",
|
|
|
|
|
Sanitizer::decodeCharReferences( "Ĉio bonas dans l'école!" ),
|
|
|
|
|
'decode mixed numeric/named entities'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Sanitizer::decodeCharReferences
|
|
|
|
|
*/
|
|
|
|
|
public function testDecodeMixedComplexEntities() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
"\xc4\x88io bonas dans l'\xc3\xa9cole! (mais pas Ĉio dans l'école)",
|
|
|
|
|
Sanitizer::decodeCharReferences(
|
|
|
|
|
"Ĉio bonas dans l'école! (mais pas &#x108;io dans l'&eacute;cole)"
|
|
|
|
|
),
|
|
|
|
|
'decode mixed complex entities'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Sanitizer::decodeCharReferences
|
|
|
|
|
*/
|
|
|
|
|
public function testInvalidAmpersand() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
'a & b',
|
|
|
|
|
Sanitizer::decodeCharReferences( 'a & b' ),
|
|
|
|
|
'Invalid ampersand'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Sanitizer::decodeCharReferences
|
|
|
|
|
*/
|
|
|
|
|
public function testInvalidEntities() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
'&foo;',
|
|
|
|
|
Sanitizer::decodeCharReferences( '&foo;' ),
|
|
|
|
|
'Invalid named entity'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Sanitizer::decodeCharReferences
|
|
|
|
|
*/
|
|
|
|
|
public function testInvalidNumberedEntities() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals( UTF8_REPLACEMENT, Sanitizer::decodeCharReferences( "�" ), 'Invalid numbered entity' );
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-06 10:02:49 +00:00
|
|
|
/**
|
2013-03-11 03:16:28 +00:00
|
|
|
* @covers Sanitizer::removeHTMLtags
|
2012-08-06 10:02:49 +00:00
|
|
|
* @dataProvider provideHtml5Tags
|
|
|
|
|
*
|
|
|
|
|
* @param String $tag Name of an HTML5 element (ie: 'video')
|
|
|
|
|
* @param Boolean $escaped Wheter sanitizer let the tag in or escape it (ie: '<video>')
|
|
|
|
|
*/
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testRemovehtmltagsOnHtml5Tags( $tag, $escaped ) {
|
2012-12-09 10:03:03 +00:00
|
|
|
$this->setMwGlobals( array(
|
|
|
|
|
'wgUseTidy' => false
|
2013-02-14 11:36:35 +00:00
|
|
|
) );
|
2012-08-06 10:02:49 +00:00
|
|
|
|
2013-02-14 11:36:35 +00:00
|
|
|
if ( $escaped ) {
|
2012-08-06 10:02:49 +00:00
|
|
|
$this->assertEquals( "<$tag>",
|
|
|
|
|
Sanitizer::removeHTMLtags( "<$tag>" )
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertEquals( "<$tag></$tag>\n",
|
|
|
|
|
Sanitizer::removeHTMLtags( "<$tag>" )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provide HTML5 tags
|
|
|
|
|
*/
|
2013-03-22 02:12:37 +00:00
|
|
|
public static function provideHtml5Tags() {
|
2013-02-14 11:36:35 +00:00
|
|
|
$ESCAPED = true; # We want tag to be escaped
|
|
|
|
|
$VERBATIM = false; # We want to keep the tag
|
2012-08-06 10:02:49 +00:00
|
|
|
return array(
|
|
|
|
|
array( 'data', $VERBATIM ),
|
|
|
|
|
array( 'mark', $VERBATIM ),
|
|
|
|
|
array( 'time', $VERBATIM ),
|
|
|
|
|
array( 'video', $ESCAPED ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-30 13:34:56 +00:00
|
|
|
function dataRemoveHTMLtags() {
|
|
|
|
|
return array(
|
|
|
|
|
// former testSelfClosingTag
|
|
|
|
|
array(
|
|
|
|
|
'<div>Hello world</div />',
|
|
|
|
|
'<div>Hello world</div>',
|
|
|
|
|
'Self-closing closing div'
|
|
|
|
|
),
|
|
|
|
|
// Make sure special nested HTML5 semantics are not broken
|
|
|
|
|
// http://www.whatwg.org/html/text-level-semantics.html#the-kbd-element
|
|
|
|
|
array(
|
|
|
|
|
'<kbd><kbd>Shift</kbd>+<kbd>F3</kbd></kbd>',
|
|
|
|
|
'<kbd><kbd>Shift</kbd>+<kbd>F3</kbd></kbd>',
|
|
|
|
|
'Nested <kbd>.'
|
|
|
|
|
),
|
|
|
|
|
// http://www.whatwg.org/html/text-level-semantics.html#the-sub-and-sup-elements
|
|
|
|
|
array(
|
|
|
|
|
'<var>x<sub><var>i</var></sub></var>, <var>y<sub><var>i</var></sub></var>',
|
|
|
|
|
'<var>x<sub><var>i</var></sub></var>, <var>y<sub><var>i</var></sub></var>',
|
|
|
|
|
'Nested <var>.'
|
|
|
|
|
),
|
|
|
|
|
// http://www.whatwg.org/html/text-level-semantics.html#the-dfn-element
|
|
|
|
|
array(
|
|
|
|
|
'<dfn><abbr title="Garage Door Opener">GDO</abbr></dfn>',
|
|
|
|
|
'<dfn><abbr title="Garage Door Opener">GDO</abbr></dfn>',
|
|
|
|
|
'<abbr> inside <dfn>',
|
|
|
|
|
),
|
2010-12-14 16:26:35 +00:00
|
|
|
);
|
|
|
|
|
}
|
2012-11-22 10:25:30 +00:00
|
|
|
|
2012-10-30 13:34:56 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider dataRemoveHTMLtags
|
2013-10-21 21:09:13 +00:00
|
|
|
* @covers Sanitizer::removeHTMLtags
|
2012-10-30 13:34:56 +00:00
|
|
|
*/
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testRemoveHTMLtags( $input, $output, $msg = null ) {
|
2012-10-30 13:34:56 +00:00
|
|
|
$GLOBALS['wgUseTidy'] = false;
|
|
|
|
|
$this->assertEquals( $output, Sanitizer::removeHTMLtags( $input ), $msg );
|
|
|
|
|
}
|
2012-11-22 10:25:30 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideTagAttributesToDecode
|
2013-03-11 03:16:28 +00:00
|
|
|
* @covers Sanitizer::decodeTagAttributes
|
2012-11-22 10:25:30 +00:00
|
|
|
*/
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testDecodeTagAttributes( $expected, $attributes, $message = '' ) {
|
2012-11-22 10:25:30 +00:00
|
|
|
$this->assertEquals( $expected,
|
|
|
|
|
Sanitizer::decodeTagAttributes( $attributes ),
|
|
|
|
|
$message
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-22 02:12:37 +00:00
|
|
|
public static function provideTagAttributesToDecode() {
|
2012-11-22 10:25:30 +00:00
|
|
|
return array(
|
|
|
|
|
array( array( 'foo' => 'bar' ), 'foo=bar', 'Unquoted attribute' ),
|
|
|
|
|
array( array( 'foo' => 'bar' ), ' foo = bar ', 'Spaced attribute' ),
|
|
|
|
|
array( array( 'foo' => 'bar' ), 'foo="bar"', 'Double-quoted attribute' ),
|
|
|
|
|
array( array( 'foo' => 'bar' ), 'foo=\'bar\'', 'Single-quoted attribute' ),
|
|
|
|
|
array( array( 'foo' => 'bar', 'baz' => 'foo' ), 'foo=\'bar\' baz="foo"', 'Several attributes' ),
|
|
|
|
|
array( array( 'foo' => 'bar', 'baz' => 'foo' ), 'foo=\'bar\' baz="foo"', 'Several attributes' ),
|
|
|
|
|
array( array( 'foo' => 'bar', 'baz' => 'foo' ), 'foo=\'bar\' baz="foo"', 'Several attributes' ),
|
|
|
|
|
array( array( ':foo' => 'bar' ), ':foo=\'bar\'', 'Leading :' ),
|
|
|
|
|
array( array( '_foo' => 'bar' ), '_foo=\'bar\'', 'Leading _' ),
|
|
|
|
|
array( array( 'foo' => 'bar' ), 'Foo=\'bar\'', 'Leading capital' ),
|
|
|
|
|
array( array( 'foo' => 'BAR' ), 'FOO=BAR', 'Attribute keys are normalized to lowercase' ),
|
|
|
|
|
|
|
|
|
|
# Invalid beginning
|
|
|
|
|
array( array(), '-foo=bar', 'Leading - is forbidden' ),
|
|
|
|
|
array( array(), '.foo=bar', 'Leading . is forbidden' ),
|
|
|
|
|
array( array( 'foo-bar' => 'bar' ), 'foo-bar=bar', 'A - is allowed inside the attribute' ),
|
|
|
|
|
array( array( 'foo-' => 'bar' ), 'foo-=bar', 'A - is allowed inside the attribute' ),
|
|
|
|
|
array( array( 'foo.bar' => 'baz' ), 'foo.bar=baz', 'A . is allowed inside the attribute' ),
|
|
|
|
|
array( array( 'foo.' => 'baz' ), 'foo.=baz', 'A . is allowed as last character' ),
|
|
|
|
|
array( array( 'foo6' => 'baz' ), 'foo6=baz', 'Numbers are allowed' ),
|
|
|
|
|
|
|
|
|
|
# This bit is more relaxed than XML rules, but some extensions use
|
|
|
|
|
# it, like ProofreadPage (see bug 27539)
|
|
|
|
|
array( array( '1foo' => 'baz' ), '1foo=baz', 'Leading numbers are allowed' ),
|
|
|
|
|
array( array(), 'foo$=baz', 'Symbols are not allowed' ),
|
|
|
|
|
array( array(), 'foo@=baz', 'Symbols are not allowed' ),
|
|
|
|
|
array( array(), 'foo~=baz', 'Symbols are not allowed' ),
|
|
|
|
|
array( array( 'foo' => '1[#^`*%w/(' ), 'foo=1[#^`*%w/(', 'All kind of characters are allowed as values' ),
|
|
|
|
|
array( array( 'foo' => '1[#^`*%\'w/(' ), 'foo="1[#^`*%\'w/("', 'Double quotes are allowed if quoted by single quotes' ),
|
|
|
|
|
array( array( 'foo' => '1[#^`*%"w/(' ), 'foo=\'1[#^`*%"w/(\'', 'Single quotes are allowed if quoted by double quotes' ),
|
|
|
|
|
array( array( 'foo' => '&"' ), 'foo=&"', 'Special chars can be provided as entities' ),
|
|
|
|
|
array( array( 'foo' => '&foobar;' ), 'foo=&foobar;', 'Entity-like items are accepted' ),
|
|
|
|
|
);
|
2011-02-19 20:16:54 +00:00
|
|
|
}
|
2011-09-25 04:08:23 +00:00
|
|
|
|
2012-06-29 16:24:20 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideDeprecatedAttributes
|
2013-03-11 03:16:28 +00:00
|
|
|
* @covers Sanitizer::fixTagAttributes
|
2012-06-29 16:24:20 +00:00
|
|
|
*/
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testDeprecatedAttributesUnaltered( $inputAttr, $inputEl, $message = '' ) {
|
2012-11-22 10:23:13 +00:00
|
|
|
$this->assertEquals( " $inputAttr",
|
|
|
|
|
Sanitizer::fixTagAttributes( $inputAttr, $inputEl ),
|
|
|
|
|
$message
|
|
|
|
|
);
|
2012-10-08 10:56:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideDeprecatedAttributes() {
|
2012-11-22 10:23:13 +00:00
|
|
|
/** array( <attribute>, <element>, [message] ) */
|
2012-06-29 16:24:20 +00:00
|
|
|
return array(
|
2012-11-01 18:08:54 +00:00
|
|
|
array( 'clear="left"', 'br' ),
|
|
|
|
|
array( 'clear="all"', 'br' ),
|
|
|
|
|
array( 'width="100"', 'td' ),
|
|
|
|
|
array( 'nowrap="true"', 'td' ),
|
|
|
|
|
array( 'nowrap=""', 'td' ),
|
|
|
|
|
array( 'align="right"', 'td' ),
|
|
|
|
|
array( 'align="center"', 'table' ),
|
|
|
|
|
array( 'align="left"', 'tr' ),
|
|
|
|
|
array( 'align="center"', 'div' ),
|
|
|
|
|
array( 'align="left"', 'h1' ),
|
Put the HTML attribute whitelist closer to HTML5
* Add the global attributes to <bdo> and <q> and add "cite" to <q>. This
is to make these elements actually usable: <bdo> needs a "dir" attribute
to be useful for anything, and the whole point of <q> compared to
hard-coded quotation marks is its support for the "lang" and "cite"
attributes.
* Drop the "align" attribute from <span> because it was never standards-
compliant and does not work in browsers either, unless one constructs
such unlikely things as <span align="center" style="display:block;">.
* Drop the obsolete "char" and "charoff" attributes from <tr>, <td>, <th>.
These have not been implemented in browsers anyway.
* Drop the obsolete presentational attributes "align", "valign" and "width"
from <colgroup>, <col>, <thead>, <tfoot> and <tbody>. These elements are
currently not accepted in wikitext anyway, but removing these attributes
from the whitelist ensures that they are not accidentally enabled in the
future.
* Drop the obsolete presentational attributes "noshade" and "size" from <hr>.
They have been overridden by skin-specific CSS for a long time anyway.
* Allow all global attributes on <br> and <wbr>. Not allowing "dir" and "lang"
on <br> was a restriction in HTML 4.01, presumably copied to <wbr>, that
has been lifted in HTML5. Allowing these may not be particularly useful,
but simplifies the code.
Bug: 55582
Change-Id: I1c3289ef51a449a7837af28d9906701534175896
2013-10-11 20:04:49 +00:00
|
|
|
array( 'align="left"', 'p' ),
|
2012-06-29 16:24:20 +00:00
|
|
|
);
|
2011-09-25 04:08:23 +00:00
|
|
|
}
|
2011-10-24 08:39:58 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideCssCommentsFixtures
|
2013-03-11 03:16:28 +00:00
|
|
|
* @covers Sanitizer::checkCss
|
2011-10-24 08:39:58 +00:00
|
|
|
*/
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testCssCommentsChecking( $expected, $css, $message = '' ) {
|
2012-11-22 10:23:13 +00:00
|
|
|
$this->assertEquals( $expected,
|
2011-10-24 08:39:58 +00:00
|
|
|
Sanitizer::checkCss( $css ),
|
|
|
|
|
$message
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideCssCommentsFixtures() {
|
2011-10-24 08:39:58 +00:00
|
|
|
/** array( <expected>, <css>, [message] ) */
|
|
|
|
|
return array(
|
2013-05-22 08:48:14 +00:00
|
|
|
// Valid comments spanning entire input
|
|
|
|
|
array( '/**/', '/**/' ),
|
|
|
|
|
array( '/* comment */', '/* comment */' ),
|
|
|
|
|
// Weird stuff
|
2011-10-24 08:39:58 +00:00
|
|
|
array( ' ', '/****/' ),
|
2013-05-22 08:48:14 +00:00
|
|
|
array( ' ', '/* /* */' ),
|
|
|
|
|
array( 'display: block;', "display:/* foo */block;" ),
|
|
|
|
|
array( 'display: block;', "display:\\2f\\2a foo \\2a\\2f block;",
|
2011-10-24 08:39:58 +00:00
|
|
|
'Backslash-escaped comments must be stripped (bug 28450)' ),
|
|
|
|
|
array( '', '/* unfinished comment structure',
|
2013-02-14 11:36:35 +00:00
|
|
|
'Remove anything after a comment-start token' ),
|
2011-10-24 08:39:58 +00:00
|
|
|
array( '', "\\2f\\2a unifinished comment'",
|
2013-02-14 11:36:35 +00:00
|
|
|
'Remove anything after a backslash-escaped comment-start token' ),
|
|
|
|
|
array( '/* insecure input */', 'filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'asdf.png\',sizingMethod=\'scale\');' ),
|
|
|
|
|
array( '/* insecure input */', '-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'asdf.png\',sizingMethod=\'scale\')";' ),
|
|
|
|
|
array( '/* insecure input */', 'width: expression(1+1);' ),
|
|
|
|
|
array( '/* insecure input */', 'background-image: image(asdf.png);' ),
|
|
|
|
|
array( '/* insecure input */', 'background-image: -webkit-image(asdf.png);' ),
|
|
|
|
|
array( '/* insecure input */', 'background-image: -moz-image(asdf.png);' ),
|
|
|
|
|
array( '/* insecure input */', 'background-image: image-set("asdf.png" 1x, "asdf.png" 2x);' ),
|
|
|
|
|
array( '/* insecure input */', 'background-image: -webkit-image-set("asdf.png" 1x, "asdf.png" 2x);' ),
|
|
|
|
|
array( '/* insecure input */', 'background-image: -moz-image-set("asdf.png" 1x, "asdf.png" 2x);' ),
|
2011-10-24 08:39:58 +00:00
|
|
|
);
|
|
|
|
|
}
|
2012-10-19 08:57:25 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test for support or lack of support for specific attributes in the attribute whitelist.
|
|
|
|
|
*/
|
2013-03-22 02:12:37 +00:00
|
|
|
public static function provideAttributeSupport() {
|
2012-10-19 08:57:25 +00:00
|
|
|
/** array( <attributes>, <expected>, <message> ) */
|
|
|
|
|
return array(
|
|
|
|
|
array( 'div', ' role="presentation"', ' role="presentation"', 'Support for WAI-ARIA\'s role="presentation".' ),
|
|
|
|
|
array( 'div', ' role="main"', '', "Other WAI-ARIA roles are currently not supported." ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideAttributeSupport
|
2013-10-21 21:09:13 +00:00
|
|
|
* @covers Sanitizer::fixTagAttributes
|
2012-10-19 08:57:25 +00:00
|
|
|
*/
|
2013-10-21 21:09:13 +00:00
|
|
|
public function testAttributeSupport( $tag, $attributes, $expected, $message ) {
|
2012-10-19 08:57:25 +00:00
|
|
|
$this->assertEquals( $expected,
|
|
|
|
|
Sanitizer::fixTagAttributes( $attributes, $tag ),
|
|
|
|
|
$message
|
|
|
|
|
);
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|