|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * |
| 5 | + * ____ _ _ __ __ _ __ __ ____ |
| 6 | + * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \ |
| 7 | + * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) | |
| 8 | + * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/ |
| 9 | + * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_| |
| 10 | + * |
| 11 | + * This program is free software: you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU Lesser General Public License as published by |
| 13 | + * the Free Software Foundation, either version 3 of the License, or |
| 14 | + * (at your option) any later version. |
| 15 | + * |
| 16 | + * @author PocketMine Team |
| 17 | + * @link http://www.pocketmine.net/ |
| 18 | + * |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +declare(strict_types=1); |
| 23 | + |
| 24 | +namespace pocketmine\phpstan\rules; |
| 25 | + |
| 26 | +use PhpParser\Node; |
| 27 | +use PhpParser\Node\Expr\FuncCall; |
| 28 | +use PhpParser\Node\Name; |
| 29 | +use PHPStan\Analyser\ArgumentsNormalizer; |
| 30 | +use PHPStan\Analyser\Scope; |
| 31 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 32 | +use PHPStan\Reflection\ReflectionProvider; |
| 33 | +use PHPStan\Rules\Rule; |
| 34 | +use PHPStan\Rules\RuleErrorBuilder; |
| 35 | +use function count; |
| 36 | + |
| 37 | +/** |
| 38 | + * @phpstan-implements Rule<FuncCall> |
| 39 | + */ |
| 40 | +final class ExplodeLimitRule implements Rule{ |
| 41 | + private ReflectionProvider $reflectionProvider; |
| 42 | + |
| 43 | + public function __construct( |
| 44 | + ReflectionProvider $reflectionProvider |
| 45 | + ){ |
| 46 | + $this->reflectionProvider = $reflectionProvider; |
| 47 | + } |
| 48 | + |
| 49 | + public function getNodeType() : string{ |
| 50 | + return FuncCall::class; |
| 51 | + } |
| 52 | + |
| 53 | + public function processNode(Node $node, Scope $scope) : array{ |
| 54 | + if(!$node->name instanceof Name){ |
| 55 | + return []; |
| 56 | + } |
| 57 | + |
| 58 | + if(!$this->reflectionProvider->hasFunction($node->name, $scope)){ |
| 59 | + return []; |
| 60 | + } |
| 61 | + |
| 62 | + $functionReflection = $this->reflectionProvider->getFunction($node->name, $scope); |
| 63 | + |
| 64 | + if($functionReflection->getName() !== 'explode'){ |
| 65 | + return []; |
| 66 | + } |
| 67 | + |
| 68 | + $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( |
| 69 | + $scope, |
| 70 | + $node->getArgs(), |
| 71 | + $functionReflection->getVariants(), |
| 72 | + $functionReflection->getNamedArgumentsVariants(), |
| 73 | + ); |
| 74 | + |
| 75 | + $normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node); |
| 76 | + |
| 77 | + if($normalizedFuncCall === null){ |
| 78 | + return []; |
| 79 | + } |
| 80 | + |
| 81 | + $count = count($normalizedFuncCall->getArgs()); |
| 82 | + if($count !== 3){ |
| 83 | + return [ |
| 84 | + RuleErrorBuilder::message('The $limit parameter of explode() must be set to prevent malicious client data wasting resources.') |
| 85 | + ->identifier("pocketmine.explode.limit") |
| 86 | + ->build() |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + return []; |
| 91 | + } |
| 92 | +} |
0 commit comments