Skip to content

Commit e7ce9bb

Browse files
committed
REST API: Exit gracefully for malformed URLs.
Exit gracefully for requests with a malformed `rest_route` query string parameter, ie anything that is not a string. This prevents fatal errors from occurring with URLs such as `example.com/?rest_route[]=array` as the URL is user input so logging the data provides no benefit to developers as they are unable to resolve the issue. Props geekofshire, dd32, timothyblynjacobs. Fixes #62932. git-svn-id: https://develop.svn.wordpress.org/trunk@59886 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 83b9080 commit e7ce9bb

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/wp-includes/rest-api.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,16 @@ function rest_api_loaded() {
430430
return;
431431
}
432432

433+
// Return an error message if query_var is not a string.
434+
if ( ! is_string( $GLOBALS['wp']->query_vars['rest_route'] ) ) {
435+
$rest_type_error = new WP_Error(
436+
'rest_path_invalid_type',
437+
__( 'The rest route parameter must be a string.' ),
438+
array( 'status' => 400 )
439+
);
440+
wp_die( $rest_type_error );
441+
}
442+
433443
/**
434444
* Whether this is a REST Request.
435445
*

tests/phpunit/tests/rest-api.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,4 +2558,29 @@ public function test_route_args_is_array_of_arrays() {
25582558

25592559
$this->assertTrue( $registered );
25602560
}
2561+
2562+
/**
2563+
* @ticket 62932
2564+
*/
2565+
public function test_should_return_error_if_rest_route_not_string() {
2566+
global $wp;
2567+
2568+
$wp = new stdClass();
2569+
2570+
$wp->query_vars = array(
2571+
'rest_route' => array( 'invalid' ),
2572+
);
2573+
2574+
$this->expectException( WPDieException::class );
2575+
2576+
try {
2577+
rest_api_loaded();
2578+
} catch ( WPDieException $e ) {
2579+
$this->assertStringContainsString(
2580+
'The rest route parameter must be a string.',
2581+
$e->getMessage()
2582+
);
2583+
throw $e; // Re-throw to satisfy expectException
2584+
}
2585+
}
25612586
}

0 commit comments

Comments
 (0)