Skip to content

PHP Fatal Error in Controller::set_fetchpriority() when LCP image URL contains percent-encoded special characters (%23) #8735

Description

@johan-las

Describe the bug
The pages can crash with an HTTP 500 Fatal Error if the detected LCP image URL (especially external, CDN-hosted, or relative URLs) contains percent-encoded characters such as %23 (in query parameters, fragments, or filenames).

Fatal error :

PHP Warning:  preg_replace_callback(): Unknown modifier '0' in /wp-content/plugins/wp-rocket/inc/Engine/Media/AboveTheFold/Frontend/Controller.php on line 156
PHP Fatal error:  Uncaught TypeError: WP_Rocket\Engine\Media\AboveTheFold\Frontend\Controller::set_fetchpriority(): Return value must be of type string, null returned in /wp-content/plugins/wp-rocket/inc/Engine/Media/AboveTheFold/Frontend/Controller.php:178
Stack trace:
#0 /wp-content/plugins/wp-rocket/inc/Engine/Media/AboveTheFold/Frontend/Controller.php(105): WP_Rocket\Engine\Media\AboveTheFold\Frontend\Controller->set_fetchpriority(Object(stdClass), NULL)
#1 /wp-content/plugins/wp-rocket/inc/Engine/Media/AboveTheFold/Frontend/Controller.php(66): WP_Rocket\Engine\Media\AboveTheFold\Frontend\Controller->preload_lcp('<!DOCTYPE html>...', Object(WP_Rocket\Engine\Media\AboveTheFold\Database\Rows\AboveTheFold))
#2 /wp-content/plugins/wp-rocket/inc/Engine/Common/PerformanceHints/Frontend/Processor.php(78): WP_Rocket\Engine\Media\AboveTheFold\Frontend\Controller->optimize('<!DOCTYPE html>...', Object(WP_Rocket\Engine\Media\AboveTheFold\Database\Rows\AboveTheFold))
#3 /wp-content/plugins/wp-rocket/inc/Engine/Common/PerformanceHints/Frontend/Subscriber.php(61): WP_Rocket\Engine\Common\PerformanceHints\Frontend\Processor->maybe_apply_optimizations('<!DOCTYPE html>...')
#4 /wp-includes/class-wp-hook.php(353): WP_Rocket\Engine\Common\PerformanceHints\Frontend\Subscriber->maybe_apply_optimizations('<!DOCTYPE html>...')
#5 /wp-includes/plugin.php(206): WP_Hook->apply_filters('<!DOCTYPE html>...', Array)
#6 /wp-content/plugins/wp-rocket/inc/Engine/Optimization/Buffer/Optimization.php(87): apply_filters('rocket_buffer', '<!DOCTYPE html>...')
#7 [internal function]: WP_Rocket\Engine\Optimization\Buffer\Optimization->maybe_process_buffer('<!DOCTYPE html>...', 9)
#8 /wp-includes/functions.php(5581): ob_end_flush()
#9 /wp-includes/class-wp-hook.php(353): wp_ob_end_flush_all('')
#10 /wp-includes/class-wp-hook.php(377): WP_Hook->apply_filters(NULL, Array)
#11 /wp-includes/plugin.php(523): WP_Hook->do_action(Array)
#12 /wp-includes/load.php(1308): do_action('shutdown')
#13 [internal function]: shutdown_action_hook()
#14 {main}
  thrown in /wp-content/plugins/wp-rocket/inc/Engine/Media/AboveTheFold/Frontend/Controller.php on line 178

Root cause:

In inc/Engine/Media/AboveTheFold/Frontend/Controller.php, inside set_fetchpriority():

$url     = urldecode( preg_quote( $lcp->src, '/' ) );
$pattern = '#<img(?:[^>]*?\s+)?src=["\']' . $url . '["\'](?:\s+[^>]*?)?>#';

Note the similar issue #6690 didn't fix this issue.

To Reproduce
Steps to reproduce the behavior:

  1. Create a page and add an external image whose URL contains a query string with %2301 for example.
  2. Make sure that this image is detected as LCP and saved into the database with the query string.
  3. Clear the cache and visit the page.
  4. See the error 500 on the page and the Fatal error in the debug.log

Expected behavior
The Fatal error should not happen.

Probably the urldecode() and preg_quote() order should be reviewed.

Additional context
Ticket: https://onecomhelp.zendesk.com/agent/tickets/18396034

Metadata

Metadata

Labels

Ready for reviewReady for human reviewmodule: OCIOptimize Critical Imagespriority: highIssues which should be resolved as quickly as possibleseverity: criticalDefect that prevents the testing/use of the software

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions