REST API: Implement strict validation for the settings controller. Fi… #10662
+96
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR addresses the issue where the /wp/v2/settings endpoint returns a 200 OK even when sent unknown properties or an empty request body.
Changes:
Updated WP_REST_Settings_Controller::update_item to validate request parameters against registered settings.
Introduced a whitelist for "Infrastructure/Global" parameters (e.g., _locale, _wpnonce, _fields, _embed) to ensure backward compatibility and prevent breaking internal WordPress tools.
Returns a rest_invalid_param (400) if unknown parameters are passed in the JSON body or URL query string.
Returns a rest_empty_request (400) if the request does not contain any valid settings to update.
Why this approach works: Previous attempts were reverted because they broke requests using query parameters or global flags. By using an array_diff against a whitelist of registered options AND internal parameters, this patch provides strict validation without breaking existing ecosystem tools like Gutenberg or third-party clients.
Testing performed:
Added unit tests for empty bodies.
Added unit tests for unknown properties in JSON bodies and URL query strings.
Verified that global parameters (like _locale) still allow the request to succeed.
Trac ticket: https://core.trac.wordpress.org/ticket/41604