Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Validate rest_route Query Var to Ensure String Type #8287

Closed
wants to merge 7 commits into from
Closed
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
10 changes: 10 additions & 0 deletions src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,16 @@ function rest_api_loaded() {
return;
}

// Return an error message if query_var is not a string.
if ( ! is_string( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
$rest_type_error = new WP_Error(
'rest_path_invalid_type',
__( 'The rest route parameter must be a string.' ),
array( 'status' => 400 )
);
wp_die( $rest_type_error );
}

/**
* Whether this is a REST Request.
*
Expand Down
25 changes: 25 additions & 0 deletions tests/phpunit/tests/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2558,4 +2558,29 @@ public function test_route_args_is_array_of_arrays() {

$this->assertTrue( $registered );
}

/**
* @ticket 62932
*/
public function test_should_return_error_if_rest_route_not_string() {
global $wp;

$wp = new stdClass();

$wp->query_vars = array(
'rest_route' => array( 'invalid' ),
);

$this->expectException( WPDieException::class );

try {
rest_api_loaded();
} catch ( WPDieException $e ) {
$this->assertStringContainsString(
'The rest route parameter must be a string.',
$e->getMessage()
);
throw $e; // Re-throw to satisfy expectException
}
}
}
Loading