Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +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 for
* updates or DELETE for deletes. All other abilities require POST.
*
* @since 6.9.0
* @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<string, (null|bool)> $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';
$allowed_methods = array( 'POST' );
if ( ! empty( $annotations['readonly'] ) ) {
$expected_method = 'GET';
$allowed_methods = array( 'GET' );
} elseif ( ! empty( $annotations['destructive'] ) && ! empty( $annotations['idempotent'] ) ) {
$expected_method = 'DELETE';
/*
* 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.
*/
$allowed_methods = array( 'PUT', 'DELETE' );
}

if ( $expected_method === $request_method ) {
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 ( 'DELETE' === $expected_method ) {
$error_message = __( 'Abilities that perform destructive actions require DELETE 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',
Expand All @@ -150,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 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.
Expand All @@ -172,6 +182,30 @@ public function check_ability_permissions( $request ) {
return $is_valid;
}

/*
* 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.
*/
$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 ) ) {
return $this->ensure_error_status( $input, 400 );
Expand Down Expand Up @@ -226,7 +260,7 @@ private function get_input_from_request( $request ) {
return $query_params['input'] ?? null;
}

// For POST 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;
}
Expand Down
191 changes: 185 additions & 6 deletions tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private function register_test_abilities(): void {
)
);

// Destructive ability (DELETE method).
// Destructive and idempotent ability (PUT method for updates, DELETE method for deletes).
$this->register_test_ability(
'test/delete-user',
array(
Expand Down Expand Up @@ -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 ) ) );
Expand All @@ -717,7 +718,159 @@ 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 for updates or DELETE method for deletes.', $data['message'] );
}

/**
* Data provider for the HTTP methods each set of annotations accepts.
*
* @return array<string, array{0: array<string, bool>, 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', 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 ),
'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<string, bool> $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 rejects input sent in the query string.
*
* @ticket 66801
*/
public function test_put_rejects_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( 400, $response->get_status() );
$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<string, array{0: bool}>
*/
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() );
}

/**
Expand Down Expand Up @@ -1781,10 +1934,29 @@ 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.
*
* 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 and PUT send it as a JSON body. Pass
* `null` to send no input at all.
*
* @param string $method HTTP method.
Expand Down Expand Up @@ -1816,6 +1988,13 @@ 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,
),
),
'DELETE' => array(
'DELETE',
array(
Expand Down
Loading