Skip to content

Commit 9b7ba5b

Browse files
committed
Parser: Make attribute parsing possessive
Bug introduced in #11369 Someone discovered high CPU usage due to catastrophic backtracking on an invalid block comment delimiter. The following input crashed the parser on the server: ```html <!-- wp:block {"a":0} / --> ``` The optimization introduced in #11369 ended up opening a place for backtracking that shouldn't be there. In this patch we're grouping the attribute parsing section of the tokenizing RegExp pattern so that we can make the group itself _possessive_ so that we abort any backtracking.
1 parent c446d06 commit 9b7ba5b

File tree

1 file changed

+6
-1
lines changed
  • packages/block-serialization-default-parser

1 file changed

+6
-1
lines changed

packages/block-serialization-default-parser/parser.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,18 @@ function next_token() {
373373
* match back in PHP to see which one it was.
374374
*/
375375
$has_match = preg_match(
376-
'/<!--\s+(?<closer>\/)?wp:(?<namespace>[a-z][a-z0-9_-]*\/)?(?<name>[a-z][a-z0-9_-]*)\s+(?<attrs>{(?:[^}]+|}+(?=})|(?!}\s+-->).)*?}\s+)?(?<void>\/)?-->/s',
376+
'/<!--\s+(?<closer>\/)?wp:(?<namespace>[a-z][a-z0-9_-]*\/)?(?<name>[a-z][a-z0-9_-]*)\s+(?<attrs>{(?:(?:[^}]+|}+(?=})|(?!}\s+-->).)*+)?}\s+)?(?<void>\/)?-->/s',
377377
$this->document,
378378
$matches,
379379
PREG_OFFSET_CAPTURE,
380380
$this->offset
381381
);
382382

383+
// if we get here we probably have catastrophic backtracking or out-of-memory in the PCRE
384+
if ( false === $has_match ) {
385+
return array( 'no-more-tokens', null, null, null, null );
386+
}
387+
383388
// we have no more tokens
384389
if ( 0 === $has_match ) {
385390
return array( 'no-more-tokens', null, null, null, null );

0 commit comments

Comments
 (0)