Skip to content

Commit ec06d13

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 ec06d13

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/wp-includes/rest-api.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,6 +2182,26 @@ 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+
21852205
if ( isset( $args['anyOf'] ) ) {
21862206
$matching_schema = rest_find_any_matching_schema( $value, $args, $param );
21872207
if ( is_wp_error( $matching_schema ) ) {
@@ -2780,6 +2800,26 @@ function rest_validate_integer_value_from_schema( $value, $args, $param ) {
27802800
* @return mixed|WP_Error The sanitized value or a WP_Error instance if the value cannot be safely sanitized.
27812801
*/
27822802
function rest_sanitize_value_from_schema( $value, $args, $param = '' ) {
2803+
// Ensure GET requests can handle JSON-encoded objects/arrays,
2804+
//aligning with POST body parsing.
2805+
$type = isset( $args['type'] ) ? $args['type'] : '';
2806+
2807+
$is_structured = ( 'object' === $type || 'array' === $type );
2808+
if ( ! $is_structured && is_array( $type ) ) {
2809+
$is_structured = in_array( 'object', $type, true ) || in_array( 'array', $type, true );
2810+
}
2811+
2812+
if ( is_string( $value ) && $is_structured ) {
2813+
$trimmed_value = trim( $value );
2814+
if ( str_starts_with( $trimmed_value, '{' ) || str_starts_with( $trimmed_value, '[' ) ) {
2815+
$decoded = json_decode( $value, true );
2816+
2817+
if ( json_last_error() === JSON_ERROR_NONE ) {
2818+
$value = $decoded;
2819+
}
2820+
}
2821+
}
2822+
27832823
if ( isset( $args['anyOf'] ) ) {
27842824
$matching_schema = rest_find_any_matching_schema( $value, $args, $param );
27852825
if ( is_wp_error( $matching_schema ) ) {

0 commit comments

Comments
 (0)