diff --git a/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php b/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php index 801a13c..9d88d34 100644 --- a/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php +++ b/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php @@ -1307,6 +1307,11 @@ public function validate_path( $value, $request, $param ) { * forward slash, made up of characters allowed in a URL path segment * (RFC 3986 `pchar`). * + * A site's path takes precedence over the files and routes beneath it, so + * the first segment after the network's base path must not be a reserved + * name such as `wp-json`, and no + * segment may name a PHP file such as `wp-login.php`. + * * @since 7.2.0 * * @param string $path The path to check. @@ -1317,10 +1322,23 @@ private function is_valid_path( $path ) { return false; } - return 1 === preg_match( - '#^/(?:(?:[A-Za-z0-9._~!$&\'()*+,;=:@-]|%[0-9A-Fa-f]{2})+/)*$#', - $path - ); + if ( 1 !== preg_match( '#^/(?:(?:[A-Za-z0-9._~!$&\'()*+,;=:@-]|%[0-9A-Fa-f]{2})+/)*$#', $path ) ) { + return false; + } + + // Sites are looked up case-insensitively, so /WP-JSON/ would take over /wp-json/ too. + $path = strtolower( $path ); + + // Reserved names apply to the first segment after the network's base path, e.g. /parent/wp-json/ on a network at /parent/. + $base = strtolower( get_network()->path ); + if ( str_starts_with( $path, $base ) ) { + $path = substr( $path, strlen( $base ) ); + } + + $segments = explode( '/', trim( $path, '/' ) ); + + // Every segment ends with a slash, so a segment naming a PHP file shows up as ".php/". + return ! in_array( $segments[0], get_subdirectory_reserved_names(), true ) && 1 !== preg_match( '#\.php/#i', $path ); } /** diff --git a/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php b/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php index b738056..19028c9 100644 --- a/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php +++ b/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php @@ -1812,10 +1812,15 @@ public function data_domain_validation() { * @covers ::create_item * @group ms-required * @dataProvider data_path_validation + * @dataProvider data_path_validation_on_nested_network */ - public function test_create_item_path_validation( $path, $is_valid ) { + public function test_create_item_path_validation( $path, $is_valid, $network_path = '/' ) { wp_set_current_user( self::$superadmin_id ); + $network = get_network(); + $orig_base = $network->path; + $network->path = $network_path; + $request = new WP_REST_Request( 'POST', '/wp/v2/sites' ); // A domain distinct from WP_TESTS_DOMAIN, so a root path doesn't collide with the network's main site. $request->set_param( 'domain', 'path-validation.example.org' ); @@ -1823,6 +1828,8 @@ public function test_create_item_path_validation( $path, $is_valid ) { $response = rest_get_server()->dispatch( $request ); + $network->path = $orig_base; + if ( $is_valid ) { $this->assertEquals( 201, $response->get_status(), "path '{$path}' should have been accepted." ); @@ -1870,13 +1877,26 @@ public function test_update_item_path_validation( $path, $is_valid ) { } } + /** + * Data provider for the `path` validation tests on a network at /parent/. + * + * @return array + */ + public function data_path_validation_on_nested_network() { + return array( + 'reserved name after base' => array( '/parent/wp-json/', false, '/parent/' ), + 'reserved name outside base' => array( '/wp-json/', false, '/parent/' ), + 'reserved name deeper nested' => array( '/parent/child/wp-json/', true, '/parent/' ), + ); + } + /** * Data provider for the `path` format validation tests. * * @return array */ public function data_path_validation() { - return array( + $data = array( 'root path' => array( '/', true ), 'single segment' => array( '/tempor/', true ), 'nested segments' => array( '/parent/child/', true ), @@ -1891,10 +1911,24 @@ public function data_path_validation() { 'path with a query string' => array( '/with?query/', false ), 'path with a fragment' => array( '/with#fragment/', false ), 'path with a double quote' => array( '/with"quote/', false ), + // A site on these paths would take over the REST API or the login page of the whole network. + 'reserved name, uppercase' => array( '/WP-JSON/', false ), + 'reserved name as parent' => array( '/wp-admin/child/', false ), + 'reserved name nested' => array( '/parent/wp-json/', true ), + 'login file' => array( '/wp-login.php/', false ), + 'php file, uppercase' => array( '/XMLRPC.PHP/', false ), + 'php file nested' => array( '/parent/wp-login.php/', false ), + 'php in the middle' => array( '/my.php.site/', true ), // The wp_blogs.path column is varchar(100). 'path at max length' => array( '/' . str_repeat( 'a', 98 ) . '/', true ), 'path over max length' => array( '/' . str_repeat( 'a', 99 ) . '/', false ), ); + + foreach ( get_subdirectory_reserved_names() as $name ) { + $data[ "reserved name {$name}" ] = array( "/{$name}/", false ); + } + + return $data; } /**