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

Fix: Prevent infinite loop in Tag Processor in certain truncated documents #45537

Merged
merged 1 commit into from
Nov 4, 2022
Merged
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
45 changes: 36 additions & 9 deletions lib/experimental/html/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ private function skip_rcdata( $tag_name ) {

$at = $this->parsed_bytes;

while ( true ) {
while ( false !== $at && $at < $doc_length ) {
$at = strpos( $this->html, '</', $at );

// If we have no possible tag closer then fail.
Expand Down Expand Up @@ -494,6 +494,8 @@ private function skip_rcdata( $tag_name ) {
return;
}
}

return false;
}

/**
Expand All @@ -507,7 +509,7 @@ private function skip_script_data() {
$doc_length = strlen( $html );
$at = $this->parsed_bytes;

while ( $at < $doc_length ) {
while ( false !== $at && $at < $doc_length ) {
$at += strcspn( $html, '-<', $at );

/*
Expand Down Expand Up @@ -613,6 +615,8 @@ private function skip_script_data() {

++$at;
}

return false;
}

/**
Expand All @@ -623,10 +627,11 @@ private function skip_script_data() {
private function parse_next_tag() {
$this->after_tag();

$html = $this->html;
$at = $this->parsed_bytes;
$html = $this->html;
$doc_length = strlen( $html );
$at = $this->parsed_bytes;

while ( true ) {
while ( false !== $at && $at < $doc_length ) {
$at = strpos( $html, '<', $at );
if ( false === $at ) {
return false;
Expand Down Expand Up @@ -663,7 +668,12 @@ private function parse_next_tag() {
'-' === $html[ $at + 2 ] &&
'-' === $html[ $at + 3 ]
) {
$at = strpos( $html, '-->', $at + 4 ) + 3;
$closer_at = strpos( $html, '-->', $at + 4 );
if ( false === $closer_at ) {
return false;
}

$at = $closer_at + 3;
continue;
}

Expand All @@ -680,7 +690,12 @@ private function parse_next_tag() {
'A' === $html[ $at + 7 ] &&
'[' === $html[ $at + 8 ]
) {
$at = strpos( $html, ']]>', $at + 9 ) + 3;
$closer_at = strpos( $html, ']]>', $at + 9 );
if ( false === $closer_at ) {
return false;
}

$at = $closer_at + 3;
continue;
}

Expand All @@ -699,7 +714,12 @@ private function parse_next_tag() {
'P' === strtoupper( $html[ $at + 7 ] ) &&
'E' === strtoupper( $html[ $at + 8 ] )
) {
$at = strpos( $html, '>', $at + 9 ) + 1;
$closer_at = strpos( $html, '>', $at + 9 );
if ( false === $closer_at ) {
return false;
}

$at = $closer_at + 1;
continue;
}

Expand All @@ -716,12 +736,19 @@ private function parse_next_tag() {
* https://html.spec.whatwg.org/multipage/parsing.html#tag-open-state
*/
if ( '?' === $html[ $at + 1 ] ) {
$at = strpos( $html, '>', $at + 2 ) + 1;
$closer_at = strpos( $html, '>', $at + 2 );
if ( false === $closer_at ) {
return false;
}

$at = $closer_at + 1;
continue;
}

++$at;
}

return false;
}

/**
Expand Down