Skip to content

Commit

Permalink
Closes #6661: safeguard exclusions for non valid path (#7254)
Browse files Browse the repository at this point in the history
Co-authored-by: Khadreal <[email protected]>
Co-authored-by: WordPressFan <[email protected]>
  • Loading branch information
3 people authored Feb 14, 2025
1 parent b3c6752 commit 0ee63da
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 10 deletions.
44 changes: 34 additions & 10 deletions inc/Engine/Media/AboveTheFold/Frontend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ private function preload_lcp( $html, $row ) {
$title = $matches[0];
$preload = $title;

if ( ! $this->is_valid_data( $row->lcp ) ) {
return $html;
}

$lcp = json_decode( $row->lcp );

$preload .= $this->preload_tag( $lcp );
Expand Down Expand Up @@ -198,13 +202,13 @@ public function add_exclusions( $exclusions ): array {
return $exclusions;
}

if ( $row->lcp && 'not found' !== $row->lcp ) {
if ( $row->lcp && 'not found' !== $row->lcp && $this->is_valid_data( $row->lcp ) ) {
$lcp = $this->generate_lcp_link_tag_with_sources( json_decode( $row->lcp ) );
$lcp = $lcp['sources'];
$lcp = $this->get_path_for_exclusion( $lcp );
}

if ( $row->viewport && 'not found' !== $row->viewport ) {
if ( $row->viewport && 'not found' !== $row->viewport && $this->is_valid_data( $row->viewport ) ) {
$atf = $this->get_atf_sources( json_decode( $row->viewport ) );
$atf = $this->get_path_for_exclusion( $atf );
}
Expand All @@ -217,22 +221,42 @@ public function add_exclusions( $exclusions ): array {
return $exclusions;
}

/**
* Check if lcp/viewport is valid
*
* @param string $data The lcp/viewport data.
*
* @return bool
*/
private function is_valid_data( string $data ): bool {
return ! empty( json_decode( $data, true ) );
}

/**
* Get only the url path to exclude.
*
* @param array $exclusions Array of exclusions.
* @return array
*/
private function get_path_for_exclusion( array $exclusions ): array {
$exclusions = array_map(
function ( $exclusion ) {
$exclusion = wp_parse_url( $exclusion );
return ltrim( $exclusion['path'], '/' );
},
$exclusions
);
$sanitized_exclusions = [];

return $exclusions;
foreach ( $exclusions as $exclusion ) {
if ( empty( $exclusion ) ) {
continue;
}

$path = wp_parse_url( $exclusion, PHP_URL_PATH );
$path = ! empty( $path ) ? ltrim( $path, '/' ) : '';

if ( empty( $path ) ) {
continue;
}

$sanitized_exclusions[] = $path;
}

return $sanitized_exclusions;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,123 @@
'foobar.jpg',
],
],
'testShouldReturnEmptyStringWhenUrlNotValid' => [
'config' => [
'filter' => true,
'wp' => (object) [
'request' => '',
],
'url' => 'http://example.org',
'is_mobile' => false,
'row' => (object) [
'lcp' => json_encode( (object) [
'type' => 'img',
'src' => ':',
] ),
'viewport' => json_encode( [
0 => (object) [
'type' => 'img',
'src' => 'https://example.com/foobar.jpg',
],
] ),
],
'cache_mobile' => 0,
'do_caching_mobile_files' => 0,
'wp_is_mobile' => false,
],
'exclusions' => [
'foo',
],
'expected' => [
'foo',
'foobar.jpg',
],
],
'testShouldReturnEmptyLcpNotValid' => [
'config' => [
'filter' => true,
'wp' => (object) [
'request' => '',
],
'url' => 'http://example.org',
'is_mobile' => false,
'row' => (object) [
'lcp' => '[]',
'viewport' => json_encode( [
0 => (object) [
'type' => 'img',
'src' => 'https://example.com/foobar.jpg',
],
] ),
],
'cache_mobile' => 0,
'do_caching_mobile_files' => 0,
'wp_is_mobile' => false,
],
'exclusions' => [
'foo',
],
'expected' => [
'foo',
'foobar.jpg',
],
],
'testShouldReturnEmptyLcpNotValid2' => [
'config' => [
'filter' => true,
'wp' => (object) [
'request' => '',
],
'url' => 'http://example.org',
'is_mobile' => false,
'row' => (object) [
'lcp' => 'null',
'viewport' => json_encode( [
0 => (object) [
'type' => 'img',
'src' => 'https://example.com/foobar.jpg',
],
] ),
],
'cache_mobile' => 0,
'do_caching_mobile_files' => 0,
'wp_is_mobile' => false,
],
'exclusions' => [
'foo',
],
'expected' => [
'foo',
'foobar.jpg',
],
],
'testShouldReturnEmptyLcpNotValid3' => [
'config' => [
'filter' => true,
'wp' => (object) [
'request' => '',
],
'url' => 'http://example.org',
'is_mobile' => false,
'row' => (object) [
'lcp' => '{}',
'viewport' => json_encode( [
0 => (object) [
'type' => 'img',
'src' => 'https://example.com/foobar.jpg',
],
] ),
],
'cache_mobile' => 0,
'do_caching_mobile_files' => 0,
'wp_is_mobile' => false,
],
'exclusions' => [
'foo',
],
'expected' => [
'foo',
'foobar.jpg',
],
],
];

0 comments on commit 0ee63da

Please sign in to comment.