Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#54700 closed defect (bug) (fixed)

wp-includes/vars.php can trigger warning

Reported by: janh2's profile janh2 Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 6.0 Priority: normal
Severity: minor Version: 5.8.2
Component: Bootstrap/Load Keywords: php8 has-patch commit
Focuses: Cc:

Description

Some plugins are calling wp-load.php directly and handle the requests by hooking into actions (InfiniteWP in my case, "/wp-load.php?no_cache_XXXXX=1234567890", but I've seen it before in a different setting that I can't recall) which leads to warnings while setting $pagenow when using php8 (E_WARNING: Undefined array key 1 in wp-includes/vars.php:32).

wp-includes/vars.php lines 25-32

<?php
if ( is_network_admin() ) {
     preg_match( '#/wp-admin/network/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches );
} elseif ( is_user_admin() ) {
     preg_match( '#/wp-admin/user/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches );
} else {
     preg_match( '#/wp-admin/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches );
}
$pagenow = $self_matches[1];

If the preg_match does not match, $self_matches will be an empty array.

It's not really caused by WP (since the plugin is not using official APIs), but since this code can be hit in that way, I believe it should check whether a match has occurred.

Change History (6)

#1 @SergeyBiryukov
2 years ago

  • Component changed from General to Bootstrap/Load
  • Milestone changed from Awaiting Review to 6.0

Hi there, welcome to WordPress Trac! Thanks for the report.

Just linking to some related tickets here: #29161, #41171.

Last edited 2 years ago by SergeyBiryukov (previous) (diff)

#2 @SergeyBiryukov
2 years ago

  • Keywords php8 added

This ticket was mentioned in PR #2278 on WordPress/wordpress-develop by konradyoast.


2 years ago
#3

  • Keywords has-patch added

#4 @peterwilsoncc
2 years ago

  • Keywords commit added

The linked pull request looks good to me.

I did some manual testing in wp cli to ensure each step provided an expected value, it does.

I can't really see how to unit test this as the variables aren't defined within a function.

#5 @SergeyBiryukov
2 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 53294:

Bootstrap/Load: Avoid a PHP warning when setting the $pagenow global in wp-includes/vars.php.

In some scenarios where is_admin() returns true but $_SERVER['PHP_SELF'] does not match a wp-admin/* location, setting the $pagenow global could trigger a PHP warning: Undefined array key 1.

This commit avoids the warning by checking whether the matches array is not empty.

Props janh2, konradyoast, peterwilsoncc.
Fixes #54700.

Note: See TracTickets for help on using tickets.