Skip to content

Commit

Permalink
Use alternative way of stripping appended HTML comments
Browse files Browse the repository at this point in the history
Co-authored-by: Alain Schlesser <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
  • Loading branch information
3 people committed Mar 31, 2021
1 parent b7c7e29 commit 0196441
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
17 changes: 16 additions & 1 deletion includes/validation/class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1848,7 +1848,22 @@ public static function validate_url( $url ) {
$response = ltrim( $response );

// Strip HTML comments that may have been injected at the end of the response (e.g. by a caching plugin).
$response = preg_replace( '/}\s*?<!--.*?-->\s*$/s', '}', $response );
while ( ! empty( $response ) ) {
$response = rtrim( $response );
$length = strlen( $response );

if ( $length < 3 || '-' !== $response[ $length - 3 ] || '-' !== $response[ $length - 2 ] || '>' !== $response[ $length - 1 ] ) {
break;
}

$start = strrpos( $response, '<!--' );

if ( false === $start ) {
break;
}

$response = substr( $response, 0, $start );
}

if ( '' === $response ) {
return new WP_Error( 'white_screen_of_death' );
Expand Down
11 changes: 11 additions & 0 deletions tests/php/validation/test-class-amp-validation-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2099,6 +2099,17 @@ public function get_validation_errors() {
],
"\n<!-- Generated by greatness! -->\n",
],
'error with multiple comments at end' => [
[
[
'code' => 'example',
],
],
'<!-- generated 2 seconds ago -->
<!-- generated in 1.134 seconds -->
<!-- served from batcache in 0.003 seconds -->
<!-- expires in 298 seconds -->',
],
'error with multi-line comment at end' => [
[
[
Expand Down

0 comments on commit 0196441

Please sign in to comment.