-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a PHPStan rule to detect invalid patterns passed to composer/pcre…
… methods
- Loading branch information
Showing
8 changed files
with
340 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Composer\Pcre\PHPStan; | ||
|
||
use Composer\Pcre\Preg; | ||
use Composer\Pcre\Regex; | ||
use Composer\Pcre\PcreException; | ||
use Nette\Utils\RegexpException; | ||
use Nette\Utils\Strings; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function in_array; | ||
use function sprintf; | ||
|
||
/** | ||
* Copy of PHPStan's RegularExpressionPatternRule | ||
* | ||
* @implements Rule<StaticCall> | ||
*/ | ||
class InvalidRegexPatternRule implements Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return StaticCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$patterns = $this->extractPatterns($node, $scope); | ||
|
||
$errors = []; | ||
foreach ($patterns as $pattern) { | ||
$errorMessage = $this->validatePattern($pattern); | ||
if ($errorMessage === null) { | ||
continue; | ||
} | ||
|
||
$errors[] = RuleErrorBuilder::message(sprintf('Regex pattern is invalid: %s', $errorMessage))->identifier('regexp.pattern')->build(); | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
private function extractPatterns(StaticCall $node, Scope $scope): array | ||
{ | ||
if (!$node->class instanceof FullyQualified) { | ||
return []; | ||
} | ||
$isRegex = $node->class->toString() === Regex::class; | ||
$isPreg = $node->class->toString() === Preg::class; | ||
if (!$isRegex && !$isPreg) { | ||
return []; | ||
} | ||
if (!$node->name instanceof Node\Identifier || !Preg::isMatch('{^(match|isMatch|grep|replace|split)}', $node->name->name)) { | ||
return []; | ||
} | ||
|
||
$functionName = $node->name->name; | ||
if (!isset($node->getArgs()[0])) { | ||
return []; | ||
} | ||
|
||
$patternNode = $node->getArgs()[0]->value; | ||
$patternType = $scope->getType($patternNode); | ||
|
||
$patternStrings = []; | ||
|
||
foreach ($patternType->getConstantStrings() as $constantStringType) { | ||
if ($functionName === 'replaceCallbackArray') { | ||
continue; | ||
} | ||
|
||
$patternStrings[] = $constantStringType->getValue(); | ||
} | ||
|
||
foreach ($patternType->getConstantArrays() as $constantArrayType) { | ||
if ( | ||
in_array($functionName, [ | ||
'replace', | ||
'replaceCallback', | ||
], true) | ||
) { | ||
foreach ($constantArrayType->getValueTypes() as $arrayKeyType) { | ||
foreach ($arrayKeyType->getConstantStrings() as $constantString) { | ||
$patternStrings[] = $constantString->getValue(); | ||
} | ||
} | ||
} | ||
|
||
if ($functionName !== 'replaceCallbackArray') { | ||
continue; | ||
} | ||
|
||
foreach ($constantArrayType->getKeyTypes() as $arrayKeyType) { | ||
foreach ($arrayKeyType->getConstantStrings() as $constantString) { | ||
$patternStrings[] = $constantString->getValue(); | ||
} | ||
} | ||
} | ||
|
||
return $patternStrings; | ||
} | ||
|
||
private function validatePattern(string $pattern): ?string | ||
{ | ||
try { | ||
$msg = null; | ||
$prev = set_error_handler(function (int $severity, string $message, string $file) use (&$msg): bool { | ||
$msg = preg_replace("#^preg_match(_all)?\\(.*?\\): #", '', $message); | ||
|
||
return true; | ||
}); | ||
|
||
if ($pattern === '') { | ||
return 'Empty string is not a valid regular expression'; | ||
} | ||
|
||
Preg::match($pattern, ''); | ||
if ($msg !== null) { | ||
return $msg; | ||
} | ||
} catch (PcreException $e) { | ||
if ($e->getCode() === PREG_INTERNAL_ERROR && $msg !== null) { | ||
return $msg; | ||
} | ||
|
||
return preg_replace('{.*? failed executing ".*": }', '', $e->getMessage()); | ||
} finally { | ||
restore_error_handler(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of composer/pcre. | ||
* | ||
* (c) Composer <https://github.com/composer> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Composer\Pcre\PHPStanTests; | ||
|
||
use PHPStan\Testing\RuleTestCase; | ||
use Composer\Pcre\PHPStan\InvalidRegexPatternRule; | ||
use PHPStan\Type\Php\RegexArrayShapeMatcher; | ||
|
||
/** | ||
* Run with "vendor/bin/phpunit --testsuite phpstan" | ||
* | ||
* This is excluded by default to avoid side effects with the library tests | ||
* | ||
* @extends RuleTestCase<InvalidRegexPatternRule> | ||
*/ | ||
class InvalidRegexPatternRuleTest extends RuleTestCase | ||
{ | ||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
return new InvalidRegexPatternRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$missing = PHP_VERSION_ID < 70300 ? ')' : 'closing parenthesis'; | ||
|
||
$this->analyse([__DIR__ . '/fixtures/invalid-patterns.php'], [ | ||
[ | ||
'Regex pattern is invalid: Compilation failed: missing '.$missing.' at offset 1', | ||
11, | ||
], | ||
[ | ||
'Regex pattern is invalid: Compilation failed: missing '.$missing.' at offset 1', | ||
13, | ||
], | ||
[ | ||
'Regex pattern is invalid: Compilation failed: missing '.$missing.' at offset 1', | ||
15, | ||
], | ||
[ | ||
'Regex pattern is invalid: Compilation failed: missing '.$missing.' at offset 1', | ||
17, | ||
], | ||
[ | ||
'Regex pattern is invalid: Compilation failed: missing '.$missing.' at offset 1', | ||
19, | ||
], | ||
[ | ||
'Regex pattern is invalid: Compilation failed: missing '.$missing.' at offset 1', | ||
21, | ||
], | ||
]); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
'phar://' . __DIR__ . '/../../vendor/phpstan/phpstan/phpstan.phar/conf/bleedingEdge.neon', | ||
__DIR__ . '/../../extension.neon', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace PregMatchShapes; | ||
|
||
use Composer\Pcre\Preg; | ||
use Composer\Pcre\Regex; | ||
use function PHPStan\Testing\assertType; | ||
|
||
function doMatch(string $s): void | ||
{ | ||
Preg::match('/(/i', $s, $matches); | ||
|
||
Regex::isMatch('/(/i', $s); | ||
|
||
Preg::grep('/(/i', [$s]); | ||
|
||
Preg::replaceCallback('/(/i', function ($match) { return ''; }, $s); | ||
|
||
Preg::replaceCallback(['/(/i', '{}'], function ($match) { return ''; }, $s); | ||
|
||
Preg::replaceCallbackArray(['/(/i' => function ($match) { return ''; }], $s); | ||
} |
Oops, something went wrong.