REST API: Split comma-separated methods given in array form. - #13136
REST API: Split comma-separated methods given in array form.#13136moonmeister wants to merge 1 commit into
Conversation
`WP_REST_Server::register_route()` accepts `methods` as either a string or an array, but only the string form was split on commas. Any array element containing one became a single unmatchable method key, so `array( WP_REST_Server::READABLE, WP_REST_Server::EDITABLE )` registered the literal key `'POST, PUT, PATCH'` and `POST`, `PUT` and `PATCH` all returned 404, with no notice at registration time. The same route written as a string worked correctly. Splitting the array branch is safe unconditionally: a comma is a delimiter in the RFC 9110 `token` grammar, so no valid HTTP method name can contain one. Splitting can only turn a permanently-dead method key into working ones. The empty-array case is preserved by building up the list rather than imploding. Adds four tests to the existing `test_route_method_*` group, which covered an array of methods and a comma-separated string but not their intersection. Three fail on trunk; the fourth guards the empty-array behavior. Fixes #65905. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
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. |
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. |
Trac ticket: https://core.trac.wordpress.org/ticket/65905
Problem
WP_REST_Server::register_route()acceptsmethodsas a string or an array, but only the string form is split on commas. Any array element containing a comma becomes a single unmatchable method key.registers the keys
'GET'and'POST, PUT, PATCH'.GETworks;POST,PUTandPATCHall 404. The same route written as the string'GET, POST, PUT, PATCH'works. Nothing warns at registration time.Dates to the array support added in 4.4.
Fix
Split the array branch as well, merging each element's comma-separated parts.
A comma is a delimiter in the RFC 9110
tokengrammar, so no valid HTTP method name can contain one. Splitting can therefore only turn a permanently-dead method key into working ones — there is no case where the current behavior is the desired one.The loop is deliberate rather than
explode( ',', implode( ',', $handler['methods'] ) ): imploding an empty array yields'', which would register an empty-string method key.test_route_method_empty_array()guards that.Tests
Four tests added to the existing
test_route_method_*group, which covered an array of methods and a comma-separated string but not their intersection:test_route_method_array_with_comma_separated_valuestest_route_method_array_with_multi_method_constanttest_route_method_array_with_multi_method_constant_dispatchestest_route_method_empty_array--group restapi: 3554 tests / 16246 assertions, no new failures against a 3550 / 16239 baseline (identical pre-existing error and warning profile).Impact
A plugin-directory regex sweep found 4 occurrences across 3 plugins (~2,000 installs) — all
array( CREATABLE, EDITABLE ), wherePOSTworks and the intendedPUT/PATCHsilently 404. The array form itself is common (921 plugins), but only 87 of 7,024 array-formmethodslines name aWP_REST_Server::constant at all, which is why the bug has stayed quiet.The larger reason to fix it is forward-looking. This was found while measuring what would happen if a method constant gained a second method — as it would if
QUERY(RFC 10008) were added toREADABLEorALLMETHODS. Every array-form registration naming that constant would break at once, with no error. Landing this removes that constraint before the question comes up.