REST API: Guard against a non-array return from the rest_endpoints filter - #13254
REST API: Guard against a non-array return from the rest_endpoints filter#13254i-am-chitti wants to merge 1 commit into
rest_endpoints filter#13254Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
rest_endpoints filter
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Ticket
Trac ticket: https://core.trac.wordpress.org/ticket/65953
Description
WP_REST_Server::get_routes()documents anarrayreturn, but does not validate what therest_endpointsfilter gives back. A callback that forgets toreturn $endpointsmakes it returnnull, which reachesarray_merge( ...$with_namespace )inmatch_request_to_handler()and throws a fatalTypeErroron PHP 8. On PHP 7.4 it only warned, so it looks like an upgrade regression.The fix validates the filtered value in
get_routes(), where the bad value originates, so every caller is covered. A non-array falls back to an empty array and calls_doing_it_wrong()to surface the offending callback. Dispatch then returns a normalrest_no_route404.Unit tests are included.
Testing instructions
add_filter( 'rest_endpoints', function ( $endpoints ) { unset( $endpoints['/wp/v2/users'] ); } );trunkand observe the fatalTypeError.rest_no_route404 plus a_doing_it_wrong()notice forWP_REST_Server::get_routes.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: drafting the fix and unit tests