Skip to content

Commit bdef9de

Browse files
committed
HTML API: Fix an infinite loop in certain unclosed SCRIPT tags.
When the Tag Processor (or HTML Processor) attempts to parse certain incomplete script tags, the parser enters an infinite loop and will hang indefinitely. The conditions to reach this situation are: - Input HTML ends with an open script tag. - The final character of input is `-` or `<`. The infinite loop was caused by the parser-advancing increment not being called when two `||` OR conditions short-circuited. If the first condition was true, the `$at++` code was never reached. This path resolves the issue. Developed in WordPress#7128 Discussed in https://core.trac.wordpress.org/ticket/61810 Follow-up to [55203]. Props: dmsnell, jonsurrell. Fixes #61810. git-svn-id: https://develop.svn.wordpress.org/trunk@58845 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0c46e2a commit bdef9de

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/wp-includes/html-api/class-wp-html-tag-processor.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,8 +1431,15 @@ private function skip_script_data(): bool {
14311431
continue;
14321432
}
14331433

1434-
// Everything of interest past here starts with "<".
1435-
if ( $at + 1 >= $doc_length || '<' !== $html[ $at++ ] ) {
1434+
if ( $at + 1 >= $doc_length ) {
1435+
return false;
1436+
}
1437+
1438+
/*
1439+
* Everything of interest past here starts with "<".
1440+
* Check this character and advance position regardless.
1441+
*/
1442+
if ( '<' !== $html[ $at++ ] ) {
14361443
continue;
14371444
}
14381445

tests/phpunit/tests/html-api/wpHtmlTagProcessor.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,4 +2875,32 @@ public function insert_after( $new_html ) {
28752875
'Should have properly applied the update from in front of the cursor.'
28762876
);
28772877
}
2878+
2879+
/**
2880+
* Test an infinite loop bugfix in incomplete script tag parsing.
2881+
*
2882+
* @small
2883+
*
2884+
* @ticket 61810
2885+
*/
2886+
public function test_script_tag_processing_no_infinite_loop_final_dash() {
2887+
$processor = new WP_HTML_Tag_Processor( '<script>-' );
2888+
2889+
$this->assertFalse( $processor->next_tag() );
2890+
$this->assertTrue( $processor->paused_at_incomplete_token() );
2891+
}
2892+
2893+
/**
2894+
* Test an infinite loop bugfix in incomplete script tag parsing.
2895+
*
2896+
* @small
2897+
*
2898+
* @ticket 61810
2899+
*/
2900+
public function test_script_tag_processing_no_infinite_loop_final_left_angle_bracket() {
2901+
$processor = new WP_HTML_Tag_Processor( '<script><' );
2902+
2903+
$this->assertFalse( $processor->next_tag() );
2904+
$this->assertTrue( $processor->paused_at_incomplete_token() );
2905+
}
28782906
}

0 commit comments

Comments
 (0)