Skip to content

Commit c636204

Browse files
committed
Abilities API: Skip redundant input sanitization in the REST run endpoint
The permission callback and `WP_Ability::execute()` already normalize, validate, and coerce input. The `input` argument now sets `sanitize_callback` to `null`, so both receive the original raw input. `sanitize_input_for_ability()` is deprecated and returns its input unchanged. Follow-up to [62674], [65044]. Developed in #15684. Props gziolo, jorgefilipecosta, euvoor, justlevine. Fixes #66383. git-svn-id: https://develop.svn.wordpress.org/trunk@65368 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 39b25ce commit c636204

2 files changed

Lines changed: 192 additions & 32 deletions

File tree

‎src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php‎

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public function validate_request_method( string $request_method, array $annotati
149149
* Checks if a given request has permission to execute a specific ability.
150150
*
151151
* @since 6.9.0
152+
* @since 7.2.0 Prepares the input read from the raw request.
152153
*
153154
* @param WP_REST_Request $request Full details about the request.
154155
* @return true|WP_Error True if the request has execution permission, WP_Error object otherwise.
@@ -171,18 +172,7 @@ public function check_ability_permissions( $request ) {
171172
return $is_valid;
172173
}
173174

174-
$input = $this->get_input_from_request( $request );
175-
$input = $ability->normalize_input( $input );
176-
if ( is_wp_error( $input ) ) {
177-
return $this->ensure_error_status( $input, 400 );
178-
}
179-
180-
$is_valid = $ability->validate_input( $input );
181-
if ( is_wp_error( $is_valid ) ) {
182-
return $this->ensure_error_status( $is_valid, 400 );
183-
}
184-
185-
$input = $ability->coerce_input( $input );
175+
$input = $this->prepare_input_for_ability( $ability, $this->get_input_from_request( $request ) );
186176
if ( is_wp_error( $input ) ) {
187177
return $this->ensure_error_status( $input, 400 );
188178
}
@@ -242,30 +232,42 @@ private function get_input_from_request( $request ) {
242232
}
243233

244234
/**
245-
* Sanitizes the run input by coercing it to the ability's input schema.
235+
* Returns run input unchanged for backward compatibility.
246236
*
247-
* Registered as the `input` argument `sanitize_callback` so request input is coerced
248-
* before any route callback runs, regardless of transport. `check_ability_permissions()`
249-
* and `WP_Ability::execute()` coerce the input again after normalization and validation,
250-
* so schema defaults and values from normalization filters are coerced too.
237+
* This method is no longer registered as the input argument sanitizer.
251238
*
252239
* @since 7.1.0
253-
*
254-
* @see WP_Ability::coerce_input()
240+
* @deprecated 7.2.0 Input is prepared by `check_ability_permissions()` and `WP_Ability::execute()`.
255241
*
256242
* @param mixed $input Raw input extracted from the request.
257-
* @param WP_REST_Request $request The request object.
258-
* @return mixed|WP_Error Coerced input, raw input when validation fails, or WP_Error when coercion fails.
243+
* @param WP_REST_Request $request Unused. The request object.
244+
* @return mixed The unchanged input.
259245
*/
260246
public function sanitize_input_for_ability( $input, $request ) {
261-
$ability = wp_has_ability( $request['name'] ) ? wp_get_ability( $request['name'] ) : null;
262-
if ( ! $ability instanceof WP_Ability ) {
247+
_deprecated_function( __METHOD__, '7.2.0' );
248+
249+
return $input;
250+
}
251+
252+
/**
253+
* Normalizes, validates, and coerces input for an ability.
254+
*
255+
* @since 7.2.0
256+
*
257+
* @param WP_Ability $ability The ability to prepare input for.
258+
* @param mixed $input Input to prepare.
259+
* @return mixed|WP_Error Prepared input, or `WP_Error` when normalization, validation, or coercion fails.
260+
*/
261+
private function prepare_input_for_ability( WP_Ability $ability, $input ) {
262+
$input = $ability->normalize_input( $input );
263+
if ( is_wp_error( $input ) ) {
263264
return $input;
264265
}
265266

266267
// Validate before coercion so sanitization cannot rescue invalid input.
267-
if ( null === $input || empty( $ability->get_input_schema() ) || is_wp_error( $ability->validate_input( $input ) ) ) {
268-
return $input;
268+
$is_valid = $ability->validate_input( $input );
269+
if ( is_wp_error( $is_valid ) ) {
270+
return $is_valid;
269271
}
270272

271273
return $ability->coerce_input( $input );
@@ -284,7 +286,8 @@ public function get_run_args(): array {
284286
'description' => __( 'Input parameters for the ability execution.' ),
285287
'type' => array( 'integer', 'number', 'boolean', 'string', 'array', 'object', 'null' ),
286288
'default' => null,
287-
'sanitize_callback' => array( $this, 'sanitize_input_for_ability' ),
289+
// Keep the raw input for the permission check and execution. This disables default REST coercion.
290+
'sanitize_callback' => null,
288291
),
289292
);
290293
}

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

Lines changed: 163 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,12 +1937,7 @@ static function () use ( $input ) {
19371937
$response = $this->dispatch_run( $method, 'test/coercion-error', 'request' === $source ? $input : null );
19381938
$this->assertSame( 400, $response->get_status() );
19391939
$data = $response->get_data();
1940-
if ( 'request' === $source ) {
1941-
$this->assertSame( 'rest_invalid_param', $data['code'] );
1942-
$this->assertSame( 'ability_invalid_input', $data['data']['details']['input']['code'] );
1943-
} else {
1944-
$this->assertSame( 'ability_invalid_input', $data['code'] );
1945-
}
1940+
$this->assertSame( 'ability_invalid_input', $data['code'] );
19461941
}
19471942

19481943
/**
@@ -2173,4 +2168,166 @@ public function test_permission_callback_receives_typed_input( $input ): void {
21732168
$this->assertSame( 'integer', $seen->types[0], 'The REST permission gate should receive coerced (typed) input, not a raw string.' );
21742169
$this->assertSame( array( 'count' => 10 ), $response->get_data(), 'The execute callback should receive coerced input too.' );
21752170
}
2171+
2172+
/**
2173+
* Data provider for input sent with the request and input left out.
2174+
*
2175+
* @return array<string, array{0: array|null}> Request input, or null to use the schema default.
2176+
*/
2177+
public static function data_input_prepared_once(): array {
2178+
return array(
2179+
'request input' => array( array( 'count' => '10' ) ),
2180+
'schema default' => array( null ),
2181+
);
2182+
}
2183+
2184+
/**
2185+
* Tests that the input is prepared only once before the permission check.
2186+
*
2187+
* @ticket 66383
2188+
* @dataProvider data_input_prepared_once
2189+
*
2190+
* @param array|null $input Request input, or null to use the schema default.
2191+
*/
2192+
public function test_input_is_prepared_once_before_permission_check( ?array $input ): void {
2193+
$calls = new stdClass();
2194+
2195+
$calls->validate = 0;
2196+
add_filter(
2197+
'wp_ability_validate_input',
2198+
static function ( $is_valid ) use ( $calls ) {
2199+
++$calls->validate;
2200+
return $is_valid;
2201+
}
2202+
);
2203+
2204+
$this->register_test_ability(
2205+
'test/prepared-once',
2206+
array(
2207+
'label' => 'Prepared Once',
2208+
'description' => 'Records how the input was prepared for the permission check.',
2209+
'category' => 'general',
2210+
'input_schema' => array(
2211+
'type' => 'object',
2212+
'default' => array( 'count' => '10' ),
2213+
'properties' => array(
2214+
'count' => array( 'type' => 'integer' ),
2215+
),
2216+
),
2217+
'permission_callback' => static function ( $value ) use ( $calls ) {
2218+
$calls->permission ??= array( $value, $calls->validate );
2219+
return true;
2220+
},
2221+
'execute_callback' => static function ( $value ) {
2222+
return $value;
2223+
},
2224+
'meta' => array(
2225+
'annotations' => array( 'readonly' => true ),
2226+
'show_in_rest' => true,
2227+
),
2228+
)
2229+
);
2230+
2231+
$response = $this->dispatch_run( 'GET', 'test/prepared-once', $input );
2232+
2233+
$this->assertSame( 200, $response->get_status() );
2234+
$this->assertSame( array( array( 'count' => 10 ), 1 ), $calls->permission, 'The permission callback should get input prepared once.' );
2235+
}
2236+
2237+
/**
2238+
* Data provider for requests rejected by the permission check.
2239+
*
2240+
* @return array<string, array{0: string, 1: bool, 2: string}> HTTP method, whether the
2241+
* ability is shown in REST, and the expected error code.
2242+
*/
2243+
public static function data_invalid_input_does_not_hide_request_errors(): array {
2244+
return array(
2245+
'hidden ability' => array( 'GET', false, 'rest_ability_not_found' ),
2246+
'wrong HTTP method' => array( 'POST', true, 'rest_ability_invalid_method' ),
2247+
);
2248+
}
2249+
2250+
/**
2251+
* Tests that invalid input does not replace not found and wrong method errors.
2252+
*
2253+
* @ticket 66383
2254+
* @dataProvider data_invalid_input_does_not_hide_request_errors
2255+
*
2256+
* @param string $method HTTP method.
2257+
* @param bool $show_in_rest Whether the ability is shown in REST.
2258+
* @param string $error_code Expected error code.
2259+
*/
2260+
public function test_invalid_input_does_not_hide_request_errors( string $method, bool $show_in_rest, string $error_code ): void {
2261+
$this->register_test_ability(
2262+
'test/rejected-request',
2263+
array(
2264+
'label' => 'Rejected Request',
2265+
'description' => 'Is rejected before its input is checked.',
2266+
'category' => 'general',
2267+
'input_schema' => array(
2268+
'type' => 'object',
2269+
'properties' => array(
2270+
'count' => array( 'type' => 'integer' ),
2271+
),
2272+
),
2273+
'permission_callback' => '__return_true',
2274+
'execute_callback' => '__return_true',
2275+
'meta' => array(
2276+
'annotations' => array( 'readonly' => true ),
2277+
'show_in_rest' => $show_in_rest,
2278+
),
2279+
)
2280+
);
2281+
2282+
$response = $this->dispatch_run( $method, 'test/rejected-request', array( 'count' => 'not-a-number' ) );
2283+
2284+
$this->assertSame( $error_code, $response->get_data()['code'] );
2285+
}
2286+
2287+
/**
2288+
* Tests that a REST run normalizes the input only once.
2289+
*
2290+
* The normalize filter adds `!` to the input, and the schema allows at most
2291+
* 4 characters. So `raw` becomes a valid `raw!`, but normalizing it again
2292+
* gives `raw!!`, which fails validation.
2293+
*
2294+
* @ticket 66383
2295+
* @dataProvider data_coercion_transports
2296+
*
2297+
* @param string $method HTTP method.
2298+
* @param array $annotations Ability annotations.
2299+
*/
2300+
public function test_rest_run_normalizes_input_once( string $method, array $annotations ): void {
2301+
add_filter(
2302+
'wp_ability_normalize_input',
2303+
static function ( $input ) {
2304+
return is_string( $input ) ? $input . '!' : $input;
2305+
}
2306+
);
2307+
$this->register_test_ability(
2308+
'test/normalize-once',
2309+
array(
2310+
'label' => 'Normalize Once',
2311+
'description' => 'Returns the normalized input.',
2312+
'category' => 'general',
2313+
'input_schema' => array(
2314+
'type' => 'string',
2315+
'maxLength' => 4,
2316+
),
2317+
'permission_callback' => '__return_true',
2318+
'execute_callback' => static function ( string $input ): string {
2319+
return $input;
2320+
},
2321+
'meta' => array(
2322+
'show_in_rest' => true,
2323+
'annotations' => $annotations,
2324+
),
2325+
)
2326+
);
2327+
2328+
$response = $this->dispatch_run( $method, 'test/normalize-once', 'raw' );
2329+
2330+
$this->assertSame( 200, $response->get_status() );
2331+
$this->assertSame( 'raw!', $response->get_data() );
2332+
}
21762333
}

0 commit comments

Comments
 (0)