Repository navigation
Conversation
…red. Previously, a request to the `/wp/v2/settings` endpoint that contained a setting that is not registered, or not exposed with `show_in_rest`, returned a 200 response and the setting was silently ignored. The request body is now checked, and a 400 `rest_invalid_param` error is returned if it contains a setting that is not registered. No settings are updated in that case. The query string is not checked. Besides settings, which are supported there in place of the request body, it carries global parameters such as `rest_route` on sites without pretty permalinks and `_locale` from the block editor, as well as parameters added by other code. Earlier attempts to fix this were reverted because they rejected requests like these. Global parameters in the request body, which are prefixed with an underscore, are ignored as well. Follow-up to [60301], [60357], [61324]. See #41604.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. 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. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
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/41604
A request to
/wp/v2/settingsthat contains a setting that is not registered (or not exposed withshow_in_rest) returns a 200 response, and the setting is silently ignored:This PR returns a 400
rest_invalid_paramerror when the request body contains a setting that is not registered. The query string is not checked.Why only the request body
The two previous fixes (r60301, r60357) were reverted in r61324 because they rejected valid requests that use the query string. The two open PRs, #10662 and #12967, use an allow-list of global parameters instead, but any parameter missing from that list breaks requests again. For example, on a site with plain permalinks every REST request is sent as
/?rest_route=/wp/v2/settings, andWP_REST_Server::serve_request()passes$_GETas-is, sorest_routebecomes a request parameter. With either open PR applied, saving any setting on such a site returns400 Invalid parameter(s): rest_route.The query string can contain:
rest_routeand_locale(comment:7),None of these can be reliably told apart from a mistyped setting, so the query string is left as it is. The request body has no such ambiguity, apart from global parameters, which are prefixed with an underscore and are ignored.
Changes
WP_REST_Settings_Controller::update_item()returns400 rest_invalid_param("Invalid parameter(s): …") if the form or JSON body contains a setting that is not registered. No settings are updated in that case._wpnonce,_method,_locale, …) are ignored.Invalid parameter(s): %sstring, so there is no new string to translate.Notes
Testing
New tests in
tests/phpunit/tests/rest-api/rest-settings-controller.php:?rest_route=/wp/v2/settings,?_locale=user, an unknown query parameter, a setting in the query string with an empty body, and_wpnoncein the body.This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.