From 3dcecdb83956e1759e401ae78ffaff9211e6ad90 Mon Sep 17 00:00:00 2001 From: Sajid Date: Thu, 26 Nov 2026 11:21:51 +0530 Subject: [PATCH 1/3] Abilities API: Accept PUT for destructive and idempotent abilities in the run endpoint. Destructive and idempotent abilities now expect PUT, which carries the input in a JSON body. PATCH is also accepted, and DELETE stays accepted with the input in the query string for backward compatibility. See #66801. --- ...ss-wp-rest-abilities-v1-run-controller.php | 23 +++- .../wpRestAbilitiesV1RunController.php | 120 +++++++++++++++++- 2 files changed, 131 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php index 8ed345bbc2321..155ad8a49855a 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php @@ -114,29 +114,40 @@ public function execute_ability( $request ) { /** * Validates if the HTTP method matches the expected method for the ability based on its annotations. * + * Read-only abilities require GET. Destructive and idempotent abilities require PUT, and also + * accept PATCH and DELETE. All other abilities require POST. + * * @since 6.9.0 + * @since 7.3.0 Destructive and idempotent abilities require PUT, and also accept PATCH and DELETE. * * @param string $request_method The HTTP method of the request. * @param array $annotations The ability annotations. * @return true|WP_Error True on success, or WP_Error object on failure. */ public function validate_request_method( string $request_method, array $annotations ) { - $expected_method = 'POST'; + $expected_method = 'POST'; + $accepted_methods = array(); if ( ! empty( $annotations['readonly'] ) ) { $expected_method = 'GET'; } elseif ( ! empty( $annotations['destructive'] ) && ! empty( $annotations['idempotent'] ) ) { - $expected_method = 'DELETE'; + /* + * Updates and deletes share these annotations. PUT is idempotent by definition + * and carries the input in a JSON body. PATCH also suits partial updates, and + * DELETE stays accepted for clients written before PUT was supported. + */ + $expected_method = 'PUT'; + $accepted_methods = array( 'PATCH', 'DELETE' ); } - if ( $expected_method === $request_method ) { + if ( $expected_method === $request_method || in_array( $request_method, $accepted_methods, true ) ) { return true; } $error_message = __( 'Abilities that perform updates require POST method.' ); if ( 'GET' === $expected_method ) { $error_message = __( 'Read-only abilities require GET method.' ); - } elseif ( 'DELETE' === $expected_method ) { - $error_message = __( 'Abilities that perform destructive actions require DELETE method.' ); + } elseif ( 'PUT' === $expected_method ) { + $error_message = __( 'Idempotent destructive abilities require PUT method.' ); } return new WP_Error( 'rest_ability_invalid_method', @@ -226,7 +237,7 @@ private function get_input_from_request( $request ) { return $query_params['input'] ?? null; } - // For POST requests, look for 'input' in JSON body. + // For POST, PUT and PATCH requests, look for 'input' in JSON body. $json_params = $request->get_json_params(); return $json_params['input'] ?? null; } diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php index 0d3f14c01423c..43ee8a29e35df 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php @@ -239,7 +239,7 @@ private function register_test_abilities(): void { ) ); - // Destructive ability (DELETE method). + // Destructive and idempotent ability (PUT method, PATCH and DELETE also accepted). $this->register_test_ability( 'test/delete-user', array( @@ -702,12 +702,13 @@ public function test_readonly_ability_requires_get(): void { } /** - * Test HTTP method validation for destructive abilities. + * Test HTTP method validation for destructive and idempotent abilities. * * @ticket 64098 + * @ticket 66801 */ - public function test_destructive_ability_requires_delete(): void { - // Try POST on a destructive ability (should fail). + public function test_destructive_idempotent_ability_requires_put(): void { + // Try POST on a destructive and idempotent ability (should fail). $request = new WP_REST_Request( 'POST', '/wp-abilities/v1/abilities/test/delete-user/run' ); $request->set_header( 'Content-Type', 'application/json' ); $request->set_body( wp_json_encode( array( 'user_id' => 1 ) ) ); @@ -717,7 +718,100 @@ public function test_destructive_ability_requires_delete(): void { $this->assertSame( 405, $response->get_status() ); $data = $response->get_data(); $this->assertSame( 'rest_ability_invalid_method', $data['code'] ); - $this->assertSame( 'Abilities that perform destructive actions require DELETE method.', $data['message'] ); + $this->assertSame( 'Idempotent destructive abilities require PUT method.', $data['message'] ); + } + + /** + * Data provider for the HTTP methods each set of annotations accepts. + * + * @return array, 1: string, 2: bool}> Ability annotations, + * HTTP method, and whether it is accepted. + */ + public static function data_request_methods_for_annotations(): array { + $readonly = array( 'readonly' => true ); + $additive = array( 'destructive' => false ); + $additive_idempotent = array( + 'destructive' => false, + 'idempotent' => true, + ); + $destructive = array( 'destructive' => true ); + $destructive_idempotent = array( + 'destructive' => true, + 'idempotent' => true, + ); + + return array( + 'read-only with GET' => array( $readonly, 'GET', true ), + 'read-only with PUT' => array( $readonly, 'PUT', false ), + 'additive with POST' => array( $additive, 'POST', true ), + 'additive with PUT' => array( $additive, 'PUT', false ), + 'additive idempotent with POST' => array( $additive_idempotent, 'POST', true ), + 'additive idempotent with PUT' => array( $additive_idempotent, 'PUT', false ), + 'destructive with POST' => array( $destructive, 'POST', true ), + 'destructive with PUT' => array( $destructive, 'PUT', false ), + 'destructive with PATCH' => array( $destructive, 'PATCH', false ), + 'destructive with DELETE' => array( $destructive, 'DELETE', false ), + 'destructive idempotent with PUT' => array( $destructive_idempotent, 'PUT', true ), + 'destructive idempotent with PATCH' => array( $destructive_idempotent, 'PATCH', true ), + 'destructive idempotent with DELETE' => array( $destructive_idempotent, 'DELETE', true ), + 'destructive idempotent with POST' => array( $destructive_idempotent, 'POST', false ), + 'destructive idempotent with GET' => array( $destructive_idempotent, 'GET', false ), + 'destructive idempotent with HEAD' => array( $destructive_idempotent, 'HEAD', false ), + ); + } + + /** + * Tests which HTTP methods the run endpoint accepts for each set of annotations. + * + * @ticket 66801 + * + * @dataProvider data_request_methods_for_annotations + * + * @param array $annotations Ability annotations. + * @param string $method HTTP method. + * @param bool $is_accepted Whether the method is accepted. + */ + public function test_validate_request_method( array $annotations, string $method, bool $is_accepted ): void { + $controller = new WP_REST_Abilities_V1_Run_Controller(); + $result = $controller->validate_request_method( $method, $annotations ); + + if ( $is_accepted ) { + $this->assertTrue( $result ); + return; + } + + $this->assertWPError( $result ); + $this->assertSame( 'rest_ability_invalid_method', $result->get_error_code() ); + $this->assertSame( 405, $result->get_error_data()['status'] ); + } + + /** + * Tests that PUT does not read the input from the query string. + * + * @ticket 66801 + */ + public function test_put_ignores_input_in_query_string(): void { + $this->register_reflecting_ability( + 'test/update-title', + array( + 'type' => array( 'object', 'null' ), + 'properties' => array( + 'id' => array( 'type' => 'integer' ), + ), + ), + array( + 'destructive' => true, + 'idempotent' => true, + ) + ); + + $request = new WP_REST_Request( 'PUT', '/wp-abilities/v1/abilities/test/update-title/run' ); + $request->set_query_params( array( 'input' => array( 'id' => 5 ) ) ); + + $response = $this->server->dispatch( $request ); + + $this->assertSame( 200, $response->get_status() ); + $this->assertNull( $response->get_data()['value'] ); } /** @@ -1784,7 +1878,7 @@ private function register_reflecting_ability( string $name, array $input_schema, /** * Dispatches a run request for an ability, nesting the value under the `input` parameter. * - * GET and DELETE send the input as query parameters, POST sends it as a JSON body. Pass + * GET and DELETE send the input as query parameters, POST, PUT and PATCH send it as a JSON body. Pass * `null` to send no input at all. * * @param string $method HTTP method. @@ -1816,6 +1910,20 @@ public static function data_coercion_transports(): array { return array( 'GET' => array( 'GET', array( 'readonly' => true ) ), 'POST' => array( 'POST', array() ), + 'PUT' => array( + 'PUT', + array( + 'destructive' => true, + 'idempotent' => true, + ), + ), + 'PATCH' => array( + 'PATCH', + array( + 'destructive' => true, + 'idempotent' => true, + ), + ), 'DELETE' => array( 'DELETE', array( From b066059825e1ad1975c9a928ced4a319ddf16770 Mon Sep 17 00:00:00 2001 From: Sajid Date: Thu, 3 Dec 2026 19:31:33 +0530 Subject: [PATCH 2/3] Abilities API: Address review feedback on PUT support for the run endpoint. Destructive and idempotent abilities now accept PUT for updates and DELETE for deletes. PATCH is rejected because it is not idempotent per RFC 9110. The 405 error message now names both PUT and DELETE. A PUT request that sends the input in the query string returns a 400 error, so a destructive ability does not silently run with its default input. See #66801. Co-Authored-By: Claude Opus 5.5 --- ...ss-wp-rest-abilities-v1-run-controller.php | 43 ++++++++++++------- .../wpRestAbilitiesV1RunController.php | 23 ++++------ 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php index 155ad8a49855a..5812bc205b586 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php @@ -114,40 +114,38 @@ public function execute_ability( $request ) { /** * Validates if the HTTP method matches the expected method for the ability based on its annotations. * - * Read-only abilities require GET. Destructive and idempotent abilities require PUT, and also - * accept PATCH and DELETE. All other abilities require POST. + * Read-only abilities require GET. Destructive and idempotent abilities require PUT for + * updates or DELETE for deletes. All other abilities require POST. * * @since 6.9.0 - * @since 7.3.0 Destructive and idempotent abilities require PUT, and also accept PATCH and DELETE. + * @since 7.3.0 Destructive and idempotent abilities accept PUT for updates, in addition to DELETE for deletes. * * @param string $request_method The HTTP method of the request. * @param array $annotations The ability annotations. * @return true|WP_Error True on success, or WP_Error object on failure. */ public function validate_request_method( string $request_method, array $annotations ) { - $expected_method = 'POST'; - $accepted_methods = array(); + $allowed_methods = array( 'POST' ); if ( ! empty( $annotations['readonly'] ) ) { - $expected_method = 'GET'; + $allowed_methods = array( 'GET' ); } elseif ( ! empty( $annotations['destructive'] ) && ! empty( $annotations['idempotent'] ) ) { /* - * Updates and deletes share these annotations. PUT is idempotent by definition - * and carries the input in a JSON body. PATCH also suits partial updates, and - * DELETE stays accepted for clients written before PUT was supported. + * Updates and deletes share these annotations. PUT is for updates and carries + * the input in a JSON body. DELETE is for deletes and carries the input in the + * query string. */ - $expected_method = 'PUT'; - $accepted_methods = array( 'PATCH', 'DELETE' ); + $allowed_methods = array( 'PUT', 'DELETE' ); } - if ( $expected_method === $request_method || in_array( $request_method, $accepted_methods, true ) ) { + if ( in_array( $request_method, $allowed_methods, true ) ) { return true; } $error_message = __( 'Abilities that perform updates require POST method.' ); - if ( 'GET' === $expected_method ) { + if ( in_array( 'GET', $allowed_methods, true ) ) { $error_message = __( 'Read-only abilities require GET method.' ); - } elseif ( 'PUT' === $expected_method ) { - $error_message = __( 'Idempotent destructive abilities require PUT method.' ); + } elseif ( in_array( 'PUT', $allowed_methods, true ) ) { + $error_message = __( 'Idempotent destructive abilities require PUT method for updates or DELETE method for deletes.' ); } return new WP_Error( 'rest_ability_invalid_method', @@ -161,6 +159,7 @@ public function validate_request_method( string $request_method, array $annotati * * @since 6.9.0 * @since 7.2.0 Prepares the input read from the raw request. + * @since 7.3.0 Rejects PUT requests that send the input in the query string. * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has execution permission, WP_Error object otherwise. @@ -183,6 +182,18 @@ public function check_ability_permissions( $request ) { return $is_valid; } + /* + * PUT reads the input only from the JSON body. Reject input sent in the query string, + * so that a destructive ability does not silently run with its default input. + */ + if ( 'PUT' === $request->get_method() && isset( $request->get_query_params()['input'] ) ) { + return new WP_Error( + 'rest_ability_invalid_input_location', + __( 'PUT requests must send the input in the JSON body.' ), + array( 'status' => 400 ) + ); + } + $input = $ability->prepare_input( $this->get_input_from_request( $request ) ); if ( is_wp_error( $input ) ) { return $this->ensure_error_status( $input, 400 ); @@ -237,7 +248,7 @@ private function get_input_from_request( $request ) { return $query_params['input'] ?? null; } - // For POST, PUT and PATCH requests, look for 'input' in JSON body. + // For POST and PUT requests, look for 'input' in JSON body. $json_params = $request->get_json_params(); return $json_params['input'] ?? null; } diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php index 43ee8a29e35df..6f58680997901 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php @@ -239,7 +239,7 @@ private function register_test_abilities(): void { ) ); - // Destructive and idempotent ability (PUT method, PATCH and DELETE also accepted). + // Destructive and idempotent ability (PUT method for updates, DELETE method for deletes). $this->register_test_ability( 'test/delete-user', array( @@ -718,7 +718,7 @@ public function test_destructive_idempotent_ability_requires_put(): void { $this->assertSame( 405, $response->get_status() ); $data = $response->get_data(); $this->assertSame( 'rest_ability_invalid_method', $data['code'] ); - $this->assertSame( 'Idempotent destructive abilities require PUT method.', $data['message'] ); + $this->assertSame( 'Idempotent destructive abilities require PUT method for updates or DELETE method for deletes.', $data['message'] ); } /** @@ -752,7 +752,7 @@ public static function data_request_methods_for_annotations(): array { 'destructive with PATCH' => array( $destructive, 'PATCH', false ), 'destructive with DELETE' => array( $destructive, 'DELETE', false ), 'destructive idempotent with PUT' => array( $destructive_idempotent, 'PUT', true ), - 'destructive idempotent with PATCH' => array( $destructive_idempotent, 'PATCH', true ), + 'destructive idempotent with PATCH' => array( $destructive_idempotent, 'PATCH', false ), 'destructive idempotent with DELETE' => array( $destructive_idempotent, 'DELETE', true ), 'destructive idempotent with POST' => array( $destructive_idempotent, 'POST', false ), 'destructive idempotent with GET' => array( $destructive_idempotent, 'GET', false ), @@ -786,11 +786,11 @@ public function test_validate_request_method( array $annotations, string $method } /** - * Tests that PUT does not read the input from the query string. + * Tests that PUT rejects input sent in the query string. * * @ticket 66801 */ - public function test_put_ignores_input_in_query_string(): void { + public function test_put_rejects_input_in_query_string(): void { $this->register_reflecting_ability( 'test/update-title', array( @@ -810,8 +810,8 @@ public function test_put_ignores_input_in_query_string(): void { $response = $this->server->dispatch( $request ); - $this->assertSame( 200, $response->get_status() ); - $this->assertNull( $response->get_data()['value'] ); + $this->assertSame( 400, $response->get_status() ); + $this->assertSame( 'rest_ability_invalid_input_location', $response->get_data()['code'] ); } /** @@ -1878,7 +1878,7 @@ private function register_reflecting_ability( string $name, array $input_schema, /** * Dispatches a run request for an ability, nesting the value under the `input` parameter. * - * GET and DELETE send the input as query parameters, POST, PUT and PATCH send it as a JSON body. Pass + * GET and DELETE send the input as query parameters, POST and PUT send it as a JSON body. Pass * `null` to send no input at all. * * @param string $method HTTP method. @@ -1917,13 +1917,6 @@ public static function data_coercion_transports(): array { 'idempotent' => true, ), ), - 'PATCH' => array( - 'PATCH', - array( - 'destructive' => true, - 'idempotent' => true, - ), - ), 'DELETE' => array( 'DELETE', array( From ed61136f2b283a692c444c77d8946cc608957fa9 Mon Sep 17 00:00:00 2001 From: Sajid Date: Thu, 3 Dec 2026 20:03:02 +0530 Subject: [PATCH 3/3] Abilities API: Reject DELETE requests that send the input in the body. DELETE reads the input only from the query string. A DELETE request that sends `input` in a JSON or form body now returns a 400 error with the `rest_ability_invalid_input_location` code, so a destructive ability does not silently run with its default input. Body data without an `input` key is still accepted. See #66801. --- ...ss-wp-rest-abilities-v1-run-controller.php | 20 ++++- .../wpRestAbilitiesV1RunController.php | 78 +++++++++++++++++++ 2 files changed, 94 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php index 5812bc205b586..e17656b6820bb 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php @@ -159,7 +159,7 @@ public function validate_request_method( string $request_method, array $annotati * * @since 6.9.0 * @since 7.2.0 Prepares the input read from the raw request. - * @since 7.3.0 Rejects PUT requests that send the input in the query string. + * @since 7.3.0 Rejects PUT input in the query string and DELETE input in the body. * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has execution permission, WP_Error object otherwise. @@ -183,16 +183,28 @@ public function check_ability_permissions( $request ) { } /* - * PUT reads the input only from the JSON body. Reject input sent in the query string, - * so that a destructive ability does not silently run with its default input. + * PUT reads the input only from the JSON body, and DELETE only from the query string. + * Reject input sent in the other place, so that a destructive ability does not silently + * run with its default input. */ - if ( 'PUT' === $request->get_method() && isset( $request->get_query_params()['input'] ) ) { + $method = $request->get_method(); + if ( 'PUT' === $method && isset( $request->get_query_params()['input'] ) ) { return new WP_Error( 'rest_ability_invalid_input_location', __( 'PUT requests must send the input in the JSON body.' ), array( 'status' => 400 ) ); } + if ( + 'DELETE' === $method && + ( isset( $request->get_json_params()['input'] ) || isset( $request->get_body_params()['input'] ) ) + ) { + return new WP_Error( + 'rest_ability_invalid_input_location', + __( 'DELETE requests must send the input in the query string.' ), + array( 'status' => 400 ) + ); + } $input = $ability->prepare_input( $this->get_input_from_request( $request ) ); if ( is_wp_error( $input ) ) { diff --git a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php index 6f58680997901..24dcc5f8b69f0 100644 --- a/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php +++ b/tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php @@ -814,6 +814,65 @@ public function test_put_rejects_input_in_query_string(): void { $this->assertSame( 'rest_ability_invalid_input_location', $response->get_data()['code'] ); } + /** + * Tests that DELETE rejects input sent in the body. + * + * @ticket 66801 + * + * @dataProvider data_delete_body_transports + * + * @param bool $is_json Whether the body is sent as JSON instead of form data. + */ + public function test_delete_rejects_input_in_body( bool $is_json ): void { + $this->register_deleting_ability(); + + $request = new WP_REST_Request( 'DELETE', '/wp-abilities/v1/abilities/test/delete-post/run' ); + if ( $is_json ) { + $request->set_header( 'Content-Type', 'application/json' ); + $request->set_body( wp_json_encode( array( 'input' => array( 'id' => 5 ) ) ) ); + } else { + $request->set_body_params( array( 'input' => array( 'id' => 5 ) ) ); + } + + $response = $this->server->dispatch( $request ); + + $this->assertSame( 400, $response->get_status() ); + $data = $response->get_data(); + $this->assertSame( 'rest_ability_invalid_input_location', $data['code'] ); + $this->assertSame( 'DELETE requests must send the input in the query string.', $data['message'] ); + } + + /** + * Data provider for test_delete_rejects_input_in_body(). + * + * @return array + */ + public static function data_delete_body_transports(): array { + return array( + 'JSON body' => array( true ), + 'form body' => array( false ), + ); + } + + /** + * Tests that DELETE accepts a body without input, and reads the input from the query string. + * + * @ticket 66801 + */ + public function test_delete_accepts_body_without_input(): void { + $this->register_deleting_ability(); + + $request = new WP_REST_Request( 'DELETE', '/wp-abilities/v1/abilities/test/delete-post/run' ); + $request->set_query_params( array( 'input' => array( 'id' => 5 ) ) ); + $request->set_header( 'Content-Type', 'application/json' ); + $request->set_body( wp_json_encode( array( 'other' => 'value' ) ) ); + + $response = $this->server->dispatch( $request ); + + $this->assertSame( 200, $response->get_status() ); + $this->assertSame( array( 'value' => array( 'id' => 5 ) ), $response->get_data() ); + } + /** * Test output validation against schema. * Note: When output validation fails in WP_Ability::execute(), it returns null, @@ -1875,6 +1934,25 @@ private function register_reflecting_ability( string $name, array $input_schema, ); } + /** + * Registers a destructive and idempotent ability that reflects its input, for DELETE tests. + */ + private function register_deleting_ability(): void { + $this->register_reflecting_ability( + 'test/delete-post', + array( + 'type' => array( 'object', 'null' ), + 'properties' => array( + 'id' => array( 'type' => 'integer' ), + ), + ), + array( + 'destructive' => true, + 'idempotent' => true, + ) + ); + } + /** * Dispatches a run request for an ability, nesting the value under the `input` parameter. *