diff --git a/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php b/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php index 75c6414..a3c55f6 100644 --- a/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php +++ b/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php @@ -56,6 +56,12 @@ public function register_routes() { $create_args = $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ); $create_args['user'] = $this->get_user_param_schema( __( 'The site administrator, set when the site is created. Accepts a user ID or "me".' ) ); + // In a subdirectory install the path names the site, and the default "/" always belongs to the main site. + if ( is_multisite() && ! is_subdomain_install() ) { + unset( $create_args['path']['default'] ); + $create_args['path']['required'] = true; + } + register_rest_route( $this->namespace, '/' . $this->rest_base, @@ -87,8 +93,9 @@ public function register_routes() { array( 'args' => array( 'id' => array( - 'description' => __( 'Unique identifier for the object.' ), - 'type' => 'integer', + 'description' => __( 'Unique identifier for the object.' ), + 'type' => 'integer', + 'validate_callback' => array( $this, 'validate_id' ), ), ), array( @@ -815,27 +822,11 @@ public function delete_item( $request ) { ); if ( ! (bool) $request['force'] ) { - /** This action is documented in wp-includes/ms-site.php */ - do_action_deprecated( 'delete_blog', array( $blog_id, false ), '5.1.0' ); - - $users = get_users( - array( - 'blog_id' => $blog_id, - 'fields' => 'ids', - ) - ); - - // Remove users from this blog. - if ( ! empty( $users ) ) { - foreach ( $users as $user_id ) { - remove_user_from_blog( $user_id, $blog_id ); - } - } + // Flag the site for deletion like Network Admin does. The users stay, so it can be undone. + /** This action is documented in wp-admin/network/sites.php */ + do_action( 'deactivate_blog', $blog_id ); update_blog_status( $blog_id, 'deleted', '1' ); - - /** This action is documented in wp-includes/ms-site.php */ - do_action_deprecated( 'deleted_blog', array( $blog_id, false ), '5.1.0' ); } else { $result = wp_delete_site( $request['id'] ); @@ -1088,7 +1079,8 @@ protected function prepare_item_for_database( $request ) { } if ( isset( $request['path'] ) ) { - $prepared_site['path'] = $request['path']; + // Lowercased like Add New Site in Network Admin does. + $prepared_site['path'] = strtolower( $request['path'] ); } if ( isset( $request['domain'] ) ) { @@ -1240,7 +1232,12 @@ public function validate_path( $value, $request, $param ) { return new WP_Error( 'rest_invalid_param', - __( 'path must be a valid site path, starting and ending with a forward slash.' ), + sprintf( + /* translators: 1: Parameter name, 2: Example site path. */ + __( '%1$s must be "/" or a site name of letters, numbers and hyphens below the network path, such as %2$s, and must not be a reserved name.' ), + $param, + get_network()->path . 'my-site/' + ), array( 'status' => 400, 'param' => $param, @@ -1249,15 +1246,14 @@ public function validate_path( $value, $request, $param ) { } /** - * Checks whether a string is a valid site path: a forward slash, or a - * sequence of `/`-delimited segments each starting and ending with a - * forward slash, made up of characters allowed in a URL path segment - * (RFC 3986 `pchar`). + * Checks whether a string is a valid path for a new site. * - * 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`. + * Follows Add New Site in Network Admin: a site name of letters, numbers + * and hyphens below the network's base path, e.g. `/my-site/`. That leaves + * no room for encoded characters, dot segments or file names such as + * `wp-login.php` or `robots.txt`, which a site's path would take over. + * Routes like `/wp-json/` are excluded as reserved names. The root path + * `/` is valid too, for a site on its own domain. * * @since 7.2.0 * @@ -1265,28 +1261,17 @@ public function validate_path( $value, $request, $param ) { * @return bool True if `$path` is a valid site path, otherwise false. */ private function is_valid_path( $path ) { - if ( ! is_string( $path ) || '' === $path ) { - return false; - } - - if ( 1 !== preg_match( '#^/(?:(?:[A-Za-z0-9._~!$&\'()*+,;=:@-]|%[0-9A-Fa-f]{2})+/)*$#', $path ) ) { - return false; + if ( '/' === $path ) { + return true; } - // Sites are looked up case-insensitively, so /WP-JSON/ would take over /wp-json/ too. - // The server decodes the URL to find a file, so /wp-login%2ephp/ would take over /wp-login.php too. - $path = strtolower( rawurldecode( $path ) ); + $base = preg_quote( strtolower( get_network()->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 ) ); + if ( 1 !== preg_match( '#^' . $base . '([a-z0-9-]+)/$#D', strtolower( $path ), $matches ) ) { + return false; } - $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 ); + return ! in_array( $matches[1], get_subdirectory_reserved_names(), true ); } /** @@ -1334,6 +1319,38 @@ protected function check_url_is_available( $prepared_site, $request ) { return true; } + /** + * Validates the `id` parameter of the single site route. + * + * A body or query `id` takes precedence over the URL, so without this check + * a request to one site could read, change or delete another one. + * + * @since 7.2.0 + * + * @param mixed $value The merged `id` parameter. + * @param WP_REST_Request $request The current request. + * @param string $param The parameter name. + * @return true|WP_Error True if the ID is valid and matches the URL, WP_Error otherwise. + */ + public function validate_id( $value, $request, $param ) { + $valid = rest_validate_request_arg( $value, $request, $param ); + if ( is_wp_error( $valid ) ) { + return $valid; + } + + $url_params = $request->get_url_params(); + + if ( (int) $url_params['id'] !== (int) $value ) { + return new WP_Error( + 'rest_site_id_mismatch', + __( 'The site ID must match the ID in the URL.' ), + array( 'status' => 400 ) + ); + } + + return true; + } + /** * Checks the `meta` parameter before anything is written, so an invalid * value doesn't leave a half-created or half-updated site behind. @@ -1411,7 +1428,7 @@ public function get_item_schema() { 'description' => __( 'Site domain.' ), 'type' => 'string', 'context' => array( 'view', 'edit', 'embed' ), - 'default' => '', + 'required' => true, // The wp_blogs.domain column is varchar(200), port included. 'maxLength' => 200, 'arg_options' => array( diff --git a/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php b/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php index a222937..52707a9 100644 --- a/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php +++ b/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php @@ -520,6 +520,48 @@ public function test_create_item_rejects_an_existing_domain_and_path() { $this->assertErrorResponse( 'rest_site_taken', $response, 400 ); } + /** + * The domain has no usable default, so it is required. + * + * @ticket 40365 + * @covers ::create_item + * @group ms-required + */ + public function test_create_item_requires_domain() { + wp_set_current_user( self::$superadmin_id ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/sites' ); + $request->set_param( 'path', '/no-domain/' ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_missing_callback_param', $response, 400 ); + $this->assertSame( array( 'domain' ), $response->get_data()['data']['params'] ); + } + + /** + * In a subdirectory install the path names the site, so it is required. + * + * @ticket 40365 + * @covers ::create_item + * @group ms-required + */ + public function test_create_item_requires_path_in_subdirectory_install() { + if ( is_subdomain_install() ) { + $this->markTestSkipped( 'The path defaults to "/" in a subdomain install.' ); + } + + wp_set_current_user( self::$superadmin_id ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/sites' ); + $request->set_param( 'domain', WP_TESTS_DOMAIN ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_missing_callback_param', $response, 400 ); + $this->assertSame( array( 'path' ), $response->get_data()['data']['params'] ); + } + /** * @ticket 40365 * @covers ::update_item @@ -953,8 +995,8 @@ public function test_delete_item_uninitializes_the_site() { } /** - * Deleting without force marks the site as deleted and removes its users, - * but does not drop the site. + * Deleting without force marks the site as deleted, like flagging it for + * deletion in Network Admin, but does not drop the site or remove its users. * * @ticket 40365 * @covers ::delete_item @@ -982,8 +1024,89 @@ public function test_delete_item_requires_force() { $this->assertNotNull( $site ); $this->assertEquals( '1', $site->deleted ); - // Its users have been removed from the blog. - $this->assertFalse( is_user_member_of_blog( $user_id, $blog_id ) ); + $this->assertSame( 1, did_action( 'deactivate_blog' ) ); + + // Undeleting the site brings it back with its users. + $request = new WP_REST_Request( 'PUT', '/wp/v2/sites/' . $blog_id ); + $request->set_body_params( array( 'deleted' => false ) ); + $this->assertSame( 200, rest_get_server()->dispatch( $request )->get_status() ); + + $this->assertTrue( is_user_member_of_blog( $user_id, $blog_id ) ); + } + + /** + * An `id` in the body or query must not redirect a request to another site. + * + * @ticket 40365 + * @group ms-required + * + * @dataProvider data_mismatched_id_is_rejected + * + * @param string $method HTTP method. + * @param string $source Where the mismatched ID is sent: 'body' or 'query'. + */ + public function test_mismatched_id_is_rejected( $method, $source ) { + wp_set_current_user( self::$superadmin_id ); + + $blog_id = self::factory()->blog->create( array( 'path' => '/mismatch-target/' ) ); + $other_id = self::factory()->blog->create( array( 'path' => '/mismatch-other/' ) ); + + $request = new WP_REST_Request( $method, '/wp/v2/sites/' . $blog_id ); + if ( 'body' === $source ) { + $request->set_body_params( + array( + 'id' => $other_id, + 'public' => false, + ) + ); + } else { + $request->set_query_params( array( 'id' => $other_id ) ); + } + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); + $this->assertSame( '1', get_site( $other_id )->public ); + $this->assertSame( '0', get_site( $other_id )->deleted ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_mismatched_id_is_rejected() { + return array( + 'GET with query id' => array( 'GET', 'query' ), + 'PUT with body id' => array( 'PUT', 'body' ), + 'PUT with query id' => array( 'PUT', 'query' ), + 'DELETE with query id' => array( 'DELETE', 'query' ), + ); + } + + /** + * An `id` matching the URL, e.g. from a site fetched before, is accepted. + * + * @ticket 40365 + * @group ms-required + */ + public function test_update_item_with_matching_id() { + wp_set_current_user( self::$superadmin_id ); + + $blog_id = self::factory()->blog->create( array( 'path' => '/matching-id/' ) ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/sites/' . $blog_id ); + $request->set_body_params( + array( + 'id' => $blog_id, + 'public' => false, + ) + ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 200, $response->get_status() ); + $this->assertSame( '0', get_site( $blog_id )->public ); } /** @@ -1644,27 +1767,6 @@ public function test_get_items_orderby_id_list_without_a_list() { } } - /** - * The data is stored as sent, without added slashes. - * - * @ticket 40365 - * @covers ::update_item - * @group ms-required - */ - public function test_update_item_does_not_slash_the_stored_data() { - wp_set_current_user( self::$superadmin_id ); - - $blog_id = self::factory()->blog->create( array( 'path' => '/sit/' ) ); - - $request = new WP_REST_Request( 'PUT', '/wp/v2/sites/' . $blog_id ); - $request->set_param( 'path', "/o'brien/" ); - - $response = rest_get_server()->dispatch( $request ); - - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( "/o'brien/", get_site( $blog_id )->path ); - } - /** * The status fields are stored when a site is created. * @@ -1915,8 +2017,9 @@ public function test_create_item_path_validation( $path, $is_valid, $network_pat if ( $is_valid ) { $this->assertEquals( 201, $response->get_status(), "path '{$path}' should have been accepted." ); + // The path is lowercased like Add New Site does. $data = $response->get_data(); - $this->assertEquals( $path, $data['path'] ); + $this->assertEquals( strtolower( $path ), $data['path'] ); } else { $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } @@ -1951,8 +2054,8 @@ public function test_update_item_path_validation( $path, $is_valid ) { $this->assertEquals( 200, $response->get_status(), "path '{$path}' should have been accepted." ); $data = $response->get_data(); - $this->assertEquals( $path, $data['path'] ); - $this->assertEquals( $path, get_site( $blog_id )->path ); + $this->assertEquals( strtolower( $path ), $data['path'] ); + $this->assertEquals( strtolower( $path ), get_site( $blog_id )->path ); } else { $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); $this->assertEquals( $orig_path, get_site( $blog_id )->path ); @@ -1966,9 +2069,11 @@ public function test_update_item_path_validation( $path, $is_valid ) { */ public function data_path_validation_on_nested_network() { return array( + 'site name below base' => array( '/parent/child/', true, '/parent/' ), + 'site name outside base' => array( '/child/', false, '/parent/' ), '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/' ), + 'reserved name deeper nested' => array( '/parent/child/wp-json/', false, '/parent/' ), ); } @@ -1981,30 +2086,35 @@ public function data_path_validation() { $data = array( 'root path' => array( '/', true ), 'single segment' => array( '/tempor/', true ), - 'nested segments' => array( '/parent/child/', true ), - 'segment with apostrophe' => array( "/o'brien/", true ), - 'segment with punctuation' => array( '/with-hyphen_and.dot~tilde/', true ), - 'percent encoded segment' => array( '/percent%20encoded/', true ), + 'letters, numbers, hyphen' => array( '/site-2-go/', true ), + 'uppercase' => array( '/Tempor-Upper/', true ), 'empty path' => array( '', false ), 'no leading slash' => array( 'no-leading-slash/', false ), 'no trailing slash' => array( '/no-trailing-slash', false ), 'double slash' => array( '//double-slash//', false ), + // Like Add New Site in Network Admin, a site name is a single segment of letters, numbers and hyphens. + 'nested segments' => array( '/parent/child/', false ), + 'segment with punctuation' => array( '/with-hyphen_and.dot~tilde/', false ), + 'percent encoded segment' => array( '/percent%20encoded/', false ), 'path with a space' => array( '/with space/', false ), '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. + // PCRE's $ also matches before a final newline. + 'trailing newline' => array( "/trailing-newline/\n", false ), + // A site on these paths would take over the REST API, the login page or other files 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 ), + 'reserved name nested' => array( '/parent/wp-json/', false ), '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 ), 'php file, encoded dot' => array( '/wp-login%2ephp/', false ), 'reserved name, encoded' => array( '/%77p-json/', false ), 'reserved name, encoded /' => array( '/wp-json%2F/', false ), - 'encoded, not reserved' => array( '/caf%C3%A9/', true ), + 'robots file' => array( '/robots.txt/', false ), + // Browsers resolve dot segments, so this site could never be reached. + 'double dot, encoded' => array( '/parent/%2e%2E/', false ), // 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 ),