#60771 closed defect (bug) (fixed)
PHP 8 Fatal error in WP_REST_Search_Controller
Reported by: | dd32 | Owned by: | swissspidy |
---|---|---|---|
Milestone: | 6.6 | Priority: | normal |
Severity: | normal | Version: | 5.0 |
Component: | REST API | Keywords: | php8 has-patch has-unit-tests commit |
Focuses: | rest-api | Cc: |
Description
WP_REST_Search_Controller
does not properly sanitize the type
parameter before using it, leading to a PHP Warning / PHP Fatal error:
PHP 7.4 E_WARNING: Illegal offset type in isset or empty in wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php:398 PHP 8.1 Fatal error: Uncaught TypeError: Illegal offset type in isset or empty in wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php:398
This happens with a rest-api request such as this:
/wp/v2/search?subtype=page&type[]=post
The schema correctly requires it to be a string, and under PHP 7.4 an incorrect schema error will be thrown.
The issue is that the warning occurs within the sanitize handler:
#0 wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php(379): WP_REST_Search_Controller->get_search_handler(Object(WP_REST_Request)) #1 wp-includes/rest-api/class-wp-rest-request.php(833): WP_REST_Search_Controller->sanitize_subtypes(Array, Object(WP_REST_Request), 'subtype') #2 wp-includes/rest-api/class-wp-rest-server.php(1056): WP_REST_Request->sanitize_params() #3 wp-includes/rest-api/class-wp-rest-server.php(439): WP_REST_Server->dispatch(Object(WP_REST_Request)) #4 wp-includes/rest-api.php(428): WP_REST_Server->serve_request('/wp/v2/search')
Basically; causing WP_REST_Search_Controller::get_search_handler()
to run with type
as an array, even though it's not acceptable according to the schema.
Change History (6)
This ticket was mentioned in PR #6270 on WordPress/wordpress-develop by @swissspidy.
10 months ago
#2
- Keywords has-unit-tests added
10 months ago
#4
@TimothyBJacobs I agree; It would be nice if $request->get_param()
didn't return the data if it was not matching the schema perhaps?
I guess it would depend upon the order of operations though - sanitizing/validating subtype
before type
got checked could still potentially end up in the same situation.
This PR as-is though, seems like the most obvious and straight forward change.
#5
@
10 months ago
- Owner set to swissspidy
- Resolution set to fixed
- Status changed from new to closed
In 57839:
@swissspidy commented on PR #6270:
10 months ago
#6
Committed in https://core.trac.wordpress.org/changeset/57839
In
WP_REST_Search_Controller
, avoid a PHP error when thetype
parameter is not a string as expected.Trac ticket: https://core.trac.wordpress.org/ticket/60771