2010-12-14 16:26:35 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
2019-04-23 17:09:36 +00:00
|
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
|
/**
|
2017-06-30 00:13:12 +00:00
|
|
|
|
* @group Sanitizer
|
2013-10-21 21:09:13 +00:00
|
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
|
class SanitizerTest extends MediaWikiIntegrationTestCase {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
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
|
|
|
|
|
|
*
|
2014-04-17 18:43:42 +00:00
|
|
|
|
* @param string $tag Name of an HTML5 element (ie: 'video')
|
|
|
|
|
|
* @param bool $escaped Whether sanitizer let the tag in or escape it (ie: '<video>')
|
2012-08-06 10:02:49 +00:00
|
|
|
|
*/
|
2013-10-21 21:09:13 +00:00
|
|
|
|
public function testRemovehtmltagsOnHtml5Tags( $tag, $escaped ) {
|
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",
|
2020-04-01 21:24:13 +00:00
|
|
|
|
Sanitizer::removeHTMLtags( "<$tag></$tag>\n" )
|
2012-08-06 10:02:49 +00:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
2016-02-17 09:09:32 +00:00
|
|
|
|
return [
|
|
|
|
|
|
[ 'data', $VERBATIM ],
|
|
|
|
|
|
[ 'mark', $VERBATIM ],
|
|
|
|
|
|
[ 'time', $VERBATIM ],
|
|
|
|
|
|
[ 'video', $ESCAPED ],
|
|
|
|
|
|
];
|
2012-08-06 10:02:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
|
public function dataRemoveHTMLtags() {
|
2016-02-17 09:09:32 +00:00
|
|
|
|
return [
|
2012-10-30 13:34:56 +00:00
|
|
|
|
// former testSelfClosingTag
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[
|
2012-10-30 13:34:56 +00:00
|
|
|
|
'<div>Hello world</div />',
|
2020-04-02 15:17:41 +00:00
|
|
|
|
'<div>Hello world</div>',
|
2012-10-30 13:34:56 +00:00
|
|
|
|
'Self-closing closing div'
|
2016-02-17 09:09:32 +00:00
|
|
|
|
],
|
2012-10-30 13:34:56 +00:00
|
|
|
|
// Make sure special nested HTML5 semantics are not broken
|
2016-10-13 05:34:26 +00:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/semantics.html#the-kbd-element
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[
|
2012-10-30 13:34:56 +00:00
|
|
|
|
'<kbd><kbd>Shift</kbd>+<kbd>F3</kbd></kbd>',
|
|
|
|
|
|
'<kbd><kbd>Shift</kbd>+<kbd>F3</kbd></kbd>',
|
|
|
|
|
|
'Nested <kbd>.'
|
2016-02-17 09:09:32 +00:00
|
|
|
|
],
|
2016-10-13 05:34:26 +00:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/semantics.html#the-sub-and-sup-elements
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[
|
2012-10-30 13:34:56 +00:00
|
|
|
|
'<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>.'
|
2016-02-17 09:09:32 +00:00
|
|
|
|
],
|
2016-10-13 05:34:26 +00:00
|
|
|
|
// https://html.spec.whatwg.org/multipage/semantics.html#the-dfn-element
|
2016-02-17 09:09:32 +00:00
|
|
|
|
[
|
2012-10-30 13:34:56 +00:00
|
|
|
|
'<dfn><abbr title="Garage Door Opener">GDO</abbr></dfn>',
|
|
|
|
|
|
'<dfn><abbr title="Garage Door Opener">GDO</abbr></dfn>',
|
|
|
|
|
|
'<abbr> inside <dfn>',
|
2016-02-17 09:09:32 +00:00
|
|
|
|
],
|
|
|
|
|
|
];
|
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
|
|
|
|
$this->assertEquals( $output, Sanitizer::removeHTMLtags( $input ), $msg );
|
|
|
|
|
|
}
|
2012-11-22 10:25:30 +00:00
|
|
|
|
|
2012-06-29 16:24:20 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @dataProvider provideDeprecatedAttributes
|
2013-03-11 03:16:28 +00:00
|
|
|
|
* @covers Sanitizer::fixTagAttributes
|
2019-04-23 17:09:36 +00:00
|
|
|
|
* @covers Sanitizer::validateTagAttributes
|
|
|
|
|
|
* @covers Sanitizer::validateAttributes
|
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() {
|
2016-07-10 15:23:29 +00:00
|
|
|
|
/** [ <attribute>, <element>, [message] ] */
|
2016-02-17 09:09:32 +00:00
|
|
|
|
return [
|
|
|
|
|
|
[ 'clear="left"', 'br' ],
|
|
|
|
|
|
[ 'clear="all"', 'br' ],
|
|
|
|
|
|
[ 'width="100"', 'td' ],
|
|
|
|
|
|
[ 'nowrap="true"', 'td' ],
|
|
|
|
|
|
[ 'nowrap=""', 'td' ],
|
|
|
|
|
|
[ 'align="right"', 'td' ],
|
|
|
|
|
|
[ 'align="center"', 'table' ],
|
|
|
|
|
|
[ 'align="left"', 'tr' ],
|
|
|
|
|
|
[ 'align="center"', 'div' ],
|
|
|
|
|
|
[ 'align="left"', 'h1' ],
|
|
|
|
|
|
[ 'align="left"', 'p' ],
|
|
|
|
|
|
];
|
2011-09-25 04:08:23 +00:00
|
|
|
|
}
|
2011-10-24 08:39:58 +00:00
|
|
|
|
|
2019-04-23 17:09:36 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @dataProvider provideValidateTagAttributes
|
|
|
|
|
|
* @covers Sanitizer::validateTagAttributes
|
|
|
|
|
|
* @covers Sanitizer::validateAttributes
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function testValidateTagAttributes( $element, $attribs, $expected ) {
|
|
|
|
|
|
$actual = Sanitizer::validateTagAttributes( $attribs, $element );
|
|
|
|
|
|
$this->assertArrayEquals( $expected, $actual, false, true );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function provideValidateTagAttributes() {
|
|
|
|
|
|
return [
|
|
|
|
|
|
[ 'math',
|
2019-07-08 00:05:57 +00:00
|
|
|
|
[ 'id' => 'foo bar', 'bogus' => 'stripped', 'data-foo' => 'bar' ],
|
|
|
|
|
|
[ 'id' => 'foo_bar', 'data-foo' => 'bar' ],
|
2019-04-23 17:09:36 +00:00
|
|
|
|
],
|
|
|
|
|
|
[ 'meta',
|
2019-07-08 00:05:57 +00:00
|
|
|
|
[ 'id' => 'foo bar', 'itemprop' => 'foo', 'content' => 'bar' ],
|
|
|
|
|
|
[ 'itemprop' => 'foo', 'content' => 'bar' ],
|
2019-04-23 17:09:36 +00:00
|
|
|
|
],
|
2019-10-04 18:26:06 +00:00
|
|
|
|
[ 'div',
|
2020-02-03 23:22:26 +00:00
|
|
|
|
[ 'role' => 'presentation', 'aria-hidden' => 'true' ],
|
|
|
|
|
|
[ 'role' => 'presentation', 'aria-hidden' => 'true' ],
|
|
|
|
|
|
],
|
|
|
|
|
|
[ 'div',
|
|
|
|
|
|
[ 'role' => 'menuitem', 'aria-hidden' => 'false' ],
|
|
|
|
|
|
[ 'role' => 'menuitem', 'aria-hidden' => 'false' ],
|
2019-10-04 18:26:06 +00:00
|
|
|
|
],
|
2019-04-23 17:09:36 +00:00
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-06-10 17:39:06 +00:00
|
|
|
|
* @dataProvider provideAttributesAllowed
|
|
|
|
|
|
* @covers Sanitizer::attributesAllowedInternal
|
2019-04-23 17:09:36 +00:00
|
|
|
|
*/
|
2020-06-10 17:39:06 +00:00
|
|
|
|
public function testAttributesAllowedInternal( $element, $attribs ) {
|
2019-04-23 17:09:36 +00:00
|
|
|
|
$sanitizer = TestingAccessWrapper::newFromClass( Sanitizer::class );
|
2020-06-10 17:39:06 +00:00
|
|
|
|
$actual = $sanitizer->attributesAllowedInternal( $element );
|
2019-04-23 17:09:36 +00:00
|
|
|
|
$this->assertArrayEquals( $attribs, array_keys( $actual ) );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-10 17:39:06 +00:00
|
|
|
|
public function provideAttributesAllowed() {
|
2019-04-23 17:09:36 +00:00
|
|
|
|
/** [ <element>, [ <good attribute 1>, <good attribute 2>, ...] ] */
|
|
|
|
|
|
return [
|
|
|
|
|
|
[ 'math', [ 'class', 'style', 'id', 'title' ] ],
|
|
|
|
|
|
[ 'meta', [ 'itemprop', 'content' ] ],
|
|
|
|
|
|
[ 'link', [ 'itemprop', 'href', 'title' ] ],
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-30 00:13:12 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @dataProvider provideEscapeIdForStuff
|
|
|
|
|
|
*
|
|
|
|
|
|
* @covers Sanitizer::escapeIdForAttribute()
|
|
|
|
|
|
* @covers Sanitizer::escapeIdForLink()
|
|
|
|
|
|
* @covers Sanitizer::escapeIdForExternalInterwiki()
|
|
|
|
|
|
* @covers Sanitizer::escapeIdInternal()
|
2020-02-15 09:24:10 +00:00
|
|
|
|
* @covers Sanitizer::escapeIdInternalUrl()
|
2017-06-30 00:13:12 +00:00
|
|
|
|
*
|
|
|
|
|
|
* @param string $stuff
|
|
|
|
|
|
* @param string[] $config
|
|
|
|
|
|
* @param string $id
|
|
|
|
|
|
* @param string|false $expected
|
|
|
|
|
|
* @param int|null $mode
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function testEscapeIdForStuff( $stuff, array $config, $id, $expected, $mode = null ) {
|
|
|
|
|
|
$func = "Sanitizer::escapeIdFor{$stuff}";
|
|
|
|
|
|
$iwFlavor = array_pop( $config );
|
|
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
|
|
'wgFragmentMode' => $config,
|
|
|
|
|
|
'wgExternalInterwikiFragmentMode' => $iwFlavor,
|
|
|
|
|
|
] );
|
2020-05-29 06:46:30 +00:00
|
|
|
|
$escaped = $func( $id, $mode );
|
2017-06-30 00:13:12 +00:00
|
|
|
|
self::assertEquals( $expected, $escaped );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function provideEscapeIdForStuff() {
|
|
|
|
|
|
// Test inputs and outputs
|
2020-02-15 09:24:10 +00:00
|
|
|
|
$text = 'foo тест_#%!\'()[]:<>&&&amp;%F0';
|
2016-05-02 05:14:45 +00:00
|
|
|
|
$legacyEncoded = 'foo_.D1.82.D0.B5.D1.81.D1.82_.23.25.21.27.28.29.5B.5D:.3C.3E' .
|
2020-02-15 09:24:10 +00:00
|
|
|
|
'.26.26amp.3B.26amp.3Bamp.3B.25F0';
|
|
|
|
|
|
$html5EncodedId = 'foo_тест_#%!\'()[]:<>&&&amp;%F0';
|
|
|
|
|
|
$html5EncodedHref = 'foo_тест_#%!\'()[]:<>&&&amp;%25F0';
|
2017-06-30 00:13:12 +00:00
|
|
|
|
|
|
|
|
|
|
// Settings: last element is $wgExternalInterwikiFragmentMode, the rest is $wgFragmentMode
|
|
|
|
|
|
$legacy = [ 'legacy', 'legacy' ];
|
|
|
|
|
|
$legacyNew = [ 'legacy', 'html5', 'legacy' ];
|
|
|
|
|
|
$newLegacy = [ 'html5', 'legacy', 'legacy' ];
|
|
|
|
|
|
$new = [ 'html5', 'legacy' ];
|
|
|
|
|
|
$allNew = [ 'html5', 'html5' ];
|
|
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
// Pure legacy: how MW worked before 2017
|
|
|
|
|
|
[ 'Attribute', $legacy, $text, $legacyEncoded, Sanitizer::ID_PRIMARY ],
|
|
|
|
|
|
[ 'Attribute', $legacy, $text, false, Sanitizer::ID_FALLBACK ],
|
|
|
|
|
|
[ 'Link', $legacy, $text, $legacyEncoded ],
|
|
|
|
|
|
[ 'ExternalInterwiki', $legacy, $text, $legacyEncoded ],
|
|
|
|
|
|
|
|
|
|
|
|
// Transition to a new world: legacy links with HTML5 fallback
|
|
|
|
|
|
[ 'Attribute', $legacyNew, $text, $legacyEncoded, Sanitizer::ID_PRIMARY ],
|
2020-02-15 09:24:10 +00:00
|
|
|
|
[ 'Attribute', $legacyNew, $text, $html5EncodedId, Sanitizer::ID_FALLBACK ],
|
2017-06-30 00:13:12 +00:00
|
|
|
|
[ 'Link', $legacyNew, $text, $legacyEncoded ],
|
|
|
|
|
|
[ 'ExternalInterwiki', $legacyNew, $text, $legacyEncoded ],
|
|
|
|
|
|
|
|
|
|
|
|
// New world: HTML5 links, legacy fallbacks
|
2020-02-15 09:24:10 +00:00
|
|
|
|
[ 'Attribute', $newLegacy, $text, $html5EncodedId, Sanitizer::ID_PRIMARY ],
|
2017-06-30 00:13:12 +00:00
|
|
|
|
[ 'Attribute', $newLegacy, $text, $legacyEncoded, Sanitizer::ID_FALLBACK ],
|
2020-02-15 09:24:10 +00:00
|
|
|
|
[ 'Link', $newLegacy, $text, $html5EncodedHref ],
|
2017-06-30 00:13:12 +00:00
|
|
|
|
[ 'ExternalInterwiki', $newLegacy, $text, $legacyEncoded ],
|
|
|
|
|
|
|
|
|
|
|
|
// Distant future: no legacy fallbacks, but still linking to leagacy wikis
|
2020-02-15 09:24:10 +00:00
|
|
|
|
[ 'Attribute', $new, $text, $html5EncodedId, Sanitizer::ID_PRIMARY ],
|
2017-06-30 00:13:12 +00:00
|
|
|
|
[ 'Attribute', $new, $text, false, Sanitizer::ID_FALLBACK ],
|
2020-02-15 09:24:10 +00:00
|
|
|
|
[ 'Link', $new, $text, $html5EncodedHref ],
|
2017-06-30 00:13:12 +00:00
|
|
|
|
[ 'ExternalInterwiki', $new, $text, $legacyEncoded ],
|
|
|
|
|
|
|
|
|
|
|
|
// Just before the heat death of universe: external interwikis are also HTML5 \m/
|
2020-02-15 09:24:10 +00:00
|
|
|
|
[ 'Attribute', $allNew, $text, $html5EncodedId, Sanitizer::ID_PRIMARY ],
|
2017-06-30 00:13:12 +00:00
|
|
|
|
[ 'Attribute', $allNew, $text, false, Sanitizer::ID_FALLBACK ],
|
2020-02-15 09:24:10 +00:00
|
|
|
|
[ 'Link', $allNew, $text, $html5EncodedHref ],
|
|
|
|
|
|
[ 'ExternalInterwiki', $allNew, $text, $html5EncodedHref ],
|
2020-02-15 10:09:03 +00:00
|
|
|
|
|
|
|
|
|
|
// Whitespace
|
|
|
|
|
|
[ 'attribute', $allNew, "foo bar", 'foo_bar', Sanitizer::ID_PRIMARY ],
|
|
|
|
|
|
[ 'attribute', $allNew, "foo\fbar", 'foo_bar', Sanitizer::ID_PRIMARY ],
|
|
|
|
|
|
[ 'attribute', $allNew, "foo\nbar", 'foo_bar', Sanitizer::ID_PRIMARY ],
|
|
|
|
|
|
[ 'attribute', $allNew, "foo\tbar", 'foo_bar', Sanitizer::ID_PRIMARY ],
|
|
|
|
|
|
[ 'attribute', $allNew, "foo\rbar", 'foo_bar', Sanitizer::ID_PRIMARY ],
|
2017-06-30 00:13:12 +00:00
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @covers Sanitizer::escapeIdInternal()
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function testInvalidFragmentThrows() {
|
|
|
|
|
|
$this->setMwGlobals( 'wgFragmentMode', [ 'boom!' ] );
|
2019-10-11 22:22:26 +00:00
|
|
|
|
$this->expectException( InvalidArgumentException::class );
|
2017-06-30 00:13:12 +00:00
|
|
|
|
Sanitizer::escapeIdForAttribute( 'This should throw' );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @covers Sanitizer::escapeIdForAttribute()
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function testNoPrimaryFragmentModeThrows() {
|
|
|
|
|
|
$this->setMwGlobals( 'wgFragmentMode', [ 666 => 'html5' ] );
|
2019-10-11 22:22:26 +00:00
|
|
|
|
$this->expectException( UnexpectedValueException::class );
|
2017-06-30 00:13:12 +00:00
|
|
|
|
Sanitizer::escapeIdForAttribute( 'This should throw' );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @covers Sanitizer::escapeIdForLink()
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function testNoPrimaryFragmentModeThrows2() {
|
|
|
|
|
|
$this->setMwGlobals( 'wgFragmentMode', [ 666 => 'html5' ] );
|
2019-10-11 22:22:26 +00:00
|
|
|
|
$this->expectException( UnexpectedValueException::class );
|
2017-06-30 00:13:12 +00:00
|
|
|
|
Sanitizer::escapeIdForLink( 'This should throw' );
|
|
|
|
|
|
}
|
2019-07-08 13:25:31 +00:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Test escapeIdReferenceList for consistency with escapeIdForAttribute
|
|
|
|
|
|
*
|
|
|
|
|
|
* @dataProvider provideEscapeIdReferenceList
|
|
|
|
|
|
* @covers Sanitizer::escapeIdReferenceList
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function testEscapeIdReferenceList( $referenceList, $id1, $id2 ) {
|
2020-08-19 16:21:31 +00:00
|
|
|
|
$this->hideDeprecated( 'Sanitizer::escapeIdReferenceList' );
|
2019-07-08 13:25:31 +00:00
|
|
|
|
$this->assertEquals(
|
|
|
|
|
|
Sanitizer::escapeIdReferenceList( $referenceList ),
|
|
|
|
|
|
Sanitizer::escapeIdForAttribute( $id1 )
|
|
|
|
|
|
. ' '
|
|
|
|
|
|
. Sanitizer::escapeIdForAttribute( $id2 )
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function provideEscapeIdReferenceList() {
|
|
|
|
|
|
/** [ <reference list>, <individual id 1>, <individual id 2> ] */
|
|
|
|
|
|
return [
|
|
|
|
|
|
[ 'foo bar', 'foo', 'bar' ],
|
|
|
|
|
|
[ '#1 #2', '#1', '#2' ],
|
|
|
|
|
|
[ '+1 +2', '+1', '+2' ],
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
|
}
|