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

Closes #6661: safeguard exclusions for non valid path #7254

Merged
merged 18 commits into from
Feb 14, 2025
Merged
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
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',
],
],
];
Loading