Options, Meta APIs: Fix "passing null to non-nullable" deprecations to (get|add|update|delete)_option().

In all four of the `get_option()`, `add_option()`, `update_option()` and `delete_option()` functions, the `$option` parameter (i.e. the option name) is passed to the PHP native `trim()` function without prior input validation.

In PHP 8.1, this could lead to a `trim(): Passing null to parameter #1 ($string) of type string is deprecated` for each of these functions.

`trim()`:

- expects a text string and is only useful when ''passed'' a text string as no other variable type can contain whitespace.
- will always return a `string`, which means that in practice for any non-string values passed, it would effectively function as a type cast to string.

This commit:
- Adds a check to verify the `$option` name is a scalar before processing it with `trim()`.
- The "type cast" behavior is maintained.
- If the given `$option` name is not a scalar, such as `null`, the fix prevents the PHP 8.1 deprecation notice.
- Tests are added for valid but undesired option names to safeguard against regressions.

This issue is already covered by:
- the existing `Tests_Option_Option::test_bad_option_names()` test group.
- the new `test_valid_but_undesired_option_names()` tests.

Follow-up to [13858], [22633], [23510], [25002], [51817].

Props jrf, hellofromTonya, pbearne.
See #53635.

git-svn-id: https://develop.svn.wordpress.org/trunk@51818 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Tonya Mork 2021-09-15 22:18:36 +00:00
parent 07a7b4262b
commit 97790af164
2 changed files with 76 additions and 4 deletions

View File

@ -77,7 +77,10 @@
function get_option( $option, $default = false ) {
global $wpdb;
$option = trim( $option );
if ( is_scalar( $option ) ) {
$option = trim( $option );
}
if ( empty( $option ) ) {
return false;
}
@ -378,7 +381,10 @@ function wp_load_core_site_options( $network_id = null ) {
function update_option( $option, $value, $autoload = null ) {
global $wpdb;
$option = trim( $option );
if ( is_scalar( $option ) ) {
$option = trim( $option );
}
if ( empty( $option ) ) {
return false;
}
@ -565,7 +571,10 @@ function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' )
_deprecated_argument( __FUNCTION__, '2.3.0' );
}
$option = trim( $option );
if ( is_scalar( $option ) ) {
$option = trim( $option );
}
if ( empty( $option ) ) {
return false;
}
@ -687,7 +696,10 @@ function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' )
function delete_option( $option ) {
global $wpdb;
$option = trim( $option );
if ( is_scalar( $option ) ) {
$option = trim( $option );
}
if ( empty( $option ) ) {
return false;
}

View File

@ -148,6 +148,66 @@ class Tests_Option_Option extends WP_UnitTestCase {
);
}
/**
* @ticket 53635
*
* @dataProvider data_valid_but_undesired_option_names
*
* @param mixed $option_name Option name.
*/
public function test_get_option_valid_but_undesired_option_names( $option_name ) {
$this->assertFalse( get_option( $option_name ) );
}
/**
* @ticket 53635
*
* @dataProvider data_valid_but_undesired_option_names
*
* @param mixed $option_name Option name.
*/
public function test_add_option_valid_but_undesired_option_names( $option_name ) {
$this->assertTrue( add_option( $option_name, '' ) );
}
/**
* @ticket 53635
*
* @dataProvider data_valid_but_undesired_option_names
*
* @param mixed $option_name Option name.
*/
public function test_update_option_valid_but_undesired_option_names( $option_name ) {
$this->assertTrue( update_option( $option_name, '' ) );
}
/**
* @ticket 53635
*
* @dataProvider data_valid_but_undesired_option_names
*
* @param mixed $option_name Option name.
*/
public function test_delete_option_valid_but_undesired_option_names( $option_name ) {
$this->assertFalse( delete_option( $option_name ) );
}
/**
* Data provider.
*
* @return array
*/
public function data_valid_but_undesired_option_names() {
return array(
'string 123' => array( '123' ),
'integer 123' => array( 123 ),
'integer -123' => array( -123 ),
'float 12.3' => array( 12.3 ),
'float -1.23' => array( -1.23 ),
'boolean true' => array( true ),
);
}
/**
* @ticket 23289
*/