Skip to content

Commit 9d7b7c2

Browse files
committed
Abilities API: Reject input that sanitization makes invalid
`WP_Ability::prepare_input()` validated input before sanitizing it, but did not validate the sanitized value. Sanitization can turn accepted input into a value that breaks the schema. For example, whitespace or markup can become an empty string below `minLength`, and an infinite number can become `0` when cast to an integer with `minimum: 1`. Callbacks could then receive input that does not match the schema. When sanitization changes the value, `prepare_input()` now validates it again with `validate_input()`, including the `wp_ability_validate_input` filter. Unchanged input is validated only once. Validation errors are returned before the permission and execute callbacks run. Developed in #15911. Follow-up to r65044, r65370. Reviewed by jorgefilipecosta. Props aryanjasala, avinashzala01, gziolo, jorgefilipecosta. Fixes #66665. git-svn-id: https://develop.svn.wordpress.org/trunk@65739 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9fb84f4 commit 9d7b7c2

3 files changed

Lines changed: 98 additions & 21 deletions

File tree

‎src/wp-includes/abilities-api/class-wp-ability.php‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,11 @@ public function validate_input( $input = null ) {
682682
* override it. If default validation passed, the filter can add additional
683683
* validation checks and return a WP_Error if those checks fail.
684684
*
685+
* During input preparation, this filter runs again with sanitized input if
686+
* sanitization changes the value and completes without an error.
687+
*
685688
* @since 7.1.0
689+
* @since 7.2.0 Also runs with sanitized input when sanitization changes the value.
686690
*
687691
* @param true|WP_Error $is_valid The validation result from default validation.
688692
* @param mixed $input The input data being validated.
@@ -704,7 +708,8 @@ public function validate_input( $input = null ) {
704708
* Normalizes the input, validates it against the input schema, and then sanitizes
705709
* it. `execute()` passes the result to the permission and execute callbacks.
706710
* Validation runs before sanitization, so sanitization cannot make invalid input
707-
* valid.
711+
* valid. Changed input is validated again after sanitization, including the
712+
* `wp_ability_validate_input` filter, before it reaches the callbacks.
708713
*
709714
* @since 7.2.0
710715
*
@@ -726,7 +731,25 @@ public function prepare_input( $input = null ) {
726731
return $is_valid;
727732
}
728733

729-
return $this->sanitize_input( $input );
734+
$sanitized = $this->sanitize_input( $input );
735+
if ( is_wp_error( $sanitized ) ) {
736+
return $sanitized;
737+
}
738+
739+
/*
740+
* Sanitization can make valid input invalid, for example by trimming a string
741+
* below its `minLength` or casting an out-of-range number to a different integer.
742+
* Validate changed input again so validation filters also evaluate the value
743+
* callbacks will receive.
744+
*/
745+
if ( $sanitized !== $input ) {
746+
$is_valid = $this->validate_input( $sanitized );
747+
if ( is_wp_error( $is_valid ) ) {
748+
return $is_valid;
749+
}
750+
}
751+
752+
return $sanitized;
730753
}
731754

732755
/**

‎tests/phpunit/tests/abilities-api/wpAbility.php‎

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,7 +2362,7 @@ public function test_prepare_input( array $schema, $input, $expected ): void {
23622362
public static function data_input_coercion(): array {
23632363
return array_intersect_key(
23642364
self::data_input_is_coerced(),
2365-
array_flip( array( 'integer number', 'stdClass object input', 'default', 'text-field format' ) )
2365+
array_flip( array( 'integer number', 'stdClass object input', 'default', 'text-field format', 'top-level string unchanged' ) )
23662366
);
23672367
}
23682368

@@ -2420,14 +2420,18 @@ static function ( $name, $value ) use ( &$seen ) {
24202420

24212421
$this->assertSame( 0, $ability->execute( $input ) );
24222422
$this->assertSame( $input, $seen['invoked'] );
2423-
$this->assertSame( empty( $schema ) ? array() : array( $normalized ), $seen['validation'], 'Validate the normalized input exactly once.' );
2423+
$expected_validation = array( $normalized );
2424+
if ( $expected !== $normalized ) {
2425+
$expected_validation[] = $expected;
2426+
}
2427+
$this->assertSame( $expected_validation, $seen['validation'], 'Validate normalized input, then sanitized input only when it changes.' );
24242428
$this->assertSame( $expected, $seen['permission'] );
24252429
$this->assertSame( $expected, $seen['before'] );
24262430
$this->assertSame( $expected, $seen['execute'] );
24272431
}
24282432

24292433
/**
2430-
* Provides top-level and nested collisions introduced by sanitization.
2434+
* Provides input that sanitization rejects or makes invalid.
24312435
*
24322436
* @return array
24332437
*/
@@ -2449,16 +2453,43 @@ public static function data_input_sanitization_errors(): array {
24492453
'ids' => '1,01',
24502454
);
24512455

2456+
$text = array(
2457+
'type' => 'object',
2458+
'properties' => array(
2459+
'title' => array(
2460+
'type' => 'string',
2461+
'format' => 'text-field',
2462+
'minLength' => 1,
2463+
),
2464+
),
2465+
);
2466+
24522467
return array(
2453-
'top-level array' => array( $unique, array( '1', '01' ), 'input' ),
2454-
'nested object' => array( $object, $input, 'input[ids]' ),
2455-
'object in list' => array(
2468+
'top-level array' => array( $unique, array( '1', '01' ), 'input has duplicate items.' ),
2469+
'nested object' => array( $object, $input, 'input[ids] has duplicate items.' ),
2470+
'object in list' => array(
24562471
array(
24572472
'type' => 'array',
24582473
'items' => $object,
24592474
),
24602475
array( $input ),
2461-
'input[0][ids]',
2476+
'input[0][ids] has duplicate items.',
2477+
),
2478+
'whitespace below minLength' => array( $text, array( 'title' => ' ' ), 'input[title] must be at least 1 character long.' ),
2479+
'markup below minLength' => array( $text, array( 'title' => '<b></b>' ), 'input[title] must be at least 1 character long.' ),
2480+
// An out-of-range numeric string is cast to `PHP_INT_MAX`, which is not a multiple of 10.
2481+
'integer cast breaks multipleOf' => array(
2482+
array(
2483+
'type' => 'object',
2484+
'properties' => array(
2485+
'count' => array(
2486+
'type' => 'integer',
2487+
'multipleOf' => 10,
2488+
),
2489+
),
2490+
),
2491+
array( 'count' => '1e19' ),
2492+
'input[count] must be a multiple of 10.',
24622493
),
24632494
);
24642495
}
@@ -2469,11 +2500,11 @@ public static function data_input_sanitization_errors(): array {
24692500
* @ticket 66383
24702501
* @dataProvider data_input_sanitization_errors
24712502
*
2472-
* @param array $schema Input schema.
2473-
* @param mixed $input Input accepted by validation but rejected by sanitization.
2474-
* @param string $path Path of the duplicate items in the error message.
2503+
* @param array $schema Input schema.
2504+
* @param mixed $input Input accepted by validation but rejected by sanitization.
2505+
* @param string $message Expected error message fragment.
24752506
*/
2476-
public function test_input_sanitization_errors( array $schema, $input, string $path ): void {
2507+
public function test_input_sanitization_errors( array $schema, $input, string $message ): void {
24772508
$args = self::$test_ability_properties;
24782509
$args['input_schema'] = $schema;
24792510
$unexpected_callback = function () {
@@ -2491,8 +2522,7 @@ public function test_input_sanitization_errors( array $schema, $input, string $p
24912522
$error = $ability->prepare_input( $input );
24922523
$this->assertWPError( $error );
24932524
$this->assertSame( 'ability_invalid_input', $error->get_error_code() );
2494-
$this->assertStringContainsString( $path . ' has duplicate items.', $error->get_error_message() );
2495-
// Keep assertEquals() because the objects are intentionally compared by value.
2525+
$this->assertStringContainsString( $message, $error->get_error_message() );
24962526
$this->assertEquals( $error, $ability->execute( $input ), 'Execution should propagate the sanitization error.' );
24972527
}
24982528

@@ -2551,6 +2581,30 @@ public function test_sanitization_respects_input_validation(): void {
25512581
$this->assertSame( array( array( 'count' => 10 ) ), $seen );
25522582
}
25532583

2584+
/**
2585+
* Validation filters can reject the sanitized value before callbacks receive it.
2586+
*
2587+
* @ticket 66665
2588+
*/
2589+
public function test_validation_filter_can_reject_sanitized_input(): void {
2590+
$args = self::$test_ability_properties;
2591+
$args['input_schema'] = array( 'type' => 'integer' );
2592+
$ability = new WP_Ability( self::$test_ability_name, $args );
2593+
$error = new WP_Error( 'sanitized_input_rejected' );
2594+
$seen = array();
2595+
$validate = static function ( $valid, $input ) use ( &$seen, $error ) {
2596+
$seen[] = $input;
2597+
return is_int( $input ) ? $error : $valid;
2598+
};
2599+
2600+
add_filter( 'wp_ability_validate_input', $validate, 10, 2 );
2601+
$result = $ability->execute( '2' );
2602+
remove_filter( 'wp_ability_validate_input', $validate );
2603+
2604+
$this->assertSame( array( '2', 2 ), $seen, 'The filter should receive both raw and sanitized input.' );
2605+
$this->assertSame( $error, $result, 'Execution should propagate the filter rejection unchanged.' );
2606+
}
2607+
25542608
/**
25552609
* Returns the test ability properties with required capabilities and no permission callback.
25562610
*

‎tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,12 +2193,12 @@ public static function data_input_prepared_once(): array {
21932193
public function test_input_is_prepared_once_before_permission_check( ?array $input ): void {
21942194
$calls = new stdClass();
21952195

2196-
$calls->validate = 0;
2196+
$calls->normalize = 0;
21972197
add_filter(
2198-
'wp_ability_validate_input',
2199-
static function ( $is_valid ) use ( $calls ) {
2200-
++$calls->validate;
2201-
return $is_valid;
2198+
'wp_ability_normalize_input',
2199+
static function ( $value ) use ( $calls ) {
2200+
++$calls->normalize;
2201+
return $value;
22022202
}
22032203
);
22042204

@@ -2216,7 +2216,7 @@ static function ( $is_valid ) use ( $calls ) {
22162216
),
22172217
),
22182218
'permission_callback' => static function ( $value ) use ( $calls ) {
2219-
$calls->permission ??= array( $value, $calls->validate );
2219+
$calls->permission ??= array( $value, $calls->normalize );
22202220
return true;
22212221
},
22222222
'execute_callback' => static function ( $value ) {

0 commit comments

Comments
 (0)