Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser: Use a non-greedy matcher to extract the block args #11355

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/block-serialization-default-parser/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ function proceed() {
$leading_html_start = $start_offset > $this->offset ? $this->offset : null;

switch ( $token_type ) {
case 'preg-failed':
/*
* Things have gone badly, we crashed PCRE.
* Log the error message, then let the no-more-tokens handler close things out as best we can.
*/
$message = sprintf(
'The data passed to the parser caused a PCRE error: %s',
array_flip( get_defined_constants( true )['pcre'])[ $block_name ]
);
_doing_it_wrong( 'WP_Block_Parser::parse', $message, '5.0.0' );

// Deliberate fall-through.
case 'no-more-tokens':
// if not in a block then flush output
if ( 0 === $stack_depth ) {
Expand Down Expand Up @@ -347,7 +359,7 @@ function next_token() {
* match back in PHP to see which one it was.
*/
$has_match = preg_match(
'/<!--\s+(?<closer>\/)?wp:(?<namespace>[a-z][a-z0-9_-]*\/)?(?<name>[a-z][a-z0-9_-]*)\s+(?<attrs>{(?:(?!}\s+-->).)+?}\s+)?(?<void>\/)?-->/s',
'/<!--\s+(?<closer>\/)?wp:(?<namespace>[a-z][a-z0-9_-]*\/)?(?<name>[a-z][a-z0-9_-]*)\s+(?<attrs>{.+?}\s+)?(?<void>\/)?-->/s',
$this->document,
$matches,
PREG_OFFSET_CAPTURE,
Expand All @@ -359,6 +371,11 @@ function next_token() {
return array( 'no-more-tokens', null, null, null, null );
}

// PREG failed for some reason.
if ( false === $has_match ) {
return array( 'preg-failed', preg_last_error(), null, null, null );
}

list( $match, $started_at ) = $matches[ 0 ];

$length = strlen( $match );
Expand Down
13 changes: 13 additions & 0 deletions phpunit/class-parsing-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,17 @@ function test_default_parser_output( $html_filename, $parsed_json_filename ) {
"File '$parsed_json_filename' does not match expected value"
);
}

function test_default_parser_doesnt_crash_on_long_args() {
require_once dirname( __FILE__ ) . '/../packages/block-serialization-default-parser/parser.php';

$long_string = str_repeat( 'a', 1000000 );
$html = '<!-- wp:core/this-is-a-fake-block {"foo":"' . $long_string . '"} /-->';

$parser = new WP_Block_Parser();
$result = $parser->parse( $html );

$this->assertArrayHasKey( 'foo', $result[0]['attrs'] );
$this->assertEquals( $long_string, $result[0]['attrs']['foo'] );
}
}