| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
l |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
namespace MediaWiki\Parser; |
| 28 |
|
| 29 |
use InvalidArgumentException; |
| 30 |
use LogicException; |
| 31 |
use MediaWiki\HookContainer\HookRunner; |
| 32 |
use MediaWiki\MediaWikiServices; |
| 33 |
use MediaWiki\Tidy\RemexCompatFormatter; |
| 34 |
use StringUtils; |
| 35 |
use UnexpectedValueException; |
| 36 |
use Wikimedia\RemexHtml\HTMLData; |
| 37 |
use Wikimedia\RemexHtml\Serializer\Serializer as RemexSerializer; |
| 38 |
use Wikimedia\RemexHtml\Tokenizer\Tokenizer as RemexTokenizer; |
| 39 |
use Wikimedia\RemexHtml\TreeBuilder\Dispatcher as RemexDispatcher; |
| 40 |
use Wikimedia\RemexHtml\TreeBuilder\TreeBuilder as RemexTreeBuilder; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
class Sanitizer { |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
private const CHAR_REFS_REGEX = |
| 54 |
'/&([A-Za-z0-9\x80-\xff]+;) |
| 55 |
|&\#([0-9]+); |
| 56 |
|&\#[xX]([0-9A-Fa-f]+); |
| 57 |
|&/x'; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private const ELEMENT_BITS_REGEX = '!^(/?)([A-Za-z][^\t\n\v />\0]*+)([^>]*?)(/?>)([^<]*)$!'; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
private const EVIL_URI_PATTERN = '!(^|\s|\*/\s*)(javascript|vbscript)([^\w]|$)!i'; |
| 75 |
private const XMLNS_ATTRIBUTE_PATTERN = "/^xmlns:[:A-Z_a-z-.0-9]+$/"; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public const ID_PRIMARY = 0; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public const ID_FALLBACK = 1; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
private const MW_ENTITY_ALIASES = [ |
| 97 |
'רלמ;' => 'rlm;', |
| 98 |
'رلم;' => 'rlm;', |
| 99 |
]; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
private static ?string $attribsRegex = null; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
private static function getAttribsRegex(): string { |
| 112 |
if ( self::$attribsRegex === null ) { |
| 113 |
$spaceChars = '\x09\x0a\x0c\x0d\x20'; |
| 114 |
$space = "[{$spaceChars}]"; |
| 115 |
$attrib = "[^{$spaceChars}\/>=]"; |
| 116 |
$attribFirst = "(?:{$attrib}|=)"; |
| 117 |
self::$attribsRegex = |
| 118 |
"/({$attribFirst}{$attrib}*) |
| 119 |
($space*=$space* |
| 120 |
(?: |
| 121 |
# The attribute value: quoted or alone |
| 122 |
\"([^\"]*)(?:\"|\$) |
| 123 |
| '([^']*)(?:'|\$) |
| 124 |
| (((?!$space|>).)*) |
| 125 |
) |
| 126 |
)?/sxu"; |
| 127 |
} |
| 128 |
return self::$attribsRegex; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
private static ?string $attribNameRegex = null; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
private static function getAttribNameRegex(): string { |
| 140 |
if ( self::$attribNameRegex === null ) { |
| 141 |
$attribFirst = "[:_\p{L}\p{N}]"; |
| 142 |
$attrib = "[:_\.\-\p{L}\p{N}]"; |
| 143 |
self::$attribNameRegex = "/^({$attribFirst}{$attrib}*)$/sxu"; |
| 144 |
} |
| 145 |
return self::$attribNameRegex; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public static function getRecognizedTagData( array $extratags = [], array $removetags = [] ): array { |
| 156 |
static $commonCase, $staticInitialised = false; |
| 157 |
$isCommonCase = ( $extratags === [] && $removetags === [] ); |
| 158 |
if ( $staticInitialised && $isCommonCase && $commonCase ) { |
| 159 |
return $commonCase; |
| 160 |
} |
| 161 |
|
| 162 |
static $htmlpairsStatic, $htmlsingle, $htmlsingleonly, $htmlnest, $tabletags, |
| 163 |
$htmllist, $listtags, $htmlsingleallowed, $htmlelementsStatic; |
| 164 |
|
| 165 |
if ( !$staticInitialised ) { |
| 166 |
$htmlpairsStatic = [ |
| 167 |
'b', 'bdi', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1', |
| 168 |
'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's', |
| 169 |
'strike', 'strong', 'tt', 'var', 'div', 'center', |
| 170 |
'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre', |
| 171 |
'ruby', 'rb', 'rp', 'rt', 'rtc', 'p', 'span', 'abbr', 'dfn', |
| 172 |
'kbd', 'samp', 'data', 'time', 'mark' |
| 173 |
]; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
$htmlsingle = [ |
| 178 |
'br', 'wbr', 'hr', 'li', 'dt', 'dd', 'meta', 'link' |
| 179 |
]; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
$htmlsingleonly = [ |
| 187 |
'br', 'wbr', 'hr', 'meta', 'link' |
| 188 |
]; |
| 189 |
|
| 190 |
$htmlnest = [ |
| 191 |
'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul', |
| 192 |
'li', 'dl', 'dt', 'dd', 'font', 'big', 'small', 'sub', 'sup', 'span', |
| 193 |
'var', 'kbd', 'samp', 'em', 'strong', 'q', 'ruby', 'bdo' |
| 194 |
]; |
| 195 |
$tabletags = [ |
| 196 |
'td', 'th', 'tr', |
| 197 |
]; |
| 198 |
$htmllist = [ |
| 199 |
'ul', 'ol', |
| 200 |
]; |
| 201 |
$listtags = [ |
| 202 |
'li', |
| 203 |
]; |
| 204 |
|
| 205 |
$htmlsingleallowed = array_unique( array_merge( $htmlsingle, $tabletags ) ); |
| 206 |
$htmlelementsStatic = array_unique( array_merge( $htmlsingle, $htmlpairsStatic, $htmlnest ) ); |
| 207 |
|
| 208 |
|
| 209 |
$vars = [ 'htmlpairsStatic', 'htmlsingle', 'htmlsingleonly', 'htmlnest', 'tabletags', |
| 210 |
'htmllist', 'listtags', 'htmlsingleallowed', 'htmlelementsStatic' ]; |
| 211 |
foreach ( $vars as $var ) { |
| 212 |
$$var = array_fill_keys( $$var, true ); |
| 213 |
} |
| 214 |
$staticInitialised = true; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
$extratags = array_fill_keys( $extratags, true ); |
| 219 |
$removetags = array_fill_keys( $removetags, true ); |
| 220 |
$htmlpairs = array_merge( $extratags, $htmlpairsStatic ); |
| 221 |
$htmlelements = array_diff_key( array_merge( $extratags, $htmlelementsStatic ), $removetags ); |
| 222 |
|
| 223 |
$result = [ |
| 224 |
'htmlpairs' => $htmlpairs, |
| 225 |
'htmlsingle' => $htmlsingle, |
| 226 |
'htmlsingleonly' => $htmlsingleonly, |
| 227 |
'htmlnest' => $htmlnest, |
| 228 |
'tabletags' => $tabletags, |
| 229 |
'htmllist' => $htmllist, |
| 230 |
'listtags' => $listtags, |
| 231 |
'htmlsingleallowed' => $htmlsingleallowed, |
| 232 |
'htmlelements' => $htmlelements, |
| 233 |
]; |
| 234 |
if ( $isCommonCase ) { |
| 235 |
$commonCase = $result; |
| 236 |
} |
| 237 |
return $result; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public static function internalRemoveHtmlTags( string $text, ?callable $processCallback = null, |
| 269 |
$args = [], array $extratags = [], array $removetags = [] |
| 270 |
): string { |
| 271 |
$tagData = self::getRecognizedTagData( $extratags, $removetags ); |
| 272 |
$htmlsingle = $tagData['htmlsingle']; |
| 273 |
$htmlsingleonly = $tagData['htmlsingleonly']; |
| 274 |
$htmlelements = $tagData['htmlelements']; |
| 275 |
|
| 276 |
|
| 277 |
$text = self::removeHTMLcomments( $text ); |
| 278 |
$bits = explode( '<', $text ); |
| 279 |
$text = str_replace( '>', '>', array_shift( $bits ) ); |
| 280 |
|
| 281 |
|
| 282 |
foreach ( $bits as $x ) { |
| 283 |
if ( preg_match( self::ELEMENT_BITS_REGEX, $x, $regs ) ) { |
| 284 |
[ , $slash, $t, $params, $brace, $rest ] = $regs; |
| 285 |
|
| 286 |
$badtag = false; |
| 287 |
$t = strtolower( $t ); |
| 288 |
if ( isset( $htmlelements[$t] ) ) { |
| 289 |
if ( is_callable( $processCallback ) ) { |
| 290 |
call_user_func_array( $processCallback, [ &$params, $args ] ); |
| 291 |
} |
| 292 |
|
| 293 |
if ( $brace == '/>' && !( isset( $htmlsingle[$t] ) || isset( $htmlsingleonly[$t] ) ) ) { |
| 294 |
|
| 295 |
|
| 296 |
$brace = '>'; |
| 297 |
} |
| 298 |
if ( !self::validateTag( $params, $t ) ) { |
| 299 |
$badtag = true; |
| 300 |
} |
| 301 |
|
| 302 |
$newparams = self::fixTagAttributes( $params, $t ); |
| 303 |
if ( !$badtag ) { |
| 304 |
if ( $brace === '/>' && !isset( $htmlsingleonly[$t] ) ) { |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
$brace = "></$t>"; |
| 309 |
} |
| 310 |
|
| 311 |
$rest = str_replace( '>', '>', $rest ); |
| 312 |
$text .= "<$slash$t$newparams$brace$rest"; |
| 313 |
continue; |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
$text .= '<' . str_replace( '>', '>', $x ); |
| 318 |
} |
| 319 |
return $text; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
public static function removeSomeTags( |
| 344 |
string $text, array $options = [] |
| 345 |
): string { |
| 346 |
$extraTags = $options['extraTags'] ?? []; |
| 347 |
$removeTags = $options['removeTags'] ?? []; |
| 348 |
|
| 349 |
$attrCallback = $options['attrCallback'] ?? null; |
| 350 |
$attrCallbackArgs = $options['attrCallbackArgs'] ?? []; |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
$text = self::normalizeCharReferences( $text ); |
| 355 |
|
| 356 |
$tagData = self::getRecognizedTagData( $extraTags, $removeTags ); |
| 357 |
|
| 358 |
$formatter = new RemexCompatFormatter; |
| 359 |
$serializer = new RemexSerializer( $formatter ); |
| 360 |
$treeBuilder = new RemexTreeBuilder( $serializer, [ |
| 361 |
'ignoreErrors' => true, |
| 362 |
'ignoreNulls' => true, |
| 363 |
] ); |
| 364 |
$dispatcher = new RemexDispatcher( $treeBuilder ); |
| 365 |
$tokenHandler = $dispatcher; |
| 366 |
$remover = new RemexRemoveTagHandler( |
| 367 |
$tokenHandler, $text, $tagData, |
| 368 |
$attrCallback, $attrCallbackArgs |
| 369 |
); |
| 370 |
$tokenizer = new RemexTokenizer( $remover, $text, [ |
| 371 |
'ignoreErrors' => true, |
| 372 |
|
| 373 |
'ignoreNulls' => true, |
| 374 |
'skipPreprocess' => true, |
| 375 |
] ); |
| 376 |
$tokenizer->execute( [ |
| 377 |
'fragmentNamespace' => HTMLData::NS_HTML, |
| 378 |
'fragmentName' => 'body', |
| 379 |
] ); |
| 380 |
return $serializer->getResult(); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
public static function removeHTMLcomments( string $text ): string { |
| 390 |
|
| 391 |
while ( ( $start = strpos( $text, '<!--' ) ) !== false ) { |
| 392 |
$end = strpos( $text, '-->', $start + 4 ); |
| 393 |
if ( $end === false ) { |
| 394 |
|
| 395 |
break; |
| 396 |
} |
| 397 |
|
| 398 |
$end += 3; |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
$spaceStart = max( $start - 1, 0 ); |
| 403 |
$spaceLen = $end - $spaceStart; |
| 404 |
while ( substr( $text, $spaceStart, 1 ) === ' ' && $spaceStart > 0 ) { |
| 405 |
$spaceStart--; |
| 406 |
$spaceLen++; |
| 407 |
} |
| 408 |
while ( substr( $text, $spaceStart + $spaceLen, 1 ) === ' ' ) { |
| 409 |
$spaceLen++; |
| 410 |
} |
| 411 |
if ( substr( $text, $spaceStart, 1 ) === "\n" |
| 412 |
&& substr( $text, $spaceStart + $spaceLen, 1 ) === "\n" ) { |
| 413 |
|
| 414 |
|
| 415 |
$text = substr_replace( $text, "\n", $spaceStart, $spaceLen + 1 ); |
| 416 |
} else { |
| 417 |
|
| 418 |
$text = substr_replace( $text, '', $start, $end - $start ); |
| 419 |
} |
| 420 |
} |
| 421 |
return $text; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
private static function validateTag( string $params, string $element ): bool { |
| 435 |
$params = self::decodeTagAttributes( $params ); |
| 436 |
|
| 437 |
if ( $element == 'meta' || $element == 'link' ) { |
| 438 |
if ( !isset( $params['itemprop'] ) ) { |
| 439 |
|
| 440 |
return false; |
| 441 |
} |
| 442 |
if ( $element == 'meta' && !isset( $params['content'] ) ) { |
| 443 |
|
| 444 |
return false; |
| 445 |
} |
| 446 |
if ( $element == 'link' && !isset( $params['href'] ) ) { |
| 447 |
|
| 448 |
return false; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
return true; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
public static function validateTagAttributes( array $attribs, string $element ): array { |
| 467 |
return self::validateAttributes( $attribs, |
| 468 |
self::attributesAllowedInternal( $element ) ); |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
public static function validateAttributes( array $attribs, array $allowed ): array { |
| 490 |
if ( isset( $allowed[0] ) ) { |
| 491 |
|
| 492 |
|
| 493 |
wfDeprecated( __METHOD__ . ' with sequential array', '1.35' ); |
| 494 |
$allowed = array_fill_keys( $allowed, true ); |
| 495 |
} |
| 496 |
$validProtocols = MediaWikiServices::getInstance()->getUrlUtils()->validProtocols(); |
| 497 |
$hrefExp = '/^(' . $validProtocols . ')[^\s]+$/'; |
| 498 |
|
| 499 |
$out = []; |
| 500 |
foreach ( $attribs as $attribute => $value ) { |
| 501 |
|
| 502 |
if ( preg_match( self::XMLNS_ATTRIBUTE_PATTERN, $attribute ) ) { |
| 503 |
if ( !preg_match( self::EVIL_URI_PATTERN, $value ) ) { |
| 504 |
$out[$attribute] = $value; |
| 505 |
} |
| 506 |
|
| 507 |
continue; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
if ( ( |
| 521 |
!preg_match( '|^data-[^:= \t\r\n/>\0__]*$|i', $attribute ) && |
| 522 |
!array_key_exists( $attribute, $allowed ) |
| 523 |
) || self::isReservedDataAttribute( $attribute ) ) { |
| 524 |
continue; |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
if ( $attribute == 'style' ) { |
| 530 |
$value = self::checkCss( $value ); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
if ( $attribute === 'id' ) { |
| 535 |
$value = self::escapeIdForAttribute( $value, self::ID_PRIMARY ); |
| 536 |
if ( $value === false || $value === '' ) { |
| 537 |
continue; |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
if ( $attribute === 'aria-describedby' |
| 543 |
|| $attribute === 'aria-flowto' |
| 544 |
|| $attribute === 'aria-labelledby' |
| 545 |
|| $attribute === 'aria-owns' |
| 546 |
) { |
| 547 |
$value = self::escapeIdReferenceListInternal( $value ); |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
if ( $attribute === 'rel' || $attribute === 'rev' |
| 552 |
|
| 553 |
|| $attribute === 'about' || $attribute === 'property' |
| 554 |
|| $attribute === 'resource' || $attribute === 'datatype' |
| 555 |
|| $attribute === 'typeof' |
| 556 |
|
| 557 |
|| $attribute === 'itemid' || $attribute === 'itemprop' |
| 558 |
|| $attribute === 'itemref' || $attribute === 'itemscope' |
| 559 |
|| $attribute === 'itemtype' |
| 560 |
) { |
| 561 |
|
| 562 |
if ( preg_match( self::EVIL_URI_PATTERN, $value ) ) { |
| 563 |
continue; |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
if ( $attribute === 'href' || $attribute === 'src' || $attribute === 'poster' ) { |
| 570 |
if ( !preg_match( $hrefExp, $value ) ) { |
| 571 |
continue; |
| 572 |
|
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
if ( $attribute === 'tabindex' && $value !== '0' ) { |
| 577 |
|
| 578 |
continue; |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
$out[$attribute] = $value; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
if ( !array_key_exists( 'itemscope', $out ) ) { |
| 588 |
unset( $out['itemtype'] ); |
| 589 |
unset( $out['itemid'] ); |
| 590 |
unset( $out['itemref'] ); |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
return $out; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
public static function isReservedDataAttribute( string $attr ): bool { |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
return (bool)preg_match( '/^data-(ooui|mw|parsoid)/i', $attr ); |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
public static function mergeAttributes( array $a, array $b ): array { |
| 623 |
$out = array_merge( $a, $b ); |
| 624 |
if ( isset( $a['class'] ) && isset( $b['class'] ) |
| 625 |
&& is_string( $a['class'] ) && is_string( $b['class'] ) |
| 626 |
&& $a['class'] !== $b['class'] |
| 627 |
) { |
| 628 |
$classes = preg_split( '/\s+/', "{$a['class']} {$b['class']}", |
| 629 |
-1, PREG_SPLIT_NO_EMPTY ); |
| 630 |
$out['class'] = implode( ' ', array_unique( $classes ) ); |
| 631 |
} |
| 632 |
return $out; |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
public static function normalizeCss( string $value ): string { |
| 644 |
|
| 645 |
$value = self::decodeCharReferences( $value ); |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
static $decodeRegex; |
| 657 |
if ( !$decodeRegex ) { |
| 658 |
$space = '[\\x20\\t\\r\\n\\f]'; |
| 659 |
$nl = '(?:\\n|\\r\\n|\\r|\\f)'; |
| 660 |
$backslash = '\\\\'; |
| 661 |
$decodeRegex = "/ $backslash |
| 662 |
(?: |
| 663 |
($nl) | # 1. Line continuation |
| 664 |
([0-9A-Fa-f]{1,6})$space? | # 2. character number |
| 665 |
(.) | # 3. backslash cancelling special meaning |
| 666 |
() | # 4. backslash at end of string |
| 667 |
)/xu"; |
| 668 |
} |
| 669 |
$value = preg_replace_callback( $decodeRegex, |
| 670 |
[ __CLASS__, 'cssDecodeCallback' ], $value ); |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
if ( !preg_match( '! ^ \s* /\* [^*\\/]* \*/ \s* $ !x', $value ) ) { |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
$value = StringUtils::delimiterReplace( '/*', '*/', ' ', $value ); |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
$commentPos = strpos( $value, '/*' ); |
| 687 |
if ( $commentPos !== false ) { |
| 688 |
$value = substr( $value, 0, $commentPos ); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
return $value; |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
public static function checkCss( $value ) { |
| 716 |
$value = self::normalizeCss( $value ); |
| 717 |
|
| 718 |
|
| 719 |
if ( preg_match( '/[\000-\010\013\016-\037\177]/', $value ) || |
| 720 |
strpos( $value, \UtfNormal\Constants::UTF8_REPLACEMENT ) !== false ) { |
| 721 |
return '/* invalid control char */'; |
| 722 |
} elseif ( preg_match( |
| 723 |
'! expression |
| 724 |
| accelerator\s*: |
| 725 |
| -o-link\s*: |
| 726 |
| -o-link-source\s*: |
| 727 |
| -o-replace\s*: |
| 728 |
| url\s*\( |
| 729 |
| src\s*\( |
| 730 |
| image\s*\( |
| 731 |
| image-set\s*\( |
| 732 |
| attr\s*\([^)]+[\s,]+url |
| 733 |
!ix', $value ) ) { |
| 734 |
return '/* insecure input */'; |
| 735 |
} |
| 736 |
return $value; |
| 737 |
} |
| 738 |
|
| 739 |
private static function cssDecodeCallback( array $matches ): string { |
| 740 |
if ( $matches[1] !== '' ) { |
| 741 |
|
| 742 |
return ''; |
| 743 |
} elseif ( $matches[2] !== '' ) { |
| 744 |
|
| 745 |
|
| 746 |
$char = \UtfNormal\Utils::codepointToUtf8( hexdec( $matches[2] ) ); |
| 747 |
} elseif ( $matches[3] !== '' ) { |
| 748 |
$char = $matches[3]; |
| 749 |
} else { |
| 750 |
$char = '\\'; |
| 751 |
} |
| 752 |
if ( $char == "\n" || $char == '"' || $char == "'" || $char == '\\' ) { |
| 753 |
|
| 754 |
|
| 755 |
return '\\' . dechex( ord( $char ) ) . ' '; |
| 756 |
} else { |
| 757 |
|
| 758 |
return $char; |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
public static function fixTagAttributes( string $text, string $element, bool $sorted = false ): string { |
| 784 |
if ( trim( $text ) == '' ) { |
| 785 |
return ''; |
| 786 |
} |
| 787 |
|
| 788 |
$decoded = self::decodeTagAttributes( $text ); |
| 789 |
$stripped = self::validateTagAttributes( $decoded, $element ); |
| 790 |
|
| 791 |
if ( $sorted ) { |
| 792 |
ksort( $stripped ); |
| 793 |
} |
| 794 |
|
| 795 |
return self::safeEncodeTagAttributes( $stripped ); |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
public static function encodeAttribute( string $text ): string { |
| 806 |
$encValue = htmlspecialchars( $text, ENT_QUOTES ); |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
$encValue = strtr( $encValue, [ |
| 812 |
"\n" => ' ', |
| 813 |
"\r" => ' ', |
| 814 |
"\t" => '	', |
| 815 |
] ); |
| 816 |
|
| 817 |
return $encValue; |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
public static function armorFrenchSpaces( string $text, string $space = ' ' ): string { |
| 829 |
|
| 830 |
$space = preg_replace( '#(?<!\\\\)(\\$|\\\\)#', '\\\\$1', $space ); |
| 831 |
$fixtags = [ |
| 832 |
|
| 833 |
|
| 834 |
'/ (?=[?:;!%»›](?!\w))/u' => "$space", |
| 835 |
|
| 836 |
'/([«‹]) /u' => "\\1$space", |
| 837 |
]; |
| 838 |
return preg_replace( array_keys( $fixtags ), array_values( $fixtags ), $text ); |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
public static function safeEncodeAttribute( string $text ): string { |
| 850 |
$encValue = self::encodeAttribute( $text ); |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
$encValue = strtr( $encValue, [ |
| 855 |
|
| 856 |
|
| 857 |
'<' => '<', |
| 858 |
'>' => '>', |
| 859 |
'"' => '"', |
| 860 |
'{' => '{', |
| 861 |
'}' => '}', |
| 862 |
'[' => '[', |
| 863 |
']' => ']', |
| 864 |
"''" => '''', |
| 865 |
'ISBN' => 'ISBN', |
| 866 |
'RFC' => 'RFC', |
| 867 |
'PMID' => 'PMID', |
| 868 |
'|' => '|', |
| 869 |
'_' => '_', |
| 870 |
'_' => '_'ds |
| 871 |
] ); |
| 872 |
|
| 873 |
|
| 874 |
$validProtocols = MediaWikiServices::getInstance()->getUrlUtils()->validProtocols(); |
| 875 |
$encValue = preg_replace_callback( |
| 876 |
'/((?i)' . $validProtocols . ')/', |
| 877 |
static function ( $matches ) { |
| 878 |
return str_replace( ':', ':', $matches[1] ); |
| 879 |
}, |
| 880 |
$encValue ); |
| 881 |
return $encValue; |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
public static function escapeIdForAttribute( string $id, int $mode = self::ID_PRIMARY ) { |
| 900 |
global $wgFragmentMode; |
| 901 |
|
| 902 |
if ( !isset( $wgFragmentMode[$mode] ) ) { |
| 903 |
if ( $mode === self::ID_PRIMARY ) { |
| 904 |
throw new UnexpectedValueException( '$wgFragmentMode is configured with no primary mode' ); |
| 905 |
} |
| 906 |
return false; |
| 907 |
} |
| 908 |
|
| 909 |
$internalMode = $wgFragmentMode[$mode]; |
| 910 |
|
| 911 |
return self::escapeIdInternal( $id, $internalMode ); |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
public static function escapeIdForLink( string $id ): string { |
| 927 |
global $wgFragmentMode; |
| 928 |
|
| 929 |
if ( !isset( $wgFragmentMode[self::ID_PRIMARY] ) ) { |
| 930 |
throw new UnexpectedValueException( '$wgFragmentMode is configured with no primary mode' ); |
| 931 |
} |
| 932 |
|
| 933 |
$mode = $wgFragmentMode[self::ID_PRIMARY]; |
| 934 |
|
| 935 |
$id = self::escapeIdInternalUrl( $id, $mode ); |
| 936 |
|
| 937 |
return $id; |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
public static function escapeIdForExternalInterwiki( string $id ): string { |
| 950 |
global $wgExternalInterwikiFragmentMode; |
| 951 |
|
| 952 |
$id = self::escapeIdInternalUrl( $id, $wgExternalInterwikiFragmentMode ); |
| 953 |
|
| 954 |
return $id; |
| 955 |
} |
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
private static function escapeIdInternalUrl( string $id, string $mode ): string { |
| 967 |
$id = self::escapeIdInternal( $id, $mode ); |
| 968 |
if ( $mode === 'html5' ) { |
| 969 |
$id = preg_replace( '/%([a-fA-F0-9]{2})/', '%25$1', $id ); |
| 970 |
} |
| 971 |
return $id; |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
private static function escapeIdInternal( string $id, string $mode ): string { |
| 982 |
|
| 983 |
|
| 984 |
$id = mb_substr( $id, 0, 1024 ); |
| 985 |
|
| 986 |
switch ( $mode ) { |
| 987 |
case 'html5': |
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
$id = str_replace( [ "\t", "\n", "\f", "\r", " " ], '_', $id ); |
| 993 |
break; |
| 994 |
case 'legacy': |
| 995 |
|
| 996 |
static $replace = [ |
| 997 |
'%3A' => ':', |
| 998 |
'%' => '.' |
| 999 |
]; |
| 1000 |
|
| 1001 |
$id = urlencode( str_replace( ' ', '_', $id ) ); |
| 1002 |
$id = strtr( $id, $replace ); |
| 1003 |
break; |
| 1004 |
default: |
| 1005 |
throw new InvalidArgumentException( "Invalid mode '$mode' passed to '" . __METHOD__ ); |
| 1006 |
} |
| 1007 |
|
| 1008 |
return $id; |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
private static function escapeIdReferenceListInternal( string $referenceString ): string { |
| 1019 |
|
| 1020 |
$references = preg_split( '/\s+/', "{$referenceString}", -1, PREG_SPLIT_NO_EMPTY ); |
| 1021 |
|
| 1022 |
|
| 1023 |
foreach ( $references as &$ref ) { |
| 1024 |
$ref = self::escapeIdForAttribute( $ref ); |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
$referenceString = implode( ' ', $references ); |
| 1030 |
|
| 1031 |
return $referenceString; |
| 1032 |
} |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
public static function escapeClass( string $class ): string { |
| 1043 |
|
| 1044 |
return rtrim( preg_replace( |
| 1045 |
[ '/(^[0-9\\-])|[\\x00-\\x20!"#$%&\'()*+,.\\/:;<=>?@[\\]^`{|}~]|\\xC2\\xA0/', '/_+/' ], |
| 1046 |
'_', |
| 1047 |
$class ), '_' ); |
| 1048 |
} |
| 1049 |
|
| 1050 |
public static function escapeCombiningChar( string $html ): string { |
| 1051 |
return strtr( $html, [ |
| 1052 |
"\u{0338}" => '̸', |
| 1053 |
] ); |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
public static function escapeHtmlAllowEntities( string $html ): string { |
| 1066 |
$html = self::decodeCharReferences( $html ); |
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
$html = htmlspecialchars( $html, ENT_QUOTES | ENT_SUBSTITUTE ); |
| 1071 |
return self::escapeCombiningChar( $html ); |
| 1072 |
} |
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
public static function decodeTagAttributes( string $text ): array { |
| 1080 |
if ( trim( $text ) == '' ) { |
| 1081 |
return []; |
| 1082 |
} |
| 1083 |
|
| 1084 |
$pairs = []; |
| 1085 |
if ( !preg_match_all( |
| 1086 |
self::getAttribsRegex(), |
| 1087 |
$text, |
| 1088 |
$pairs, |
| 1089 |
PREG_SET_ORDER ) ) { |
| 1090 |
return []; |
| 1091 |
} |
| 1092 |
|
| 1093 |
$attribs = []; |
| 1094 |
foreach ( $pairs as $set ) { |
| 1095 |
$attribute = strtolower( $set[1] ); |
| 1096 |
|
| 1097 |
|
| 1098 |
if ( !preg_match( self::getAttribNameRegex(), $attribute ) ) { |
| 1099 |
continue; |
| 1100 |
} |
| 1101 |
|
| 1102 |
$value = self::getTagAttributeCallback( $set ); |
| 1103 |
|
| 1104 |
|
| 1105 |
$value = preg_replace( '/[\t\r\n ]+/', ' ', $value ); |
| 1106 |
$value = trim( $value ); |
| 1107 |
|
| 1108 |
|
| 1109 |
$attribs[$attribute] = self::decodeCharReferences( $value ); |
| 1110 |
} |
| 1111 |
return $attribs; |
| 1112 |
} |
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
public static function safeEncodeTagAttributes( array $assoc_array ): string { |
| 1119 |
$attribs = []; |
| 1120 |
foreach ( $assoc_array as $attribute => $value ) { |
| 1121 |
$encAttribute = htmlspecialchars( $attribute, ENT_COMPAT ); |
| 1122 |
$encValue = self::safeEncodeAttribute( $value ); |
| 1123 |
|
| 1124 |
$attribs[] = "$encAttribute=\"$encValue\""; |
| 1125 |
} |
| 1126 |
return count( $attribs ) ? ' ' . implode( ' ', $attribs ) : ''; |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
private static function getTagAttributeCallback( array $set ): string { |
| 1134 |
if ( isset( $set[5] ) ) { |
| 1135 |
|
| 1136 |
return $set[5]; |
| 1137 |
} elseif ( isset( $set[4] ) ) { |
| 1138 |
|
| 1139 |
return $set[4]; |
| 1140 |
} elseif ( isset( $set[3] ) ) { |
| 1141 |
|
| 1142 |
return $set[3]; |
| 1143 |
} elseif ( !isset( $set[2] ) ) { |
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
return ""; |
| 1148 |
} else { |
| 1149 |
throw new LogicException( "Tag conditions not met. This should never happen and is a bug." ); |
| 1150 |
} |
| 1151 |
} |
| 1152 |
|
| 1153 |
private static function normalizeWhitespace( string $text ): string { |
| 1154 |
$normalized = preg_replace( '/[ \r\n\t]+/', ' ', $text ); |
| 1155 |
if ( $normalized === null ) { |
| 1156 |
wfLogWarning( __METHOD__ . ': Failed to normalize whitespace: ' . preg_last_error() ); |
| 1157 |
return ''; |
| 1158 |
} |
| 1159 |
return trim( $normalized ); |
| 1160 |
} |
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
public static function normalizeSectionNameWhitespace( string $section ): string { |
| 1168 |
$normalized = preg_replace( '/[ _]+/', ' ', $section ); |
| 1169 |
if ( $normalized === null ) { |
| 1170 |
wfLogWarning( __METHOD__ . ': Failed to normalize whitespace: ' . preg_last_error() ); |
| 1171 |
return ''; |
| 1172 |
} |
| 1173 |
return trim( $normalized ); |
| 1174 |
} |
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
public static function normalizeCharReferences( string $text ): string { |
| 1190 |
return preg_replace_callback( |
| 1191 |
self::CHAR_REFS_REGEX, |
| 1192 |
[ self::class, 'normalizeCharReferencesCallback' ], |
| 1193 |
$text, -1, $count, PREG_UNMATCHED_AS_NULL |
| 1194 |
); |
| 1195 |
} |
| 1196 |
|
| 1197 |
private static function normalizeCharReferencesCallback( array $matches ): string { |
| 1198 |
$ret = null; |
| 1199 |
if ( isset( $matches[1] ) ) { |
| 1200 |
$ret = self::normalizeEntity( $matches[1] ); |
| 1201 |
} elseif ( isset( $matches[2] ) ) { |
| 1202 |
$ret = self::decCharReference( $matches[2] ); |
| 1203 |
} elseif ( isset( $matches[3] ) ) { |
| 1204 |
$ret = self::hexCharReference( $matches[3] ); |
| 1205 |
} |
| 1206 |
if ( $ret === null ) { |
| 1207 |
return htmlspecialchars( $matches[0], ENT_COMPAT ); |
| 1208 |
} else { |
| 1209 |
return $ret; |
| 1210 |
} |
| 1211 |
} |
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
private static function normalizeEntity( string $name ): string { |
| 1224 |
if ( isset( self::MW_ENTITY_ALIASES[$name] ) ) { |
| 1225 |
|
| 1226 |
return '&' . self::MW_ENTITY_ALIASES[$name]; |
| 1227 |
} elseif ( in_array( $name, [ 'lt;', 'gt;', 'amp;', 'quot;' ], true ) ) { |
| 1228 |
|
| 1229 |
return "&$name"; |
| 1230 |
} elseif ( isset( HTMLData::$namedEntityTranslations[$name] ) ) { |
| 1231 |
|
| 1232 |
return preg_replace_callback( '/./Ssu', static function ( $m ) { |
| 1233 |
return '&#' . \UtfNormal\Utils::utf8ToCodepoint( $m[0] ) . ';'; |
| 1234 |
}, HTMLData::$namedEntityTranslations[$name] ); |
| 1235 |
} else { |
| 1236 |
return "&$name"; |
| 1237 |
} |
| 1238 |
} |
| 1239 |
|
| 1240 |
private static function decCharReference( string $codepoint ): ?string { |
| 1241 |
|
| 1242 |
|
| 1243 |
$point = intval( $codepoint ); |
| 1244 |
if ( self::validateCodepoint( $point ) ) { |
| 1245 |
return "&#$point;"; |
| 1246 |
} else { |
| 1247 |
return null; |
| 1248 |
} |
| 1249 |
} |
| 1250 |
|
| 1251 |
private static function hexCharReference( string $codepoint ): ?string { |
| 1252 |
$point = hexdec( $codepoint ); |
| 1253 |
|
| 1254 |
if ( is_int( $point ) && self::validateCodepoint( $point ) ) { |
| 1255 |
return sprintf( '&#x%x;', $point ); |
| 1256 |
} else { |
| 1257 |
return null; |
| 1258 |
} |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
private static function validateCodepoint( int $codepoint ): bool { |
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
return $codepoint == 0x09 |
| 1270 |
|| $codepoint == 0x0a |
| 1271 |
|| ( $codepoint >= 0x20 && $codepoint <= 0x7e ) |
| 1272 |
|| ( $codepoint >= 0xa0 && $codepoint <= 0xd7ff ) |
| 1273 |
|| ( $codepoint >= 0xe000 && $codepoint <= 0xfffd ) |
| 1274 |
|| ( $codepoint >= 0x10000 && $codepoint <= 0x10ffff ); |
| 1275 |
} |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
public static function decodeCharReferences( string $text ): string { |
| 1282 |
return preg_replace_callback( |
| 1283 |
self::CHAR_REFS_REGEX, |
| 1284 |
[ self::class, 'decodeCharReferencesCallback' ], |
| 1285 |
$text, -1, $count, PREG_UNMATCHED_AS_NULL |
| 1286 |
); |
| 1287 |
} |
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
public static function decodeCharReferencesAndNormalize( string $text ): string { |
| 1300 |
$text = preg_replace_callback( |
| 1301 |
self::CHAR_REFS_REGEX, |
| 1302 |
[ self::class, 'decodeCharReferencesCallback' ], |
| 1303 |
$text, -1, $count, PREG_UNMATCHED_AS_NULL |
| 1304 |
); |
| 1305 |
|
| 1306 |
if ( $count ) { |
| 1307 |
return MediaWikiServices::getInstance()->getContentLanguage()->normalize( $text ); |
| 1308 |
} else { |
| 1309 |
return $text; |
| 1310 |
} |
| 1311 |
} |
| 1312 |
|
| 1313 |
private static function decodeCharReferencesCallback( array $matches ): string { |
| 1314 |
if ( isset( $matches[1] ) ) { |
| 1315 |
return self::decodeEntity( $matches[1] ); |
| 1316 |
} elseif ( isset( $matches[2] ) ) { |
| 1317 |
|
| 1318 |
|
| 1319 |
return self::decodeChar( @intval( $matches[2] ) ); |
| 1320 |
} elseif ( isset( $matches[3] ) ) { |
| 1321 |
$point = hexdec( $matches[3] ); |
| 1322 |
|
| 1323 |
if ( !is_int( $point ) ) { |
| 1324 |
|
| 1325 |
return \UtfNormal\Constants::UTF8_REPLACEMENT; |
| 1326 |
} |
| 1327 |
return self::decodeChar( $point ); |
| 1328 |
} |
| 1329 |
|
| 1330 |
return $matches[0]; |
| 1331 |
} |
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
private static function decodeChar( int $codepoint ): string { |
| 1339 |
if ( self::validateCodepoint( $codepoint ) ) { |
| 1340 |
return \UtfNormal\Utils::codepointToUtf8( $codepoint ); |
| 1341 |
} else { |
| 1342 |
return \UtfNormal\Constants::UTF8_REPLACEMENT; |
| 1343 |
} |
| 1344 |
} |
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
private static function decodeEntity( string $name ): string { |
| 1355 |
|
| 1356 |
if ( isset( self::MW_ENTITY_ALIASES[$name] ) ) { |
| 1357 |
$name = self::MW_ENTITY_ALIASES[$name]; |
| 1358 |
} |
| 1359 |
$trans = HTMLData::$namedEntityTranslations[$name] ?? null; |
| 1360 |
return $trans ?? "&$name"; |
| 1361 |
} |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
private static function attributesAllowedInternal( string $element ): array { |
| 1371 |
$list = self::setupAttributesAllowedInternal(); |
| 1372 |
return $list[$element] ?? []; |
| 1373 |
} |
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
private static function setupAttributesAllowedInternal(): array { |
| 1383 |
static $allowed; |
| 1384 |
|
| 1385 |
if ( $allowed !== null ) { |
| 1386 |
return $allowed; |
| 1387 |
} |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
$merge = static function ( $a, $b, $c = [] ) { |
| 1392 |
return array_merge( |
| 1393 |
$a, |
| 1394 |
array_fill_keys( $b, true ), |
| 1395 |
array_fill_keys( $c, true ) ); |
| 1396 |
}; |
| 1397 |
$common = $merge( [], [ |
| 1398 |
|
| 1399 |
'id', |
| 1400 |
'class', |
| 1401 |
'style', |
| 1402 |
'lang', |
| 1403 |
'dir', |
| 1404 |
'title', |
| 1405 |
'tabindex', |
| 1406 |
|
| 1407 |
|
| 1408 |
'aria-describedby', |
| 1409 |
'aria-flowto', |
| 1410 |
'aria-hidden', |
| 1411 |
'aria-label', |
| 1412 |
'aria-labelledby', |
| 1413 |
'aria-level', |
| 1414 |
'aria-owns', |
| 1415 |
'role', |
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
'about', |
| 1421 |
'property', |
| 1422 |
'resource', |
| 1423 |
'datatype', |
| 1424 |
'typeof', |
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
'itemid', |
| 1429 |
'itemprop', |
| 1430 |
'itemref', |
| 1431 |
'itemscope', |
| 1432 |
'itemtype', |
| 1433 |
] ); |
| 1434 |
|
| 1435 |
$block = $merge( $common, [ 'align' ] ); |
| 1436 |
|
| 1437 |
$tablealign = [ 'align', 'valign' ]; |
| 1438 |
$tablecell = [ |
| 1439 |
'abbr', |
| 1440 |
'axis', |
| 1441 |
'headers', |
| 1442 |
'scope', |
| 1443 |
'rowspan', |
| 1444 |
'colspan', |
| 1445 |
'nowrap', |
| 1446 |
'width', |
| 1447 |
'height', |
| 1448 |
'bgcolor', |
| 1449 |
]; |
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
$allowed = [ |
| 1454 |
|
| 1455 |
'div' => $block, |
| 1456 |
'center' => $common, |
| 1457 |
'span' => $common, |
| 1458 |
|
| 1459 |
|
| 1460 |
'h1' => $block, |
| 1461 |
'h2' => $block, |
| 1462 |
'h3' => $block, |
| 1463 |
'h4' => $block, |
| 1464 |
'h5' => $block, |
| 1465 |
'h6' => $block, |
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
'bdo' => $common, |
| 1472 |
|
| 1473 |
|
| 1474 |
'em' => $common, |
| 1475 |
'strong' => $common, |
| 1476 |
'cite' => $common, |
| 1477 |
'dfn' => $common, |
| 1478 |
'code' => $common, |
| 1479 |
'samp' => $common, |
| 1480 |
'kbd' => $common, |
| 1481 |
'var' => $common, |
| 1482 |
'abbr' => $common, |
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
'blockquote' => $merge( $common, [ 'cite' ] ), |
| 1487 |
'q' => $merge( $common, [ 'cite' ] ), |
| 1488 |
|
| 1489 |
|
| 1490 |
'sub' => $common, |
| 1491 |
'sup' => $common, |
| 1492 |
|
| 1493 |
|
| 1494 |
'p' => $block, |
| 1495 |
|
| 1496 |
|
| 1497 |
'br' => $merge( $common, [ 'clear' ] ), |
| 1498 |
|
| 1499 |
|
| 1500 |
'wbr' => $common, |
| 1501 |
|
| 1502 |
|
| 1503 |
'pre' => $merge( $common, [ 'width' ] ), |
| 1504 |
|
| 1505 |
|
| 1506 |
'ins' => $merge( $common, [ 'cite', 'datetime' ] ), |
| 1507 |
'del' => $merge( $common, [ 'cite', 'datetime' ] ), |
| 1508 |
|
| 1509 |
|
| 1510 |
'ul' => $merge( $common, [ 'type' ] ), |
| 1511 |
'ol' => $merge( $common, [ 'type', 'start', 'reversed' ] ), |
| 1512 |
'li' => $merge( $common, [ 'type', 'value' ] ), |
| 1513 |
|
| 1514 |
|
| 1515 |
'dl' => $common, |
| 1516 |
'dd' => $common, |
| 1517 |
'dt' => $common, |
| 1518 |
|
| 1519 |
|
| 1520 |
'table' => $merge( $common, |
| 1521 |
[ 'summary', 'width', 'border', 'frame', |
| 1522 |
'rules', 'cellspacing', 'cellpadding', |
| 1523 |
'align', 'bgcolor', |
| 1524 |
] ), |
| 1525 |
|
| 1526 |
|
| 1527 |
'caption' => $block, |
| 1528 |
|
| 1529 |
|
| 1530 |
'thead' => $common, |
| 1531 |
'tfoot' => $common, |
| 1532 |
'tbody' => $common, |
| 1533 |
|
| 1534 |
|
| 1535 |
'colgroup' => $merge( $common, [ 'span' ] ), |
| 1536 |
'col' => $merge( $common, [ 'span' ] ), |
| 1537 |
|
| 1538 |
|
| 1539 |
'tr' => $merge( $common, [ 'bgcolor' ], $tablealign ), |
| 1540 |
|
| 1541 |
|
| 1542 |
'td' => $merge( $common, $tablecell, $tablealign ), |
| 1543 |
'th' => $merge( $common, $tablecell, $tablealign ), |
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
'a' => $merge( $common, [ 'href', 'rel', 'rev' ] ), |
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
'img' => $merge( $common, [ 'alt', 'src', 'width', 'height', 'srcset' ] ), |
| 1554 |
|
| 1555 |
'audio' => $merge( $common, [ 'controls', 'preload', 'width', 'height' ] ), |
| 1556 |
'video' => $merge( $common, [ 'poster', 'controls', 'preload', 'width', 'height' ] ), |
| 1557 |
'source' => $merge( $common, [ 'type', 'src' ] ), |
| 1558 |
'track' => $merge( $common, [ 'type', 'src', 'srclang', 'kind', 'label' ] ), |
| 1559 |
|
| 1560 |
|
| 1561 |
'tt' => $common, |
| 1562 |
'b' => $common, |
| 1563 |
'i' => $common, |
| 1564 |
'big' => $common, |
| 1565 |
'small' => $common, |
| 1566 |
'strike' => $common, |
| 1567 |
's' => $common, |
| 1568 |
'u' => $common, |
| 1569 |
|
| 1570 |
|
| 1571 |
'font' => $merge( $common, [ 'size', 'color', 'face' ] ), |
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
'hr' => $merge( $common, [ 'width' ] ), |
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
'ruby' => $common, |
| 1580 |
|
| 1581 |
'rb' => $common, |
| 1582 |
'rp' => $common, |
| 1583 |
'rt' => $common, |
| 1584 |
'rtc' => $common, |
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
'math' => $merge( [], [ 'class', 'style', 'id', 'title' ] ), |
| 1590 |
|
| 1591 |
|
| 1592 |
'figure' => $common, |
| 1593 |
'figcaption' => $common, |
| 1594 |
|
| 1595 |
|
| 1596 |
'bdi' => $common, |
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
'data' => $merge( $common, [ 'value' ] ), |
| 1601 |
'time' => $merge( $common, [ 'datetime' ] ), |
| 1602 |
'mark' => $common, |
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
'meta' => $merge( [], [ 'itemprop', 'content' ] ), |
| 1610 |
'link' => $merge( [], [ 'itemprop', 'href', 'title' ] ), |
| 1611 |
|
| 1612 |
|
| 1613 |
'aside' => $common, |
| 1614 |
]; |
| 1615 |
|
| 1616 |
return $allowed; |
| 1617 |
} |
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
public static function stripAllTags( string $html ): string { |
| 1631 |
|
| 1632 |
$handler = new RemexStripTagHandler; |
| 1633 |
$tokenizer = new RemexTokenizer( $handler, $html, [ |
| 1634 |
'ignoreErrors' => true, |
| 1635 |
|
| 1636 |
'ignoreNulls' => true, |
| 1637 |
'skipPreprocess' => true, |
| 1638 |
] ); |
| 1639 |
$tokenizer->execute(); |
| 1640 |
$text = $handler->getResult(); |
| 1641 |
|
| 1642 |
$text = self::normalizeWhitespace( $text ); |
| 1643 |
return $text; |
| 1644 |
} |
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
public static function hackDocType(): string { |
| 1657 |
$out = "<!DOCTYPE html [\n"; |
| 1658 |
foreach ( HTMLData::$namedEntityTranslations as $entity => $translation ) { |
| 1659 |
if ( substr( $entity, -1 ) !== ';' ) { |
| 1660 |
|
| 1661 |
|
| 1662 |
continue; |
| 1663 |
} |
| 1664 |
$name = substr( $entity, 0, -1 ); |
| 1665 |
$expansion = self::normalizeEntity( $entity ); |
| 1666 |
if ( $entity === $expansion ) { |
| 1667 |
|
| 1668 |
continue; |
| 1669 |
} |
| 1670 |
$out .= "<!ENTITY $name \"$expansion\">"; |
| 1671 |
} |
| 1672 |
$out .= "]>\n"; |
| 1673 |
return $out; |
| 1674 |
} |
| 1675 |
|
| 1676 |
public static function cleanUrl( string $url ): string { |
| 1677 |
|
| 1678 |
|
| 1679 |
$url = self::decodeCharReferences( $url ); |
| 1680 |
|
| 1681 |
|
| 1682 |
$url = preg_replace_callback( '/[\][<>"\\x00-\\x20\\x7F\|]+/', |
| 1683 |
static fn ( $m ) => urlencode( $m[0] ), $url ); |
| 1684 |
|
| 1685 |
|
| 1686 |
$matches = []; |
| 1687 |
if ( preg_match( '!^([^:]+:)(//[^/]+)?(.*)$!iD', $url, $matches ) ) { |
| 1688 |
[ , $protocol, $host, $rest ] = $matches; |
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
$strip = "/ |
| 1695 |
\\s| # general whitespace |
| 1696 |
\u{00AD}| # SOFT HYPHEN |
| 1697 |
\u{034F}| # COMBINING GRAPHEME JOINER |
| 1698 |
\u{061C}| # ARABIC LETTER MARK |
| 1699 |
[\u{115F}-\u{1160}]| # HANGUL CHOSEONG FILLER.. |
| 1700 |
# HANGUL JUNGSEONG FILLER |
| 1701 |
[\u{17B4}-\u{17B5}]| # KHMER VOWEL INHERENT AQ.. |
| 1702 |
# KHMER VOWEL INHERENT AA |
| 1703 |
[\u{180B}-\u{180D}]| # MONGOLIAN FREE VARIATION SELECTOR ONE.. |
| 1704 |
# MONGOLIAN FREE VARIATION SELECTOR THREE |
| 1705 |
\u{180E}| # MONGOLIAN VOWEL SEPARATOR |
| 1706 |
[\u{200B}-\u{200F}]| # ZERO WIDTH SPACE.. |
| 1707 |
# RIGHT-TO-LEFT MARK |
| 1708 |
[\u{202A}-\u{202E}]| # LEFT-TO-RIGHT EMBEDDING.. |
| 1709 |
# RIGHT-TO-LEFT OVERRIDE |
| 1710 |
[\u{2060}-\u{2064}]| # WORD JOINER.. |
| 1711 |
# INVISIBLE PLUS |
| 1712 |
\u{2065}| # <reserved-2065> |
| 1713 |
[\u{2066}-\u{206F}]| # LEFT-TO-RIGHT ISOLATE.. |
| 1714 |
# NOMINAL DIGIT SHAPES |
| 1715 |
\u{3164}| # HANGUL FILLER |
| 1716 |
[\u{FE00}-\u{FE0F}]| # VARIATION SELECTOR-1.. |
| 1717 |
# VARIATION SELECTOR-16 |
| 1718 |
\u{FEFF}| # ZERO WIDTH NO-BREAK SPACE |
| 1719 |
\u{FFA0}| # HALFWIDTH HANGUL FILLER |
| 1720 |
[\u{FFF0}-\u{FFF8}]| # <reserved-FFF0>.. |
| 1721 |
# <reserved-FFF8> |
| 1722 |
[\u{1BCA0}-\u{1BCA3}]| # SHORTHAND FORMAT LETTER OVERLAP.. |
| 1723 |
# SHORTHAND FORMAT UP STEP |
| 1724 |
[\u{1D173}-\u{1D17A}]| # MUSICAL SYMBOL BEGIN BEAM.. |
| 1725 |
# MUSICAL SYMBOL END PHRASE |
| 1726 |
\u{E0000}| # <reserved-E0000> |
| 1727 |
\u{E0001}| # LANGUAGE TAG |
| 1728 |
[\u{E0002}-\u{E001F}]| # <reserved-E0002>.. |
| 1729 |
# <reserved-E001F> |
| 1730 |
[\u{E0020}-\u{E007F}]| # TAG SPACE.. |
| 1731 |
# CANCEL TAG |
| 1732 |
[\u{E0080}-\u{E00FF}]| # <reserved-E0080>.. |
| 1733 |
# <reserved-E00FF> |
| 1734 |
[\u{E0100}-\u{E01EF}]| # VARIATION SELECTOR-17.. |
| 1735 |
# VARIATION SELECTOR-256 |
| 1736 |
[\u{E01F0}-\u{E0FFF}]| # <reserved-E01F0>.. |
| 1737 |
# <reserved-E0FFF> |
| 1738 |
/xuD"; |
| 1739 |
|
| 1740 |
$host = preg_replace( $strip, '', $host ); |
| 1741 |
|
| 1742 |
|
| 1743 |
if ( str_starts_with( $host, "//%5B" ) && |
| 1744 |
preg_match( '!^//%5B([0-9A-Fa-f:.]+)%5D((:\d+)?)$!', $host, $matches ) |
| 1745 |
) { |
| 1746 |
$host = '//[' . $matches[1] . ']' . $matches[2]; |
| 1747 |
} |
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
return $protocol . $host . $rest; |
| 1752 |
} else { |
| 1753 |
return $url; |
| 1754 |
} |
| 1755 |
} |
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
public static function validateEmail( string $addr ): bool { |
| 1786 |
$result = null; |
| 1787 |
|
| 1788 |
$hookRunner = new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ); |
| 1789 |
if ( !$hookRunner->onIsValidEmailAddr( $addr, $result ) ) { |
| 1790 |
return $result; |
| 1791 |
} |
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
$rfc5322_atext = "a-z0-9!#$%&'*+\\-\/=?^_`{|}~"; |
| 1797 |
$rfc1034_ldh_str = "a-z0-9\\-"; |
| 1798 |
|
| 1799 |
$html5_email_regexp = "/ |
| 1800 |
^ # start of string |
| 1801 |
[$rfc5322_atext\\.]+ # user part which is liberal :p |
| 1802 |
@ # 'apostrophe' |
| 1803 |
[$rfc1034_ldh_str]+ # First domain part |
| 1804 |
(\\.[$rfc1034_ldh_str]+)* # Following part prefixed with a dot |
| 1805 |
$ # End of string |
| 1806 |
/ix"; |
| 1807 |
|
| 1808 |
return (bool)preg_match( $html5_email_regexp, $addr ); |
| 1809 |
} |
| 1810 |
} |
| 1811 |
|
| 1812 |
|
| 1813 |
class_alias( Sanitizer::class, 'Sanitizer' ); |