| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\Parser; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use LogicException; |
| 7 |
use MediaWiki\Extension\AbuseFilter\KeywordsManager; |
| 8 |
use MediaWiki\Extension\AbuseFilter\Parser\Exception\InternalException; |
| 9 |
use MediaWiki\Extension\AbuseFilter\Parser\Exception\UserVisibleException; |
| 10 |
use MediaWiki\Message\Message; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class SyntaxChecker { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private $treeRoot; |
| 35 |
|
| 36 |
|
| 37 |
private $keywordsManager; |
| 38 |
|
| 39 |
public const MCONSERVATIVE = 'MODE_CONSERVATIVE'; |
| 40 |
public const MLIBERAL = 'MODE_LIBERAL'; |
| 41 |
public const DUMMYPOS = 0; |
| 42 |
public const CACHE_VERSION = 1; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private $mode; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
private $checkUnusedVars; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function __construct( |
| 77 |
AFPSyntaxTree $tree, |
| 78 |
KeywordsManager $keywordsManager, |
| 79 |
string $mode = self::MCONSERVATIVE, |
| 80 |
bool $checkUnusedVars = false |
| 81 |
) { |
| 82 |
$this->treeRoot = $tree->getRoot(); |
| 83 |
$this->keywordsManager = $keywordsManager; |
| 84 |
$this->mode = $mode; |
| 85 |
$this->checkUnusedVars = $checkUnusedVars; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function start(): void { |
| 94 |
if ( !$this->treeRoot ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
$bound = $this->check( $this->desugar( $this->treeRoot ), [] ); |
| 98 |
$unused = array_keys( array_filter( $bound, static function ( $v ) { |
| 99 |
return !$v; |
| 100 |
} ) ); |
| 101 |
if ( $this->checkUnusedVars && $unused ) { |
| 102 |
throw new UserVisibleException( |
| 103 |
'unusedvars', |
| 104 |
self::DUMMYPOS, |
| 105 |
[ Message::listParam( $unused, 'comma' ) ] |
| 106 |
); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
private function desugar( AFPTreeNode $node ): AFPTreeNode { |
| 130 |
switch ( $node->type ) { |
| 131 |
case AFPTreeNode::ATOM: |
| 132 |
return $node; |
| 133 |
|
| 134 |
case AFPTreeNode::FUNCTION_CALL: |
| 135 |
if ( $node->children[0] === 'set_var' ) { |
| 136 |
$node->children[0] = 'set'; |
| 137 |
} |
| 138 |
return $this->newNodeMapExceptFirst( $node ); |
| 139 |
|
| 140 |
case AFPTreeNode::ARRAY_INDEX: |
| 141 |
return $this->newNodeNamedBinop( $node, '[]' ); |
| 142 |
|
| 143 |
case AFPTreeNode::POW: |
| 144 |
return $this->newNodeNamedBinop( $node, '**' ); |
| 145 |
|
| 146 |
case AFPTreeNode::UNARY: |
| 147 |
case AFPTreeNode::INDEX_ASSIGNMENT: |
| 148 |
case AFPTreeNode::ARRAY_APPEND: |
| 149 |
return $this->newNodeMapExceptFirst( $node ); |
| 150 |
|
| 151 |
case AFPTreeNode::BOOL_INVERT: |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
return $this->newNodeMapAll( $node ); |
| 158 |
|
| 159 |
case AFPTreeNode::KEYWORD_OPERATOR: |
| 160 |
case AFPTreeNode::MUL_REL: |
| 161 |
case AFPTreeNode::SUM_REL: |
| 162 |
case AFPTreeNode::COMPARE: |
| 163 |
return $this->newNodeBinop( $node ); |
| 164 |
|
| 165 |
case AFPTreeNode::LOGIC: |
| 166 |
$result = $this->newNodeBinop( $node ); |
| 167 |
[ $op, $left, $right ] = $result->children; |
| 168 |
if ( $op === '&' || $op === '|' ) { |
| 169 |
return $this->desugarAndOr( $op, $left, $right, $node->position ); |
| 170 |
} else { |
| 171 |
return $result; |
| 172 |
} |
| 173 |
|
| 174 |
case AFPTreeNode::ARRAY_DEFINITION: |
| 175 |
case AFPTreeNode::SEMICOLON: |
| 176 |
return $this->newNodeMapAll( $node ); |
| 177 |
|
| 178 |
case AFPTreeNode::CONDITIONAL: |
| 179 |
if ( $node->children[2] === null ) { |
| 180 |
$node->children[2] = new AFPTreeNode( |
| 181 |
AFPTreeNode::ATOM, |
| 182 |
new AFPToken( |
| 183 |
AFPToken::TKEYWORD, |
| 184 |
"null", |
| 185 |
$node->position |
| 186 |
), |
| 187 |
$node->position |
| 188 |
); |
| 189 |
} |
| 190 |
return $this->newNodeMapAll( $node ); |
| 191 |
|
| 192 |
case AFPTreeNode::ASSIGNMENT: |
| 193 |
[ $varname, $value ] = $node->children; |
| 194 |
|
| 195 |
return new AFPTreeNode( |
| 196 |
AFPTreeNode::FUNCTION_CALL, |
| 197 |
[ |
| 198 |
"set", |
| 199 |
new AFPTreeNode( |
| 200 |
AFPTreeNode::ATOM, |
| 201 |
new AFPToken( |
| 202 |
AFPToken::TSTRING, |
| 203 |
$varname, |
| 204 |
$node->position |
| 205 |
), |
| 206 |
$node->position |
| 207 |
), |
| 208 |
$this->desugar( $value ) |
| 209 |
], |
| 210 |
$node->position |
| 211 |
); |
| 212 |
|
| 213 |
default: |
| 214 |
|
| 215 |
throw new InternalException( "Unknown node type passed: {$node->type}" ); |
| 216 |
|
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
private function desugarAndOr( |
| 228 |
string $op, |
| 229 |
AFPTreeNode $left, |
| 230 |
AFPTreeNode $right, |
| 231 |
int $position |
| 232 |
): AFPTreeNode { |
| 233 |
$trueNode = new AFPTreeNode( |
| 234 |
AFPTreeNode::ATOM, |
| 235 |
new AFPToken( |
| 236 |
AFPToken::TKEYWORD, |
| 237 |
"true", |
| 238 |
$position |
| 239 |
), |
| 240 |
$position |
| 241 |
); |
| 242 |
$falseNode = new AFPTreeNode( |
| 243 |
AFPTreeNode::ATOM, |
| 244 |
new AFPToken( |
| 245 |
AFPToken::TKEYWORD, |
| 246 |
"false", |
| 247 |
$position |
| 248 |
), |
| 249 |
$position |
| 250 |
); |
| 251 |
$conditionalNode = new AFPTreeNode( |
| 252 |
AFPTreeNode::CONDITIONAL, |
| 253 |
[ |
| 254 |
$right, |
| 255 |
$trueNode, |
| 256 |
$falseNode |
| 257 |
], |
| 258 |
$position |
| 259 |
); |
| 260 |
|
| 261 |
if ( $op === '&' ) { |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
return new AFPTreeNode( |
| 266 |
AFPTreeNode::CONDITIONAL, |
| 267 |
[ |
| 268 |
$left, |
| 269 |
$conditionalNode, |
| 270 |
$falseNode |
| 271 |
], |
| 272 |
$position |
| 273 |
); |
| 274 |
} elseif ( $op === '|' ) { |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
return new AFPTreeNode( |
| 279 |
AFPTreeNode::CONDITIONAL, |
| 280 |
[ |
| 281 |
$left, |
| 282 |
$trueNode, |
| 283 |
$conditionalNode |
| 284 |
], |
| 285 |
$position |
| 286 |
); |
| 287 |
} else { |
| 288 |
|
| 289 |
throw new InternalException( "Unknown operator: {$op}" ); |
| 290 |
|
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
private function newNode( AFPTreeNode $node, $children ): AFPTreeNode { |
| 303 |
return new AFPTreeNode( $node->type, $children, $node->position ); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
private function newNodeReplaceType( |
| 315 |
AFPTreeNode $node, |
| 316 |
string $type |
| 317 |
): AFPTreeNode { |
| 318 |
return new AFPTreeNode( $type, $node->children, $node->position ); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
private function newNodeMapAll( AFPTreeNode $node ): AFPTreeNode { |
| 328 |
$children = $node->children; |
| 329 |
if ( !is_array( $children ) ) { |
| 330 |
|
| 331 |
throw new LogicException( |
| 332 |
"Unexpected non-array children of an AFPTreeNode of type " . |
| 333 |
"{$node->type} at position {$node->position}" |
| 334 |
); |
| 335 |
|
| 336 |
} |
| 337 |
return $this->newNode( $node, array_map( [ $this, 'desugar' ], $children ) ); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
private function newNodeMapExceptFirst( AFPTreeNode $node ): AFPTreeNode { |
| 347 |
$items = [ $node->children[0] ]; |
| 348 |
$args = array_slice( $node->children, 1 ); |
| 349 |
foreach ( $args as $el ) { |
| 350 |
$items[] = $this->desugar( $el ); |
| 351 |
} |
| 352 |
return $this->newNode( $node, $items ); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
private function newNodeBinop( AFPTreeNode $node ): AFPTreeNode { |
| 362 |
return $this->newNodeReplaceType( |
| 363 |
$this->newNodeMapExceptFirst( $node ), |
| 364 |
AFPTreeNode::BINOP |
| 365 |
); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
private function newNodeNamedBinop( |
| 376 |
AFPTreeNode $node, |
| 377 |
string $op |
| 378 |
): AFPTreeNode { |
| 379 |
$items = $this->newNodeMapAll( $node )->children; |
| 380 |
array_unshift( $items, $op ); |
| 381 |
return $this->newNodeReplaceType( |
| 382 |
$this->newNode( $node, $items ), |
| 383 |
AFPTreeNode::BINOP |
| 384 |
); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
private function check( AFPTreeNode $node, array $bound ): array { |
| 415 |
switch ( $node->type ) { |
| 416 |
|
| 417 |
case AFPTreeNode::ATOM: |
| 418 |
$tok = $node->children; |
| 419 |
switch ( $tok->type ) { |
| 420 |
case AFPToken::TID: |
| 421 |
return $this->lookupVar( |
| 422 |
$tok->value, |
| 423 |
$tok->pos, |
| 424 |
$bound |
| 425 |
); |
| 426 |
|
| 427 |
case AFPToken::TSTRING: |
| 428 |
case AFPToken::TFLOAT: |
| 429 |
case AFPToken::TINT: |
| 430 |
case AFPToken::TKEYWORD: |
| 431 |
return $bound; |
| 432 |
|
| 433 |
default: |
| 434 |
|
| 435 |
throw new InternalException( "Unknown token {$tok->type} provided in the ATOM node" ); |
| 436 |
|
| 437 |
} |
| 438 |
case AFPTreeNode::ARRAY_DEFINITION: |
| 439 |
|
| 440 |
foreach ( $node->children as $el ) { |
| 441 |
$bound = $this->check( $el, $bound ); |
| 442 |
} |
| 443 |
return $bound; |
| 444 |
|
| 445 |
case AFPTreeNode::FUNCTION_CALL: |
| 446 |
$fname = $node->children[0]; |
| 447 |
$args = array_slice( $node->children, 1 ); |
| 448 |
if ( !array_key_exists( $fname, FilterEvaluator::FUNCTIONS ) ) { |
| 449 |
throw new UserVisibleException( |
| 450 |
'unknownfunction', |
| 451 |
$node->position, |
| 452 |
[ $fname ] |
| 453 |
); |
| 454 |
} |
| 455 |
$this->checkArgCount( $args, $fname, $node->position ); |
| 456 |
|
| 457 |
if ( $fname === 'set' ) { |
| 458 |
|
| 459 |
$tok = $args[0]->children; |
| 460 |
|
| 461 |
if ( |
| 462 |
!( $tok instanceof AFPToken ) || |
| 463 |
$tok->type !== AFPToken::TSTRING |
| 464 |
) { |
| 465 |
throw new UserVisibleException( |
| 466 |
'variablevariable', |
| 467 |
$node->position, |
| 468 |
[] |
| 469 |
); |
| 470 |
} |
| 471 |
|
| 472 |
$bound = $this->check( $args[1], $bound ); |
| 473 |
|
| 474 |
return $this->assignVar( |
| 475 |
$tok->value, |
| 476 |
$tok->pos, |
| 477 |
$bound |
| 478 |
); |
| 479 |
} else { |
| 480 |
foreach ( $args as $arg ) { |
| 481 |
$bound = $this->check( $arg, $bound ); |
| 482 |
} |
| 483 |
return $bound; |
| 484 |
} |
| 485 |
|
| 486 |
case AFPTreeNode::BINOP: |
| 487 |
[ , $left, $right ] = $node->children; |
| 488 |
return $this->check( $right, $this->check( $left, $bound ) ); |
| 489 |
|
| 490 |
case AFPTreeNode::UNARY: |
| 491 |
[ , $argument ] = $node->children; |
| 492 |
return $this->check( $argument, $bound ); |
| 493 |
|
| 494 |
case AFPTreeNode::BOOL_INVERT: |
| 495 |
[ $argument ] = $node->children; |
| 496 |
return $this->check( $argument, $bound ); |
| 497 |
|
| 498 |
case AFPTreeNode::CONDITIONAL: |
| 499 |
[ $condition, $exprIfTrue, $exprIfFalse ] = $node->children; |
| 500 |
$bound = $this->check( $condition, $bound ); |
| 501 |
$boundLeft = $this->check( $exprIfTrue, $bound ); |
| 502 |
$boundRight = $this->check( $exprIfFalse, $bound ); |
| 503 |
switch ( $this->mode ) { |
| 504 |
case self::MCONSERVATIVE: |
| 505 |
return $this->mapUnion( $boundLeft, $boundRight ); |
| 506 |
case self::MLIBERAL: |
| 507 |
return $this->mapIntersect( $boundLeft, $boundRight ); |
| 508 |
default: |
| 509 |
|
| 510 |
throw new LogicException( "Unknown mode: {$this->mode}" ); |
| 511 |
|
| 512 |
} |
| 513 |
|
| 514 |
case AFPTreeNode::INDEX_ASSIGNMENT: |
| 515 |
[ $varName, $offset, $value ] = $node->children; |
| 516 |
|
| 517 |
|
| 518 |
$bound = $this->lookupVar( $varName, $node->position, $bound ); |
| 519 |
$bound = $this->check( $offset, $bound ); |
| 520 |
$bound = $this->check( $value, $bound ); |
| 521 |
|
| 522 |
return $this->assignVar( $varName, $node->position, $bound ); |
| 523 |
|
| 524 |
case AFPTreeNode::ARRAY_APPEND: |
| 525 |
[ $varName, $value ] = $node->children; |
| 526 |
|
| 527 |
|
| 528 |
$bound = $this->lookupVar( $varName, $node->position, $bound ); |
| 529 |
$bound = $this->check( $value, $bound ); |
| 530 |
|
| 531 |
return $this->assignVar( $varName, $node->position, $bound ); |
| 532 |
|
| 533 |
case AFPTreeNode::SEMICOLON: |
| 534 |
|
| 535 |
foreach ( $node->children as $statement ) { |
| 536 |
$bound = $this->check( $statement, $bound ); |
| 537 |
} |
| 538 |
return $bound; |
| 539 |
|
| 540 |
default: |
| 541 |
|
| 542 |
throw new LogicException( "Unknown type: {$node->type}" ); |
| 543 |
|
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
private function mapUnion( array $left, array $right ): array { |
| 553 |
foreach ( $right as $key => $val ) { |
| 554 |
if ( array_key_exists( $key, $left ) ) { |
| 555 |
$left[ $key ] = $left[ $key ] || $val; |
| 556 |
} else { |
| 557 |
$left[ $key ] = $val; |
| 558 |
} |
| 559 |
} |
| 560 |
return $left; |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
private function mapIntersect( array $left, array $right ): array { |
| 569 |
$keys = array_intersect_key( $left, $right ); |
| 570 |
$result = []; |
| 571 |
foreach ( $keys as $key => $val ) { |
| 572 |
$result[ $key ] = $left[ $key ] || $right[ $key ]; |
| 573 |
} |
| 574 |
return $result; |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
private function assignVar( string $var, int $pos, array $bound ): array { |
| 584 |
$var = strtolower( $var ); |
| 585 |
if ( $this->isReservedIdentifier( $var ) ) { |
| 586 |
throw new UserVisibleException( |
| 587 |
'overridebuiltin', |
| 588 |
$pos, |
| 589 |
[ $var ] |
| 590 |
); |
| 591 |
} |
| 592 |
$bound[ $var ] = false; |
| 593 |
return $bound; |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
private function lookupVar( string $var, int $pos, array $bound ): array { |
| 603 |
$var = strtolower( $var ); |
| 604 |
if ( array_key_exists( $var, $bound ) ) { |
| 605 |
|
| 606 |
$bound[ $var ] = true; |
| 607 |
return $bound; |
| 608 |
} elseif ( $this->keywordsManager->isVarDisabled( $var ) ) { |
| 609 |
|
| 610 |
throw new UserVisibleException( |
| 611 |
'disabledvar', |
| 612 |
$pos, |
| 613 |
[ $var ] |
| 614 |
); |
| 615 |
} elseif ( $this->keywordsManager->varExists( $var ) ) { |
| 616 |
|
| 617 |
return $bound; |
| 618 |
} elseif ( $this->isReservedIdentifier( $var ) ) { |
| 619 |
|
| 620 |
throw new UserVisibleException( |
| 621 |
'usebuiltin', |
| 622 |
$pos, |
| 623 |
[ $var ] |
| 624 |
); |
| 625 |
} else { |
| 626 |
|
| 627 |
throw new UserVisibleException( |
| 628 |
'unrecognisedvar', |
| 629 |
$pos, |
| 630 |
[ $var ] |
| 631 |
); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
private function checkArgCount( array $args, string $func, int $position ): void { |
| 644 |
if ( !array_key_exists( $func, FilterEvaluator::FUNC_ARG_COUNT ) ) { |
| 645 |
|
| 646 |
throw new InvalidArgumentException( "$func is not a valid function." ); |
| 647 |
|
| 648 |
} |
| 649 |
[ $min, $max ] = FilterEvaluator::FUNC_ARG_COUNT[ $func ]; |
| 650 |
if ( count( $args ) < $min ) { |
| 651 |
throw new UserVisibleException( |
| 652 |
$min === 1 ? 'noparams' : 'notenoughargs', |
| 653 |
$position, |
| 654 |
[ $func, $min, count( $args ) ] |
| 655 |
); |
| 656 |
} elseif ( count( $args ) > $max ) { |
| 657 |
throw new UserVisibleException( |
| 658 |
'toomanyargs', |
| 659 |
$position, |
| 660 |
[ $func, $max, count( $args ) ] |
| 661 |
); |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
private function isReservedIdentifier( string $name ): bool { |
| 673 |
return $this->keywordsManager->varExists( $name ) || |
| 674 |
array_key_exists( $name, FilterEvaluator::FUNCTIONS ) || |
| 675 |
|
| 676 |
|
| 677 |
in_array( $name, AbuseFilterTokenizer::KEYWORDS, true ); |
| 678 |
} |
| 679 |
} |