Skip to content

Commit 6398886

Browse files
committed
Add is_numeric() check and int cast for error status in rest_convert_error_to_response().
1 parent d88933d commit 6398886

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/wp-includes/rest-api.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3429,7 +3429,10 @@ function rest_convert_error_to_response( $error ) {
34293429
$error->get_all_error_data(),
34303430
static function ( $status, $error_data ) {
34313431
// Note: $error_data may not be an array (e.g. stdClass), so is_array() check is intentional.
3432-
return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status;
3432+
if ( is_array( $error_data ) && isset( $error_data['status'] ) && is_numeric( $error_data['status'] ) ) {
3433+
$status = (int) $error_data['status'];
3434+
}
3435+
return $status;
34333436
},
34343437
500
34353438
);

tests/phpunit/tests/rest-api/rest-server.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,19 @@ public function test_error_to_response_with_stdclass_data() {
605605
$this->assertSame( 500, $response->get_status() );
606606
}
607607

608+
/**
609+
* @ticket 64901
610+
*/
611+
public function test_error_to_response_with_non_numeric_status() {
612+
$error = new WP_Error( 'test', 'test', array( 'status' => 'forbidden' ) );
613+
614+
$response = rest_convert_error_to_response( $error );
615+
$this->assertInstanceOf( 'WP_REST_Response', $response );
616+
617+
// Non-numeric status should be ignored, status should default to 500.
618+
$this->assertSame( 500, $response->get_status() );
619+
}
620+
608621
public function test_rest_error() {
609622
$data = array(
610623
'code' => 'wp-api-test-error',

0 commit comments

Comments
 (0)