Make WordPress Core

Opened 9 hours ago

Last modified 2 hours ago

#66871 new defect (bug)

REST API: Validation skips additional matching patternProperties schemas

Reported by: ekamran Owned by:
Priority: normal Milestone: Awaiting Review
Component: REST API Version: 5.6
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

REST API object validation uses only the first matching patternProperties schema. A value can therefore pass validation even when it fails another matching pattern's constraints. Reordering the patterns changes the result.

JSON Schema draft 4 requires a property to validate against every matching pattern's schema, as well as its properties schema when present. See ​section 8.3.3 of the validation specification referenced by #51024.

Reproduction

Run this in a loaded WordPress environment, for example with wp eval-file:

<?php
$digits = array( 'type' => 'string', 'pattern' => '^[0-9]+$' );
$any    = array( 'type' => 'string' );

$a = array( 'type' => 'object', 'patternProperties' => array( '^p' => $any, '1$' => $digits ) );
$b = array( 'type' => 'object', 'patternProperties' => array( '1$' => $digits, '^p' => $any ) );

var_dump( rest_validate_value_from_schema( array( 'p1' => 'abc' ), $a, 'arg' ) );
var_dump( rest_validate_value_from_schema( array( 'p1' => 'abc' ), $b, 'arg' ) );

Actual: $a returns true; $b returns a WP_Error with code rest_invalid_pattern and message arg[p1] does not match pattern ^[0-9]+$.

Expected under draft 4: both reject abc, because p1 matches both patterns. As a control, 123 passes in both orders.

Related overlap with properties

An explicitly named property also bypasses matching patterns:

<?php
$schema = array(
        'type'              => 'object',
        'properties'        => array( 'p1' => array( 'type' => 'string' ) ),
        'patternProperties' => array( '^p' => array( 'type' => 'string', 'maxLength' => 2 ) ),
);

var_dump( rest_validate_value_from_schema( array( 'p1' => 'abcd' ), $schema, 'arg' ) );

Actual: true. Expected under draft 4: rejection because abcd exceeds maxLength. Removing the properties entry produces rest_too_long.

Verification and implementation

Reproduced on WordPress 7.1.5 with PHP 8.4.27. The helper and the three callers below are unchanged on trunk as of [65822].

  • Dispatching a WP_REST_Request through rest_do_request() also reproduces the difference: with a POST route's data argument using $a, data[p1]=abc returns 200; using $b returns 400 with rest_invalid_param. This was an in-process REST dispatch, not an HTTP test.
  • Direct sanitization is order-dependent too. With '^p' => array( 'type' => 'integer' ) before '1$' => array( 'type' => 'string' ), rest_sanitize_value_from_schema() changes array( 'p1' => '5' ) to array( 'p1' => 5 ); reversing them retains the string. This is a separate coercion example, not a claim that draft 4 defines sanitization or that conflicting types should both be applied as casts.

rest_validate_object_value_from_schema() checks properties first and otherwise uses rest_find_matching_pattern_property_schema(), which returns the first matching schema. The same lookup is also used by rest_sanitize_value_from_schema() and rest_filter_response_by_context(), so changing the helper's return contract would affect those callers.

History and requested direction

Support was introduced in WordPress 5.6 in [49082] / #51024. The ​original review explicitly requested avoiding pattern checks for named properties. That is relevant history for the second example, though it does not address multiple matching patterns. #66770 concerns the helper's parameter documentation, not this validation behavior.

Applying every matching schema during validation would follow draft 4, but could reject requests accepted today. Could the REST API maintainers advise whether to align validation with draft 4 or retain and explicitly document the current limitation? Sanitization and response-context handling need their own compatibility consideration; this ticket does not propose changing all three callers together. No patch is attached pending that guidance.

Change History (1)

This ticket was mentioned in ​PR #16328 on ​WordPress/wordpress-develop by ​@therssoftware.


2 hours ago
#1

  • Keywords has-patch has-unit-tests added

Description

rest_validate_object_value_from_schema() previously called rest_find_matching_pattern_property_schema(), which returns only the first matching patternProperties schema. As a result, a property matching multiple patterns could pass validation even when it violated a subsequent matching pattern's constraints, making validation order-dependent and inconsistent with JSON Schema draft 4.

Changes

  • In rest_validate_object_value_from_schema() (src/wp-includes/rest-api.php), iterate over all entries in $args['patternProperties'] and validate the property value against every matching pattern's schema.
  • Preserve rest_find_matching_pattern_property_schema()'s public signature and array|null return contract for backwards compatibility with existing callers (rest_sanitize_value_from_schema(), rest_filter_response_by_context(), and third-party code).
  • Preserve existing properties precedence ([49082] / #51024) and additionalProperties fallback when no pattern matches.
  • Add unit tests in tests/phpunit/tests/rest-api/rest-schema-validation.php covering multiple matching patterns, pattern-order independence, conflicting type constraints, single-pattern matches, and additionalProperties: false fallback.

Testing

  • Ran vendor/bin/phpcs on both modified files.
  • Ran single-site PHPUnit on WP_Test_REST_Schema_Validation and WP_Test_REST_Schema_Sanitization (258 tests, 454 assertions) and multisite PHPUnit on WP_Test_REST_Schema_Validation (217 tests, 339 assertions).
Note: See TracTickets for help on using tickets.