| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Parser; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\Parser\Sanitizer; |
| 8 |
use MediaWikiIntegrationTestCase; |
| 9 |
use UnexpectedValueException; |
| 10 |
use Wikimedia\TestingAccessWrapper; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class SanitizerTest extends MediaWikiIntegrationTestCase { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function testInternalRemoveHtmlTagsOnHtml5Tags( $tag, $escaped ) { |
| 25 |
if ( $escaped ) { |
| 26 |
$this->assertEquals( "<$tag>", |
| 27 |
Sanitizer::internalRemoveHtmlTags( "<$tag>" ) |
| 28 |
); |
| 29 |
} else { |
| 30 |
$this->assertEquals( "<$tag></$tag>\n", |
| 31 |
Sanitizer::internalRemoveHtmlTags( "<$tag></$tag>\n" ) |
| 32 |
); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function testRemoveSomeTagsOnHtml5Tags( $tag, $escaped ) { |
| 44 |
if ( $escaped ) { |
| 45 |
$this->assertEquals( "<$tag>", |
| 46 |
Sanitizer::removeSomeTags( "<$tag>" ) |
| 47 |
); |
| 48 |
} else { |
| 49 |
$this->assertEquals( "<$tag></$tag>\n", |
| 50 |
Sanitizer::removeSomeTags( "<$tag></$tag>\n" ) |
| 51 |
); |
| 52 |
$this->assertEquals( "<$tag></$tag>", |
| 53 |
Sanitizer::removeSomeTags( "<$tag>" ) |
| 54 |
); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
public static function provideHtml5Tags() { |
| 59 |
$ESCAPED = true; |
| 60 |
$VERBATIM = false; |
| 61 |
return [ |
| 62 |
[ 'data', $VERBATIM ], |
| 63 |
[ 'mark', $VERBATIM ], |
| 64 |
[ 'time', $VERBATIM ], |
| 65 |
[ 'video', $ESCAPED ], |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
public function dataRemoveHTMLtags() { |
| 70 |
return [ |
| 71 |
|
| 72 |
[ |
| 73 |
'<div>Hello world</div />', |
| 74 |
'<div>Hello world</div>', |
| 75 |
'Self-closing closing div' |
| 76 |
], |
| 77 |
|
| 78 |
|
| 79 |
[ |
| 80 |
'<kbd><kbd>Shift</kbd>+<kbd>F3</kbd></kbd>', |
| 81 |
'<kbd><kbd>Shift</kbd>+<kbd>F3</kbd></kbd>', |
| 82 |
'Nested <kbd>.' |
| 83 |
], |
| 84 |
|
| 85 |
[ |
| 86 |
'<var>x<sub><var>i</var></sub></var>, <var>y<sub><var>i</var></sub></var>', |
| 87 |
'<var>x<sub><var>i</var></sub></var>, <var>y<sub><var>i</var></sub></var>', |
| 88 |
'Nested <var>.' |
| 89 |
], |
| 90 |
|
| 91 |
[ |
| 92 |
'<dfn><abbr title="Garage Door Opener">GDO</abbr></dfn>', |
| 93 |
'<dfn><abbr title="Garage Door Opener">GDO</abbr></dfn>', |
| 94 |
'<abbr> inside <dfn>', |
| 95 |
], |
| 96 |
]; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function testInternalRemoveHTMLtags( $input, $output, $msg = null ) { |
| 104 |
$this->assertEquals( $output, Sanitizer::internalRemoveHtmlTags( $input ), $msg ); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function testRemoveSomeTags( $input, $output, $msg = null ) { |
| 112 |
$this->assertEquals( $output, Sanitizer::removeSomeTags( $input ), $msg ); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public function testDeprecatedAttributesUnaltered( $inputAttr, $inputEl, $message = '' ) { |
| 122 |
$this->assertEquals( " $inputAttr", |
| 123 |
Sanitizer::fixTagAttributes( $inputAttr, $inputEl ), |
| 124 |
$message |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
public static function provideDeprecatedAttributes() { |
| 129 |
|
| 130 |
return [ |
| 131 |
[ 'clear="left"', 'br' ], |
| 132 |
[ 'clear="all"', 'br' ], |
| 133 |
[ 'width="100"', 'td' ], |
| 134 |
[ 'nowrap="true"', 'td' ], |
| 135 |
[ 'nowrap=""', 'td' ], |
| 136 |
[ 'align="right"', 'td' ], |
| 137 |
[ 'align="center"', 'table' ], |
| 138 |
[ 'align="left"', 'tr' ], |
| 139 |
[ 'align="center"', 'div' ], |
| 140 |
[ 'align="left"', 'h1' ], |
| 141 |
[ 'align="left"', 'p' ], |
| 142 |
]; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function testValidateTagAttributes( $element, $attribs, $expected ) { |
| 151 |
$actual = Sanitizer::validateTagAttributes( $attribs, $element ); |
| 152 |
$this->assertArrayEquals( $expected, $actual, false, true ); |
| 153 |
} |
| 154 |
|
| 155 |
public static function provideValidateTagAttributes() { |
| 156 |
return [ |
| 157 |
[ 'math', |
| 158 |
[ 'id' => 'foo bar', 'bogus' => 'stripped', 'data-foo' => 'bar' ], |
| 159 |
[ 'id' => 'foo_bar', 'data-foo' => 'bar' ], |
| 160 |
], |
| 161 |
[ 'meta', |
| 162 |
[ 'id' => 'foo bar', 'itemprop' => 'foo', 'content' => 'bar' ], |
| 163 |
[ 'itemprop' => 'foo', 'content' => 'bar' ], |
| 164 |
], |
| 165 |
[ 'div', |
| 166 |
[ 'role' => 'presentation', 'aria-hidden' => 'true' ], |
| 167 |
[ 'role' => 'presentation', 'aria-hidden' => 'true' ], |
| 168 |
], |
| 169 |
[ 'div', |
| 170 |
[ 'role' => 'menuitem', 'aria-hidden' => 'false' ], |
| 171 |
[ 'role' => 'menuitem', 'aria-hidden' => 'false' ], |
| 172 |
], |
| 173 |
[ 'div', |
| 174 |
[ |
| 175 |
'data-wikitext' => 'wikitext', |
| 176 |
'DATA-WIKITEXT-2' => 'WIKITEXT-2', |
| 177 |
'data-wikitext-ij' => 'wikitext-IJ', |
| 178 |
'data-mw' => 'disallow impersonating parsoid', |
| 179 |
'DATA-mw' => 'disallow impersonating PARSOID', |
| 180 |
'data-mw-extension' => 'disallow impersonating extension', |
| 181 |
'data-:namespaced' => 'disallow namespace', |
| 182 |
'data- invalid' => 'disallow XSS', |
| 183 |
'data-/invalid' => 'disallow XSS', |
| 184 |
'data->invalid' => 'disallow XSS', |
| 185 |
'data-=invalid' => 'disallow XSS', |
| 186 |
], |
| 187 |
[ |
| 188 |
'data-wikitext' => 'wikitext', |
| 189 |
'DATA-WIKITEXT-2' => 'WIKITEXT-2', |
| 190 |
'data-wikitext-ij' => 'wikitext-IJ', |
| 191 |
|
| 192 |
] |
| 193 |
], |
| 194 |
]; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public function testAttributesAllowedInternal( $element, $attribs ) { |
| 202 |
$sanitizer = TestingAccessWrapper::newFromClass( Sanitizer::class ); |
| 203 |
$actual = $sanitizer->attributesAllowedInternal( $element ); |
| 204 |
$this->assertArrayEquals( $attribs, array_keys( $actual ) ); |
| 205 |
} |
| 206 |
|
| 207 |
public static function provideAttributesAllowed() { |
| 208 |
|
| 209 |
return [ |
| 210 |
[ 'math', [ 'class', 'style', 'id', 'title' ] ], |
| 211 |
[ 'meta', [ 'itemprop', 'content' ] ], |
| 212 |
[ 'link', [ 'itemprop', 'href', 'title' ] ], |
| 213 |
]; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public function testEscapeIdForStuff( $stuff, array $config, $id, $expected, $mode = null ) { |
| 232 |
$func = "Sanitizer::escapeIdFor{$stuff}"; |
| 233 |
$iwFlavor = array_pop( $config ); |
| 234 |
$this->overrideConfigValues( [ |
| 235 |
MainConfigNames::FragmentMode => $config, |
| 236 |
MainConfigNames::ExternalInterwikiFragmentMode => $iwFlavor, |
| 237 |
] ); |
| 238 |
$escaped = $func( $id, $mode ); |
| 239 |
self::assertEquals( $expected, $escaped ); |
| 240 |
} |
| 241 |
|
| 242 |
public static function provideEscapeIdForStuff() { |
| 243 |
|
| 244 |
$text = 'foo тест_#%!\'()[]:<>&&&amp;%F0'; |
| 245 |
$legacyEncoded = 'foo_.D1.82.D0.B5.D1.81.D1.82_.23.25.21.27.28.29.5B.5D:.3C.3E' . |
| 246 |
'.26.26amp.3B.26amp.3Bamp.3B.25F0'; |
| 247 |
$html5EncodedId = 'foo_тест_#%!\'()[]:<>&&&amp;%F0'; |
| 248 |
$html5EncodedHref = 'foo_тест_#%!\'()[]:<>&&&amp;%25F0'; |
| 249 |
|
| 250 |
|
| 251 |
$legacy = [ 'legacy', 'legacy' ]; |
| 252 |
$legacyNew = [ 'legacy', 'html5', 'legacy' ]; |
| 253 |
$newLegacy = [ 'html5', 'legacy', 'legacy' ]; |
| 254 |
$new = [ 'html5', 'legacy' ]; |
| 255 |
$allNew = [ 'html5', 'html5' ]; |
| 256 |
|
| 257 |
return [ |
| 258 |
|
| 259 |
[ 'Attribute', $legacy, $text, $legacyEncoded, Sanitizer::ID_PRIMARY ], |
| 260 |
[ 'Attribute', $legacy, $text, false, Sanitizer::ID_FALLBACK ], |
| 261 |
[ 'Link', $legacy, $text, $legacyEncoded ], |
| 262 |
[ 'ExternalInterwiki', $legacy, $text, $legacyEncoded ], |
| 263 |
|
| 264 |
|
| 265 |
[ 'Attribute', $legacyNew, $text, $legacyEncoded, Sanitizer::ID_PRIMARY ], |
| 266 |
[ 'Attribute', $legacyNew, $text, $html5EncodedId, Sanitizer::ID_FALLBACK ], |
| 267 |
[ 'Link', $legacyNew, $text, $legacyEncoded ], |
| 268 |
[ 'ExternalInterwiki', $legacyNew, $text, $legacyEncoded ], |
| 269 |
|
| 270 |
|
| 271 |
[ 'Attribute', $newLegacy, $text, $html5EncodedId, Sanitizer::ID_PRIMARY ], |
| 272 |
[ 'Attribute', $newLegacy, $text, $legacyEncoded, Sanitizer::ID_FALLBACK ], |
| 273 |
[ 'Link', $newLegacy, $text, $html5EncodedHref ], |
| 274 |
[ 'ExternalInterwiki', $newLegacy, $text, $legacyEncoded ], |
| 275 |
|
| 276 |
|
| 277 |
[ 'Attribute', $new, $text, $html5EncodedId, Sanitizer::ID_PRIMARY ], |
| 278 |
[ 'Attribute', $new, $text, false, Sanitizer::ID_FALLBACK ], |
| 279 |
[ 'Link', $new, $text, $html5EncodedHref ], |
| 280 |
[ 'ExternalInterwiki', $new, $text, $legacyEncoded ], |
| 281 |
|
| 282 |
|
| 283 |
[ 'Attribute', $allNew, $text, $html5EncodedId, Sanitizer::ID_PRIMARY ], |
| 284 |
[ 'Attribute', $allNew, $text, false, Sanitizer::ID_FALLBACK ], |
| 285 |
[ 'Link', $allNew, $text, $html5EncodedHref ], |
| 286 |
[ 'ExternalInterwiki', $allNew, $text, $html5EncodedHref ], |
| 287 |
|
| 288 |
|
| 289 |
[ 'attribute', $allNew, "foo bar", 'foo_bar', Sanitizer::ID_PRIMARY ], |
| 290 |
[ 'attribute', $allNew, "foo\fbar", 'foo_bar', Sanitizer::ID_PRIMARY ], |
| 291 |
[ 'attribute', $allNew, "foo\nbar", 'foo_bar', Sanitizer::ID_PRIMARY ], |
| 292 |
[ 'attribute', $allNew, "foo\tbar", 'foo_bar', Sanitizer::ID_PRIMARY ], |
| 293 |
[ 'attribute', $allNew, "foo\rbar", 'foo_bar', Sanitizer::ID_PRIMARY ], |
| 294 |
]; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public function testInvalidFragmentThrows() { |
| 301 |
$this->overrideConfigValue( MainConfigNames::FragmentMode, [ 'boom!' ] ); |
| 302 |
$this->expectException( InvalidArgumentException::class ); |
| 303 |
Sanitizer::escapeIdForAttribute( 'This should throw' ); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public function testNoPrimaryFragmentModeThrows() { |
| 310 |
$this->overrideConfigValue( MainConfigNames::FragmentMode, [ 666 => 'html5' ] ); |
| 311 |
$this->expectException( UnexpectedValueException::class ); |
| 312 |
Sanitizer::escapeIdForAttribute( 'This should throw' ); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
public function testNoPrimaryFragmentModeThrows2() { |
| 319 |
$this->overrideConfigValue( MainConfigNames::FragmentMode, [ 666 => 'html5' ] ); |
| 320 |
$this->expectException( UnexpectedValueException::class ); |
| 321 |
Sanitizer::escapeIdForLink( 'This should throw' ); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public function testEscapeIdReferenceListInternal( $referenceList, $id1, $id2 ) { |
| 331 |
$sanitizer = TestingAccessWrapper::newFromClass( Sanitizer::class ); |
| 332 |
$actual = $sanitizer->escapeIdReferenceListInternal( $referenceList ); |
| 333 |
|
| 334 |
$this->assertEquals( |
| 335 |
$actual, |
| 336 |
Sanitizer::escapeIdForAttribute( $id1 ) |
| 337 |
. ' ' |
| 338 |
. Sanitizer::escapeIdForAttribute( $id2 ) |
| 339 |
); |
| 340 |
} |
| 341 |
|
| 342 |
public static function provideEscapeIdReferenceListInternal() { |
| 343 |
|
| 344 |
return [ |
| 345 |
[ 'foo bar', 'foo', 'bar' ], |
| 346 |
[ '#1 #2', '#1', '#2' ], |
| 347 |
[ '+1 +2', '+1', '+2' ], |
| 348 |
]; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
public function testCleanUrl( string $input, string $output ) { |
| 358 |
$this->assertEquals( $output, Sanitizer::cleanUrl( $input ) ); |
| 359 |
} |
| 360 |
|
| 361 |
public static function provideCleanUrl() { |
| 362 |
return [ |
| 363 |
[ 'http://www.example.com/file.txt', 'http://www.example.com/file.txt' ], |
| 364 |
[ |
| 365 |
"https://www.exa\u{00AD}\u{200B}\u{2060}\u{FEFF}" . |
| 366 |
"\u{034F}\u{180B}\u{180C}\u{180D}\u{200C}\u{200D}" . |
| 367 |
"\u{FE00}\u{FE08}\u{FE0F}mple.com", |
| 368 |
'https://www.example.com' |
| 369 |
], |
| 370 |
]; |
| 371 |
} |
| 372 |
|
| 373 |
} |