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
Changes from 14 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
36 changes: 26 additions & 10 deletions inc/Engine/Media/AboveTheFold/Frontend/Controller.php
Original file line number Diff line number Diff line change
@@ -88,6 +88,10 @@ private function preload_lcp( $html, $row ) {
$title = $matches[0];
$preload = $title;

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

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

$preload .= $this->preload_tag( $lcp );
@@ -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_non_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_non_valid_data( $row->viewport ) ) {
$atf = $this->get_atf_sources( json_decode( $row->viewport ) );
$atf = $this->get_path_for_exclusion( $atf );
}
@@ -217,22 +221,34 @@ 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_non_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 ) {
$parsed = wp_parse_url( $exclusion );
if ( isset( $parsed['path'] ) ) {
$sanitized_exclusions[] = ltrim( $parsed['path'], '/' );
}
}

return $sanitized_exclusions;
}

/**
Original file line number Diff line number Diff line change
@@ -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',
],
],
];

Unchanged files with check annotations Beta

protected $myrc4;
protected function readfile_chunked( $file, $retbytes=true, $myrc4, $cache_to_file='' ) {

Check warning on line 112 in tests/Fixtures/inc/ThirdParty/Plugins/PDFEmbedder/core/secure/uploads.php

GitHub Actions / WP latest with PHP 8.3 on ubuntu-latest.

Optional parameter $retbytes declared before required parameter $myrc4 is implicitly treated as a required parameter

Check warning on line 112 in tests/Fixtures/inc/ThirdParty/Plugins/PDFEmbedder/core/secure/uploads.php

GitHub Actions / WP latest with PHP 8.0 on ubuntu-latest.

Required parameter $myrc4 follows optional parameter $retbytes

Check warning on line 112 in tests/Fixtures/inc/ThirdParty/Plugins/PDFEmbedder/core/secure/uploads.php

GitHub Actions / WP latest with PHP 8.1 on ubuntu-latest.

Optional parameter $retbytes declared before required parameter $myrc4 is implicitly treated as a required parameter

Check warning on line 112 in tests/Fixtures/inc/ThirdParty/Plugins/PDFEmbedder/core/secure/uploads.php

GitHub Actions / WP latest with PHP 8.2 on ubuntu-latest.

Optional parameter $retbytes declared before required parameter $myrc4 is implicitly treated as a required parameter
$chunksize = 1024 * 1024;
$buffer = '';