Repository navigation
Expand file tree
/
Copy pathPatternLab.php
More file actions
381 lines (322 loc) · 14 KB
/
Copy pathPatternLab.php
File metadata and controls
381 lines (322 loc) · 14 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
<?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\PHPUnit;
use PHPRegex\Parser\Analysis\PatternInfo;
use PHPRegex\Parser\Analysis\PatternInfoAnalyzer;
use PHPRegex\Parser\Engine\PcreEngine;
use PHPRegex\Parser\Exception\RegexException;
use PHPRegex\Parser\RegexParser;
use PHPRegex\Redos\RedosAnalysis;
use PHPRegex\Redos\RedosAnalyzer;
use PHPRegex\Redos\RedosProof;
/**
* The machinery every regex assertion leans on: one lazily created parser
* held for the process, the no-JIT guarded spelling each match attempt runs
* under, the capture-expectation vocabulary, and the memoized ReDoS
* verdicts.
*
* The parser is held once because its cache is per instance, so a fresh
* parser per assertion would parse every pattern again. ReDoS verdicts are
* memoized per pattern; the analyzer is deterministic per release, so the
* memoization cannot be observed, only the budget spent twice is saved.
*
* @internal the assertions and their constraints are the public doors
*/
final class PatternLab
{
private static ?RegexParser $parser = null;
private static ?PcreEngine $engine = null;
/**
* @var array<string, string|null> the memoized failure text, a missing key an unrun verdict
*
* Uncapped by design: it grows one entry per distinct pattern a suite
* asserts linearity on (measured at roughly a kilobyte each), so it is
* bounded by the suite itself — a ceiling, never a leak
*/
private static array $linearFailures = [];
/**
* The parser every assertion reads patterns through, created on the
* first call and held for the process.
*/
public static function parser(): RegexParser
{
return self::$parser ??= RegexParser::create();
}
/**
* One guarded match attempt of the pattern against the subject, judged
* against the expectation vocabulary before any value is compared: a
* pattern that does not parse, an expectation key no group of the
* pattern answers to, or an engine refusal each answer as a failure
* text, never as a match result.
*
* @param array<int|string, mixed> $expected the capture expectation: true, false, a string value, or [value, byte offset], keyed by group number or name
*/
public static function run(string $pattern, string $subject, array $expected): MatchAttempt
{
try {
$node = self::parser()->parse($pattern);
} catch (RegexException $e) {
return new MatchAttempt(null, [], [], self::libraryFailure($e));
}
$expectation = null;
if ([] !== $expected) {
try {
$info = PatternInfoAnalyzer::analyzePattern(self::parser(), $pattern);
} catch (RegexException $e) {
return new MatchAttempt(null, [], [], self::libraryFailure($e));
}
// The trailing flags carry the whole-pattern n; an inline (?n)
// only narrows the message wording (the generic unknown-group
// refusal), never the numbers, which the views already model.
$judged = self::judge($expected, $info, str_contains($node->flags, 'n'));
if (\is_string($judged)) {
return new MatchAttempt(null, [], [], $judged);
}
$expectation = $judged;
}
$attempt = self::match($pattern, $subject);
if (false === $attempt['result']) {
return new MatchAttempt(null, [], [], $attempt['error']);
}
if (1 !== $attempt['result']) {
return new MatchAttempt(false, [], [], null);
}
// An empty expectation asks only whether the pattern matched, so
// nothing is judged and nothing is compared.
$actual = [];
if (null !== $expectation) {
foreach ($expected as $key => $spelling) {
[$value, $offset] = $attempt['groups'][$expectation->numbers[$key]] ?? [null, -1];
// Both boolean spellings read the one comparable fact,
// whether the group participated: 'false' expects it not to.
$actual[$key] = \is_bool($spelling) ? null !== $value
: (\is_string($spelling) ? $value : [$value, $offset]);
}
}
return new MatchAttempt(true, $expected, $actual, null);
}
/**
* Why the running engine refuses the pattern, or null when it compiles
* it: the truth of assertPatternCompiles() is the engine's alone.
*/
public static function compileFailure(string $pattern): ?string
{
self::$engine ??= new PcreEngine();
$error = self::$engine->compile($pattern);
return $error?->message;
}
/**
* Why the pattern cannot be called linear, or null when one match
* attempt is proven linear and the unanchored search carries no
* quadratic-cost witness. The first line of a failure carries the
* verdict's stable prefix: "proven unsafe:" for a held witness or a
* quadratic search, "could not analyze:" for an unproven one.
*
* Memoized per pattern for the process: the analyzer is deterministic
* per release, so the second call answers what the first did.
*/
public static function linearFailure(string $pattern): ?string
{
if (!\array_key_exists($pattern, self::$linearFailures)) {
self::$linearFailures[$pattern] = self::verdictFailure((new RedosAnalyzer(self::parser()))->analyze($pattern));
}
return self::$linearFailures[$pattern];
}
/**
* A value as the failure messages show it, spelled the same whatever
* PHPUnit release renders the assertion. The depth is capped because a
* refusal renders what the caller handed over, and a circular or merely
* deep spelling must meet its refusal, not the stack floor.
*/
public static function export(mixed $value, int $depth = 0): string
{
if (null === $value) {
return 'null';
}
if (\is_bool($value)) {
return true === $value ? 'true' : 'false';
}
if (\is_int($value) || \is_float($value)) {
return (string) $value;
}
if (\is_string($value)) {
return "'".str_replace(['\\', "'"], ['\\\\', "\\'"], $value)."'";
}
if (\is_array($value)) {
if ($depth >= 8) {
return '[…]';
}
$list = array_is_list($value);
$parts = [];
foreach ($value as $key => $part) {
$parts[] = ($list ? '' : self::export($key, $depth + 1).' => ').self::export($part, $depth + 1);
}
return '['.implode(', ', $parts).']';
}
return get_debug_type($value);
}
/**
* The one guarded match attempt, under PREG_UNMATCHED_AS_NULL and
* PREG_OFFSET_CAPTURE, so a participating group is [string value, int
* byte offset] and a non-participating one a null value at offset -1.
* A pattern the engine answers false for is an error, carried as one.
*
* @return array{result: int|false, groups: array<int|string, array{string|null, int}>, error: string|null}
*/
private static function match(string $pattern, string $subject): array
{
self::$engine ??= new PcreEngine();
$outcome = self::$engine->match($pattern, $subject, null, 0, \PREG_UNMATCHED_AS_NULL | \PREG_OFFSET_CAPTURE);
if (null === $outcome->matched) {
return ['result' => false, 'groups' => [], 'error' => $outcome->error];
}
/** @var array<int|string, array{string|null, int}> $groups */
$groups = $outcome->groups;
return ['result' => true === $outcome->matched ? 1 : 0, 'groups' => $groups, 'error' => null];
}
/**
* The expectation judged against the pattern's own groups: every key
* resolved to the group number it names, or the refusal that names what
* the pattern really has.
*
* @param array<int|string, mixed> $expected
*
* @return Expectation|string the expectation read, or the refusal text
*/
private static function judge(array $expected, PatternInfo $info, bool $noAutoCapture): Expectation|string
{
// Under n the engine keeps the NAMED groups capturing, renumbered
// from 1, and drops the un-named ones — and these views model
// exactly that, so names and numbers alike are judged against them.
$numbers = [];
foreach ($expected as $key => $spelling) {
if (\is_int($key) || ('' !== ($digitable = (string) $key) && \strlen($digitable) === strspn($digitable, '0123456789'))) {
$number = (int) $key;
if (0 > $number || $number > $info->captureCount) {
if ($noAutoCapture) {
return sprintf(
'the pattern has no group %d under the n modifier; only named groups capture under it, renumbered from 1. Its groups are %s.',
$number,
self::groupList($info),
);
}
return sprintf('the pattern has no group %d; its groups are %s.', $number, self::groupList($info));
}
$numbers[$key] = $number;
continue;
}
$owned = $info->names[$key] ?? null;
if (null === $owned) {
return sprintf("the pattern has no group named '%s'; its groups are %s.", $key, self::groupList($info));
}
if (\count($owned) > 1) {
return sprintf(
"the group name '%s' owns several group numbers (%s); assert by number instead.",
$key,
implode(', ', $owned),
);
}
$numbers[$key] = $owned[0];
}
foreach ($expected as $key => $spelling) {
if (true === $spelling || \is_string($spelling)) {
continue;
}
if (false === $spelling) {
if (0 === $numbers[$key]) {
return 'group 0 is the whole match: it participates whenever the pattern matches, so expecting it not to contradicts the match.';
}
continue;
}
if (\is_array($spelling)
&& 2 === \count($spelling)
&& \array_key_exists(0, $spelling)
&& \array_key_exists(1, $spelling)
&& \is_string($spelling[0])
&& \is_int($spelling[1])
) {
continue;
}
return sprintf(
'unknown expectation for %s: %s. Expected true (participated), false (did not participate), a string value, or [value, byte offset].',
\is_int($key) ? "group {$key}" : "group '{$key}'",
self::export($spelling),
);
}
return new Expectation($numbers, $expected);
}
/**
* The pattern's groups as a refusal lists them: the whole match, then
* every capture group by number, its names beside it when it has any.
*/
private static function groupList(PatternInfo $info): string
{
$names = [];
foreach ($info->names as $name => $owned) {
foreach ($owned as $number) {
$names[$number][] = $name;
}
}
$groups = ['0 (the whole match)'];
for ($number = 1; $number <= $info->captureCount; $number++) {
$groups[] = isset($names[$number])
? sprintf("%d '%s'", $number, implode('/', $names[$number]))
: (string) $number;
}
return implode(', ', $groups);
}
/**
* The failure text for a library exception: its own message, and the
* caret snippet it carries when it carries one.
*/
private static function libraryFailure(RegexException $e): string
{
$snippet = $e->snippet ?? '';
return '' === $snippet ? $e->getMessage() : $e->getMessage()."\n".$snippet;
}
/**
* The failure text for the ReDoS verdict: nothing when one attempt is
* proven linear with no quadratic search witness, else the verdict in
* the assertion's own words, led by its stable prefix.
*/
private static function verdictFailure(RedosAnalysis $verdict): ?string
{
if ($verdict->isProvenSafe() && null === $verdict->searchCost) {
return null;
}
if (null !== $verdict->searchCost) {
return sprintf(
'proven unsafe: quadratic unanchored search. One match attempt is linear, but an unanchored search retries it at every start of a run of %s, a cost the analyzer holds a degree-%d witness for.',
self::export($verdict->searchCost->run),
$verdict->searchCost->degree,
);
}
if (null !== $verdict->witness) {
return sprintf('proven unsafe: %s. A witness input is %s.', $verdict->headline(), $verdict->witness->render());
}
if (null !== $verdict->error) {
return sprintf('could not analyze: the analyzer returned an error: %s', self::cleanError($verdict->error));
}
if (RedosProof::BudgetExceeded === $verdict->proof) {
return 'could not analyze: the analysis budget was exceeded before any verdict was reached.';
}
return sprintf("could not analyze: %s; the pattern sits outside the analyzer's model.", $verdict->headline());
}
/**
* The analyzer's error text without its exception class name, which the
* verdict carries in front of the message itself.
*/
private static function cleanError(string $error): string
{
$cut = strpos($error, ': ');
return false === $cut || !str_contains(substr($error, 0, $cut), '\\') ? $error : substr($error, $cut + 2);
}
}