REST API: Fix object/array validation for JSON strings in GET requests (#64926)#11371
REST API: Fix object/array validation for JSON strings in GET requests (#64926)#11371liaisontw wants to merge 1 commit intoWordPress:trunkfrom
Conversation
|
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. |
99a47e0 to
90e693a
Compare
….This commit aligns GET parameter handling with POST requests by allowingJSON-encoded strings to pass 'object' and 'array' validation andsanitization.- Added JSON coercion in rest_validate_value_from_schema().- Added JSON coercion in rest_sanitize_value_from_schema().- Supports multi-type schemas and uses json_last_error() for safety.Fixes #64926
90e693a to
ec06d13
Compare
|
Appreciate you taking the time for this @liaisontw |
Description
Trac Ticket: https://core.trac.wordpress.org/ticket/64926
This PR addresses #64926, where REST API GET requests fail validation when parameters defined as object or array are passed as JSON-encoded strings (e.g., via JSON.stringify).
The Problem
Currently, rest_validate_value_from_schema() receives these parameters as raw strings from $_GET. Since the schema expects an object or array, it triggers a rest_invalid_type error (400 Bad Request) before any sanitization or decoding can occur. While POST requests handle this via parse_json_params(), no equivalent coercion exists for query-string parameters.
The Fix
This PR introduces JSON coercion in both rest_validate_value_from_schema() and rest_sanitize_value_from_schema().
If the schema expects a structured type but receives a string, it attempts to json_decode().
Uses json_last_error() === JSON_ERROR_NONE to ensure only valid JSON is coerced, maintaining safety for regular strings.
Supports multi-type schemas (e.g., ['string', 'object']).
No changes to function signatures, ensuring backward compatibility.
How to Test
Register a test endpoint with an object type parameter:
PHP
register_rest_route( 'my-test/v1', '/schema-test', array(
'methods' => 'GET',
'callback' => function( $request ) {
return array( 'success' => true, 'data' => $request->get_param( 'config' ) );
},
'args' => array(
'config' => array( 'type' => 'object' ),
),
) );
Test Case A: Valid JSON Object
URL: /wp-json/my-test/v1/schema-test?config={"id":123,"name":"Gemini"}
Expected Result: {"success":true,"data":{"id":123,"name":"Gemini"}} (Integers preserved).
Test Case B: Empty Object
URL: /wp-json/my-test/v1/schema-test?config={}
Expected Result: {"success":true,"data":[]} (PHP decodes empty JSON object as array).
Test Case C: Invalid JSON (Safety Check)
URL: /wp-json/my-test/v1/schema-test?config={id:123}
Expected Result: 400 Bad Request (Correctly rejected because it's not valid JSON and doesn't match type object).
Screenshots/Logs
Tested in a local WordPress development environment.
Before Patch: Returns rest_invalid_type error for all JSON string inputs in GET.
After Patch: Successfully decodes and validates structured data while maintaining strictness for invalid JSON.
Types of changes
[x] Bug fix (non-breaking change which fixes an issue)
[ ] New feature (non-breaking change which adds functionality)
[ ] Performance improvement