Skip to content

Commit 0952cc9

Browse files
Connectors: Match the settings route the way the REST server does.
`_wp_connectors_rest_settings_dispatch()` now matches the request route with the REST server's own pattern, to fix possible inconsistencies with the server's route matching. Follow-up to [61749], [62684]. Props mcliwanow, jorgefilipecosta. Fixes #66330. git-svn-id: https://develop.svn.wordpress.org/trunk@64432 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 70f53fb commit 0952cc9

2 files changed

Lines changed: 142 additions & 2 deletions

File tree

‎src/wp-includes/connectors.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,8 @@ function wp_connectors_sanitize_application_password_credentials( $value, string
696696
* @return WP_REST_Response The modified response with masked/validated keys.
697697
*/
698698
function _wp_connectors_rest_settings_dispatch( WP_REST_Response $response, WP_REST_Server $server, WP_REST_Request $request ): WP_REST_Response {
699-
if ( '/wp/v2/settings' !== $request->get_route() ) {
699+
// Match the route the way WP_REST_Server::match_request_to_handler() does, ignoring case.
700+
if ( ! preg_match( '@^/wp/v2/settings$@i', $request->get_route() ) ) {
700701
return $response;
701702
}
702703

‎tests/phpunit/tests/connectors/wpConnectorsRestSettingsDispatch.php‎

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ class Tests_Connectors_WpConnectorsRestSettingsDispatch extends WP_UnitTestCase
1616
const CREDENTIALS_SETTING_NAME = 'connectors_test_remote_credentials';
1717
const AI_KEY_SETTING_NAME = 'connectors_ai_mock_connectors_test_api_key';
1818

19+
/**
20+
* Administrator user ID.
21+
*
22+
* @var int
23+
*/
24+
private static $administrator_id;
25+
1926
/**
2027
* Registers the mock AI provider connector once before any tests in this class run.
2128
*/
@@ -32,6 +39,22 @@ public static function tear_down_after_class(): void {
3239
parent::tear_down_after_class();
3340
}
3441

42+
/**
43+
* Creates an administrator for REST settings requests.
44+
*
45+
* @param WP_UnitTest_Factory $factory Test factory.
46+
*/
47+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
48+
self::$administrator_id = $factory->user->create( array( 'role' => 'administrator' ) );
49+
}
50+
51+
/**
52+
* Removes the administrator.
53+
*/
54+
public static function wpTearDownAfterClass() {
55+
self::delete_user( self::$administrator_id );
56+
}
57+
3558
/**
3659
* Registers an application password connector before each test.
3760
*/
@@ -54,14 +77,16 @@ public function set_up(): void {
5477
}
5578

5679
/**
57-
* Removes the test connector after each test.
80+
* Removes the test connector and its setting after each test.
5881
*/
5982
public function tear_down(): void {
6083
$registry = WP_Connector_Registry::get_instance();
6184
if ( null !== $registry && $registry->is_registered( self::CONNECTOR_ID ) ) {
6285
$registry->unregister( self::CONNECTOR_ID );
6386
}
6487

88+
unregister_setting( 'connectors', self::CREDENTIALS_SETTING_NAME );
89+
6590
parent::tear_down();
6691
}
6792

@@ -183,4 +208,118 @@ public function test_keeps_and_masks_submitted_valid_ai_key(): void {
183208
'The submitted AI provider key should be masked in the response.'
184209
);
185210
}
211+
212+
/**
213+
* Ensures credentials are masked for every spelling of the settings route
214+
* that the REST server matches.
215+
*
216+
* @ticket 66330
217+
*
218+
* @dataProvider data_settings_route_spellings
219+
*
220+
* @param string $route Requested settings route.
221+
*/
222+
public function test_masks_credentials_for_every_settings_route_spelling( string $route ): void {
223+
$response = new WP_REST_Response(
224+
array(
225+
self::AI_KEY_SETTING_NAME => 'sk-stored-valid-key',
226+
self::CREDENTIALS_SETTING_NAME => array(
227+
'username' => 'remote-user',
228+
'password' => 'abcd efgh ijkl mnop 1234',
229+
),
230+
)
231+
);
232+
$request = new WP_REST_Request( 'GET', $route );
233+
234+
$result = _wp_connectors_rest_settings_dispatch( $response, rest_get_server(), $request );
235+
$data = $result->get_data();
236+
237+
$this->assertSame(
238+
_wp_connectors_mask_api_key( 'sk-stored-valid-key' ),
239+
$data[ self::AI_KEY_SETTING_NAME ],
240+
'The AI provider key should be masked.'
241+
);
242+
$this->assertSame(
243+
str_repeat( "\u{2022}", 16 ),
244+
$data[ self::CREDENTIALS_SETTING_NAME ]['password'],
245+
'The application password should be masked.'
246+
);
247+
}
248+
249+
/**
250+
* Ensures settings endpoint responses mask credentials for every `rest_route`
251+
* query variable value that the REST server dispatches to the settings controller.
252+
*
253+
* @ticket 66330
254+
*
255+
* @dataProvider data_settings_rest_route_query_values
256+
*
257+
* @param string $rest_route Raw `rest_route` query variable value.
258+
*/
259+
public function test_settings_endpoint_masks_credentials_for_every_rest_route_spelling( string $rest_route ): void {
260+
// Parse the request URL without serving it, then read the route as rest_api_loaded() does.
261+
remove_action( 'parse_request', 'rest_api_loaded' );
262+
rest_api_init();
263+
$this->go_to( home_url( '/?rest_route=' . $rest_route ) );
264+
$route = untrailingslashit( $GLOBALS['wp']->query_vars['rest_route'] );
265+
266+
wp_set_current_user( self::$administrator_id );
267+
_wp_register_default_connector_settings();
268+
269+
update_option( self::AI_KEY_SETTING_NAME, 'sk-stored-valid-key' );
270+
update_option(
271+
self::CREDENTIALS_SETTING_NAME,
272+
array(
273+
'username' => 'remote-user',
274+
'password' => 'abcd efgh ijkl mnop 1234',
275+
)
276+
);
277+
278+
// Apply `rest_post_dispatch` the way WP_REST_Server::serve_request() does.
279+
$request = new WP_REST_Request( 'GET', $route );
280+
$response = apply_filters( 'rest_post_dispatch', rest_ensure_response( rest_do_request( $request ) ), rest_get_server(), $request );
281+
$data = $response->get_data();
282+
283+
$this->assertSame( 200, $response->get_status(), 'The settings controller should handle the request.' );
284+
$this->assertSame(
285+
_wp_connectors_mask_api_key( 'sk-stored-valid-key' ),
286+
$data[ self::AI_KEY_SETTING_NAME ],
287+
'The AI provider key should be masked in the settings response.'
288+
);
289+
$this->assertSame(
290+
str_repeat( "\u{2022}", 16 ),
291+
$data[ self::CREDENTIALS_SETTING_NAME ]['password'],
292+
'The application password should be masked in the settings response.'
293+
);
294+
}
295+
296+
/**
297+
* Data provider for spellings of the settings route that the REST server
298+
* dispatches to the settings controller.
299+
*
300+
* @return array<string, array{string}> Test cases.
301+
*/
302+
public function data_settings_route_spellings(): array {
303+
return array(
304+
'lowercase' => array( '/wp/v2/settings' ),
305+
'mixed case' => array( '/wp/v2/Settings' ),
306+
'uppercase' => array( '/WP/V2/SETTINGS' ),
307+
'trailing newline' => array( "/wp/v2/settings\n" ),
308+
);
309+
}
310+
311+
/**
312+
* Data provider for raw `rest_route` query variable values that the REST
313+
* server dispatches to the settings controller.
314+
*
315+
* @return array<string, array{string}> Test cases.
316+
*/
317+
public function data_settings_rest_route_query_values(): array {
318+
return array(
319+
'lowercase' => array( '/wp/v2/settings' ),
320+
'mixed case' => array( '/wp/v2/Settings' ),
321+
'uppercase' => array( '/WP/V2/SETTINGS' ),
322+
'encoded trailing newline' => array( '/wp/v2/settings%0A' ),
323+
);
324+
}
186325
}

0 commit comments

Comments
 (0)