Skip to content

Commit 99a47e0

Browse files
committed
REST API: Fix object/array validation for JSON-encoded GET parameters.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
1 parent 4d3b0b9 commit 99a47e0

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

src/wp-includes/rest-api.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,6 +2182,27 @@ function rest_get_allowed_schema_keywords() {
21822182
* @return true|WP_Error
21832183
*/
21842184
function rest_validate_value_from_schema( $value, $args, $param = '' ) {
2185+
// Ensure GET requests can handle JSON-encoded objects/arrays,
2186+
//aligning with POST body parsing.
2187+
$type = isset( $args['type'] ) ? $args['type'] : '';
2188+
2189+
$is_structured = ( 'object' === $type || 'array' === $type );
2190+
if ( ! $is_structured && is_array( $type ) ) {
2191+
$is_structured = in_array( 'object', $type, true ) || in_array( 'array', $type, true );
2192+
}
2193+
2194+
if ( is_string( $value ) && $is_structured ) {
2195+
$trimmed_value = trim( $value );
2196+
if ( str_starts_with( $trimmed_value, '{' ) || str_starts_with( $trimmed_value, '[' ) ) {
2197+
$decoded = json_decode( $value, true );
2198+
2199+
if ( json_last_error() === JSON_ERROR_NONE ) {
2200+
$value = $decoded;
2201+
}
2202+
}
2203+
}
2204+
2205+
21852206
if ( isset( $args['anyOf'] ) ) {
21862207
$matching_schema = rest_find_any_matching_schema( $value, $args, $param );
21872208
if ( is_wp_error( $matching_schema ) ) {
@@ -2780,6 +2801,26 @@ function rest_validate_integer_value_from_schema( $value, $args, $param ) {
27802801
* @return mixed|WP_Error The sanitized value or a WP_Error instance if the value cannot be safely sanitized.
27812802
*/
27822803
function rest_sanitize_value_from_schema( $value, $args, $param = '' ) {
2804+
// Ensure GET requests can handle JSON-encoded objects/arrays,
2805+
//aligning with POST body parsing.
2806+
$type = isset( $args['type'] ) ? $args['type'] : '';
2807+
2808+
$is_structured = ( 'object' === $type || 'array' === $type );
2809+
if ( ! $is_structured && is_array( $type ) ) {
2810+
$is_structured = in_array( 'object', $type, true ) || in_array( 'array', $type, true );
2811+
}
2812+
2813+
if ( is_string( $value ) && $is_structured ) {
2814+
$trimmed_value = trim( $value );
2815+
if ( str_starts_with( $trimmed_value, '{' ) || str_starts_with( $trimmed_value, '[' ) ) {
2816+
$decoded = json_decode( $value, true );
2817+
2818+
if ( json_last_error() === JSON_ERROR_NONE ) {
2819+
$value = $decoded;
2820+
}
2821+
}
2822+
}
2823+
27832824
if ( isset( $args['anyOf'] ) ) {
27842825
$matching_schema = rest_find_any_matching_schema( $value, $args, $param );
27852826
if ( is_wp_error( $matching_schema ) ) {

0 commit comments

Comments
 (0)