Repository navigation
Expand file tree
/
Copy pathRegexPatternRule.php
More file actions
432 lines (371 loc) · 16.4 KB
/
Copy pathRegexPatternRule.php
File metadata and controls
432 lines (371 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<?php
declare(strict_types=1);
/*
* This file is part of the PHPRegex package.
*
* (c) Younes ENNAJI <younes.ennaji.pro@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPRegex\PHPStan;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\PrettyPrinter\Standard;
use PHPRegex\Automata\TrivialMatchClassifier;
use PHPRegex\Parser\Analysis\GroupNumbering;
use PHPRegex\Parser\Analysis\GroupNumberingCollector;
use PHPRegex\Parser\Exception\ExceptionInterface;
use PHPRegex\Parser\Exception\InvalidRegexOptionException;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Type;
/**
* Reports, in `preg_*` calls, the patterns the targeted PHP and PCRE2 refuse
* while the engine running PHPStan compiles them (PHPStan core reports what
* the running engine refuses); lint, ReDoS risks and optimizations on demand.
*
* @implements Rule<FuncCall>
*/
final class RegexPatternRule implements Rule
{
public const IDENTIFIER_INVALID_FOR_TARGET = 'regex.invalidForTarget';
public const IDENTIFIER_REDOS = 'regex.redos';
public const IDENTIFIER_OPTIMIZATION = 'regex.optimization';
public const IDENTIFIER_TRIVIAL_MATCH = 'regex.trivialMatch';
/**
* One attempt proven linear, the unanchored search that retries it
* quadratic.
*/
public const IDENTIFIER_REDOS_SEARCH = 'regex.redos.search';
/**
* A constant replacement of preg_replace() or preg_filter() refers to a
* group the pattern does not have; always reported.
*/
public const IDENTIFIER_REPLACEMENT_UNDEFINED_GROUP = 'regex.replacement.undefinedGroup';
/**
* The preg functions the rule reads, with the position and name of their
* subject parameter; the pattern comes first in each.
*/
private const PREG_FUNCTION_MAP = [
'preg_match' => [1, 'subject'],
'preg_match_all' => [1, 'subject'],
'preg_replace' => [2, 'subject'],
'preg_replace_callback' => [2, 'subject'],
'preg_split' => [1, 'subject'],
'preg_grep' => [1, 'array'],
'preg_filter' => [2, 'subject'],
'preg_replace_callback_array' => [1, 'subject'],
];
private readonly PatternChecker $checker;
private ?TrivialMatchClassifier $trivialMatches = null;
/**
* @param array<string, mixed> $config the "phpRegex" parameter: "phpVersion", "pcreVersion" and
* "checks" ("lint", "redos", "optimizations"), every key optional
* @param PhpVersion|null $phpVersion the PHP version PHPStan analyses the project for: patterns are
* judged for it, with the PCRE2 it bundles, unless "phpVersion"
* is "runtime" or names a version, and "pcreVersion" a release
* @param mixed $phpVersionRange PHPStan's "phpVersion" parameter: with {min, max}, a pattern is
* also validated at each later PHP up to max where a rule of the
* library changes, unless "phpVersion" names one version
*
* @throws InvalidRegexOptionException when "phpVersion", "pcreVersion" or "checks.redos.threshold" cannot be read
*/
public function __construct(array $config = [], ?PhpVersion $phpVersion = null, mixed $phpVersionRange = null)
{
$this->checker = new PatternChecker($config, $phpVersion, $phpVersionRange);
}
public function getNodeType(): string
{
return FuncCall::class;
}
/**
* @return array<IdentifierRuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
if (!$node instanceof FuncCall) {
return [];
}
if (!$node->name instanceof Name) {
return [];
}
$functionName = $node->name->toLowerString();
if (!isset(self::PREG_FUNCTION_MAP[$functionName])) {
return [];
}
$args = $node->getArgs();
$patternArg = self::argument($args, 0, 'pattern');
if (null === $patternArg) {
return [];
}
// A constant subject cannot carry an attack: the call backtracks the
// same way on every run, or never.
[$subjectPosition, $subjectName] = self::PREG_FUNCTION_MAP[$functionName];
$subjectArg = self::argument($args, $subjectPosition, $subjectName);
$constantSubject = null !== $subjectArg && $scope->getType($subjectArg)->isConstantValue()->yes();
if ('preg_replace_callback_array' === $functionName) {
return $this->processPregReplaceCallbackArray($patternArg, $scope, $node->getLine(), $functionName, $constantSubject);
}
// preg_match($pattern, $subject) alone, its result the only output.
$plainMatch = $this->checker->optimizationsEnabled() && 'preg_match' === $functionName && 2 === \count($args) && null !== $subjectArg;
$errors = [];
foreach ($scope->getType($patternArg)->getConstantStrings() as $constantString) {
$errors = array_merge(
$errors,
$this->checker->check($constantString->getValue(), $node->getLine(), $scope, $this->formatSource($functionName), $constantSubject),
);
if ($plainMatch) {
$errors = array_merge($errors, $this->trivialMatch($constantString->getValue(), $subjectArg, $node->getLine()));
}
}
$replacementArg = 'preg_replace' === $functionName || 'preg_filter' === $functionName ? self::argument($args, 1, 'replacement') : null;
if (null !== $replacementArg) {
$errors = array_merge($errors, $this->undefinedReplacementGroups($scope->getType($patternArg), $scope->getType($replacementArg), $node->getLine(), $functionName));
}
return $errors;
}
public function isOptimizationFormatSafe(string $original, string $optimized): bool
{
return $this->checker->isOptimizationFormatSafe($original, $optimized);
}
/**
* A preg_match() a string function answers alike, as the automata prove.
*
* @return list<IdentifierRuleError>
*/
private function trivialMatch(string $pattern, Expr $subject, int $lineNumber): array
{
if (!$this->checker->runningEngineCompiles($pattern)) {
return [];
}
$match = ($this->trivialMatches ??= new TrivialMatchClassifier($this->checker->parser()))->classify($pattern);
if (null === $match) {
return [];
}
return [RuleErrorBuilder::message(\sprintf(
'preg_match() with %s is %s.',
PatternChecker::displayPattern($pattern),
$match->phpExpression((new Standard())->prettyPrintExpr($subject)),
))
->line($lineNumber)
->identifier(self::IDENTIFIER_TRIVIAL_MATCH)
->tip('Both say yes for exactly the same subjects, as the automata prove, and the function needs no regex engine; preg_match() returns 1 or 0 where the function returns true or false.')
->build()];
}
/**
* The references of a constant replacement to a group the pattern does
* not have, and the "${name}" PHP never substitutes. An array of patterns
* takes the replacement of the same position in an array of
* replacements (none: the empty string), or the one string replacement.
*
* When the pattern and the replacement both vary, they may vary together
* (a ternary on one condition, two maps read with one key), which the
* types do not keep: a reference is then reported only if no pattern
* the call may take defines its group. A single pattern or a single
* replacement meets every value of the other, and is fully checked.
*
* @return list<IdentifierRuleError>
*/
private function undefinedReplacementGroups(Type $patternType, Type $replacementType, int $lineNumber, string $functionName): array
{
$stringReplacements = self::constantStrings($replacementType);
/** @var list<array{list<string>, list<string>}> $pairs the patterns a call may take, with the replacements they may take */
$pairs = [[self::constantStrings($patternType), $stringReplacements]];
$replacementArrays = $replacementType->getConstantArrays();
// A key that may be missing moves every entry after it.
$certainPositions = [] === array_filter($replacementArrays, static fn (ConstantArrayType $array): bool => [] !== $array->getOptionalKeys());
/** @var array<int, list<string>> $positions the patterns each position may hold */
$positions = [];
foreach ($patternType->getConstantArrays() as $patterns) {
if (!$certainPositions || [] !== $patterns->getOptionalKeys()) {
continue;
}
foreach ($patterns->getValueTypes() as $position => $patternValue) {
$positions[$position] = [...$positions[$position] ?? [], ...self::constantStrings($patternValue)];
}
}
foreach ($positions as $position => $patterns) {
$replacements = $stringReplacements;
foreach ($replacementArrays as $replacementArray) {
$replacements = [...$replacements, ...self::constantStrings($replacementArray->getValueTypes()[$position] ?? null)];
}
$pairs[] = [$patterns, $replacements];
}
$errors = [];
foreach ($pairs as [$patterns, $replacements]) {
$patterns = array_values(array_unique($patterns));
$replacements = array_values(array_unique($replacements));
if ([] === $replacements) {
continue;
}
$correlated = \count($patterns) > 1 && \count($replacements) > 1;
/** @var list<array{string, GroupNumbering}> $numberings */
$numberings = [];
foreach ($patterns as $pattern) {
$numbering = $this->groupNumbering($pattern);
if (null !== $numbering) {
$numberings[] = [$pattern, $numbering];
} elseif ($correlated) {
// A pattern that cannot be read may define any group.
continue 2;
}
}
foreach ($replacements as $replacement) {
foreach (ReplacementReferences::of($replacement) as $reference) {
$found = [];
foreach ($numberings as [$pattern, $numbering]) {
$error = self::undefinedReplacementGroup($reference, $numbering, $pattern, $functionName);
if (null !== $error) {
$found[] = $error;
} elseif ($correlated) {
// One of the patterns defines it.
continue 2;
}
}
foreach ($found as $error) {
if (isset($errors[$error[0]])) {
continue;
}
$builder = RuleErrorBuilder::message($error[0])
->line($lineNumber)
->identifier(self::IDENTIFIER_REPLACEMENT_UNDEFINED_GROUP);
if (null !== $error[1]) {
$builder = $builder->tip($error[1]);
}
$errors[$error[0]] = $builder->build();
}
}
}
}
return array_values($errors);
}
/**
* The message and tip for a reference to a group the pattern does not
* have, null for a reference to one of its groups.
*
* @param array{raw: string, group: int|null, name: string|null} $reference
*
* @return array{string, string|null}|null
*/
private static function undefinedReplacementGroup(array $reference, GroupNumbering $numbering, string $pattern, string $functionName): ?array
{
$name = $reference['name'];
if (null !== $name) {
$numbers = $numbering->getNamedGroupNumbers($name);
return [
\sprintf('Replacement text %s is not a group reference: %s() reads group numbers only and leaves it in the result as written.', $reference['raw'], $functionName),
[] === $numbers
? \sprintf('%s has no group named "%s" either; refer to a group by its number, as in ${1}.', PatternChecker::displayPattern($pattern), $name)
: \sprintf('Write ${%d}, the number of the group "%s".', $numbers[0], $name),
];
}
$group = (int) $reference['group'];
$count = $numbering->maxGroupNumber;
if ($group <= $count) {
return null;
}
$message = \sprintf(
'Replacement reference %s names group %d, but %s has %s: %s() substitutes an empty string.',
$reference['raw'],
$group,
PatternChecker::displayPattern($pattern),
match ($count) {
0 => 'no capturing group',
1 => '1 capturing group',
default => $count.' capturing groups',
},
$functionName,
);
// "$10" is group 10: group 1 followed by a "0" is written "${1}0".
if ($group >= 10 && intdiv($group, 10) <= $count && !str_starts_with($reference['raw'], '${')) {
return [$message, \sprintf('Two digits are read after "%1$s": write ${%2$d}%3$d for group %2$d followed by "%3$d".', $reference['raw'][0], intdiv($group, 10), $group % 10)];
}
return [$message, null];
}
/**
* The group numbers of a pattern the running engine compiles, null for
* one it refuses: PHPStan core reports that one.
*/
private function groupNumbering(string $pattern): ?GroupNumbering
{
if (!$this->checker->runningEngineCompiles($pattern)) {
return null;
}
try {
return (new GroupNumberingCollector())->collect($this->checker->parser()->parse($pattern));
} catch (ExceptionInterface) {
return null;
}
}
/**
* @return list<string>
*/
private static function constantStrings(?Type $type): array
{
$strings = [];
foreach ($type?->getConstantStrings() ?? [] as $constantString) {
$strings[] = $constantString->getValue();
}
return $strings;
}
/**
* The argument passed for a parameter, by name when the call names its
* arguments, by position otherwise; null when it cannot be told.
*
* @param array<Arg> $args
*/
private static function argument(array $args, int $position, string $name): ?Expr
{
foreach ($args as $index => $arg) {
if (null !== $arg->name) {
if ($name === $arg->name->toString()) {
return $arg->value;
}
continue;
}
if ($arg->unpack) {
return null;
}
if ($position === $index) {
return $arg->value;
}
}
return null;
}
/**
* @return array<IdentifierRuleError>
*/
private function processPregReplaceCallbackArray(Node $arrayNode, Scope $scope, int $lineNumber, string $functionName, bool $constantSubject): array
{
if (!$arrayNode instanceof Array_) {
return [];
}
$errors = [];
foreach ($arrayNode->items as $item) {
if (!$item instanceof ArrayItem || !$item->key instanceof String_) {
continue;
}
$pattern = $item->key->value;
$errors = array_merge(
$errors,
$this->checker->check($pattern, $lineNumber, $scope, $this->formatSource($functionName), $constantSubject),
);
}
return $errors;
}
private function formatSource(string $functionName): string
{
return 'php:'.$functionName.'()';
}
}