From dad03c0cf1d2215ea76cd83909df4a227b7dd4c5 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Mon, 2 Nov 2026 13:23:15 +0100 Subject: [PATCH 1/4] Alignment - REST API: Restrict the sites endpoint to the main site and network admins. --- modules/RestSitesEndpoint/About.php | 1 + .../class-wp-rest-sites-controller.php | 198 +++++++++--------- 2 files changed, 96 insertions(+), 103 deletions(-) diff --git a/modules/RestSitesEndpoint/About.php b/modules/RestSitesEndpoint/About.php index f2c017d..0295478 100644 --- a/modules/RestSitesEndpoint/About.php +++ b/modules/RestSitesEndpoint/About.php @@ -31,6 +31,7 @@ public static function get(): Summary { ), array( 'https://core.trac.wordpress.org/ticket/40365', + 'https://github.com/WordPress/wordpress-develop/pull/13055', ) ); } diff --git a/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php b/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php index 9d88d34..75c6414 100644 --- a/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php +++ b/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php @@ -137,81 +137,29 @@ public function get_items_permissions_check( $request ) { return $multisite_support; } - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - - $can_edit = $this->check_edit_permission(); - $is_own = $this->is_own_user_filter( $request ); - - if ( 'edit' === $context && ! $can_edit ) { - return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit sites.' ), array( 'status' => rest_authorization_required_code() ) ); - } - - // Only users who can manage sites may list the sites of another user. - if ( ! empty( $request['user'] ) && ! $is_own && ! $can_edit ) { - return new WP_Error( 'rest_forbidden_user', __( 'Sorry, you are not allowed to list the sites of other users.' ), array( 'status' => rest_authorization_required_code() ) ); - } - - // Fields outside the view context must not be inferable through filters or ordering either. - if ( ! $can_edit && $this->has_edit_only_filter( $request ) ) { - return new WP_Error( 'rest_forbidden_param', __( 'Sorry, you are not allowed to filter or order sites by these parameters.' ), array( 'status' => 403 ) ); - } - - // A user may list their own sites from any site, but only in the view context. - if ( 'view' === $context && $is_own ) { - return true; - } - - if ( ! is_main_site() ) { - return new WP_Error( 'rest_cannot_view_not_on_main_site', __( 'Sorry, sites can only be viewed from the main site.' ), array( 'status' => 403 ) ); - } - - if ( $can_edit ) { - return true; - } - - return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view sites.' ), array( 'status' => rest_authorization_required_code() ) ); + return $this->check_read_permission(); } /** - * Checks whether the request filters or orders by fields outside the view context. + * Checks that the request is served by the main site and the user can manage sites. * - * Users listing only their own sites do not see these fields, so filtering - * or ordering by them would give their values away. + * Sites are only readable from the main site, so the request runs with the + * same plugins as the network admin. * * @since 7.2.0 * - * @param WP_REST_Request $request Full details about the request. - * @return bool Whether the request uses a filter that needs the edit context. + * @return true|WP_Error True if the request has read access, error object otherwise. */ - protected function has_edit_only_filter( $request ) { - $params = array( 'archived', 'mature', 'spam', 'deleted', 'lang_id', 'lang_id_exclude', 'before', 'after' ); - - foreach ( $params as $param ) { - // The language filters default to an empty list, which does not filter. - if ( isset( $request[ $param ] ) && array() !== $request[ $param ] ) { - return true; - } + protected function check_read_permission() { + if ( ! is_main_site() ) { + return new WP_Error( 'rest_cannot_view_not_on_main_site', __( 'Sorry, sites can only be viewed from the main site.' ), array( 'status' => 403 ) ); } - return in_array( $request['orderby'], array( 'registered', 'last_updated' ), true ); - } - - /** - * Checks whether the request is limited to the sites of the current user. - * - * @since 7.2.0 - * - * @param WP_REST_Request $request Full details about the request. - * @return bool Whether the request asks for the current user's own sites. - */ - protected function is_own_user_filter( $request ) { - $user = $request['user']; - - if ( empty( $user ) ) { - return false; + if ( ! $this->check_edit_permission() ) { + return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view sites.' ), array( 'status' => rest_authorization_required_code() ) ); } - return is_user_logged_in() && get_current_user_id() === $this->get_user_id_from_param( $user ); + return true; } /** @@ -501,21 +449,9 @@ public function get_item_permissions_check( $request ) { } // Check capabilities before looking up the site, so unauthorized users can't probe which site IDs exist. - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - - if ( 'edit' === $context && ! $this->check_edit_permission() ) { - return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit sites.' ), array( 'status' => rest_authorization_required_code() ) ); - } - - $is_member = 'view' === $context && is_user_member_of_blog( get_current_user_id(), (int) $request['id'] ); - - // Members may read their own sites from any site, everything else needs the main site. - if ( ! $is_member && ! is_main_site() ) { - return new WP_Error( 'rest_cannot_view_not_on_main_site', __( 'Sorry, sites can only be viewed from the main site.' ), array( 'status' => 403 ) ); - } - - if ( ! $is_member && ! $this->check_edit_permission() ) { - return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to view sites.' ), array( 'status' => rest_authorization_required_code() ) ); + $read_permission = $this->check_read_permission(); + if ( is_wp_error( $read_permission ) ) { + return $read_permission; } $site = $this->get_site( $request['id'] ); @@ -615,6 +551,11 @@ public function create_item( $request ) { return new WP_Error( 'rest_site_exists', __( 'Cannot create existing site.' ), array( 'status' => 400 ) ); } + $meta_check = $this->check_meta( $request ); + if ( is_wp_error( $meta_check ) ) { + return $meta_check; + } + $prepared_site = $this->prepare_item_for_database( $request ); if ( is_wp_error( $prepared_site ) ) { return $prepared_site; @@ -663,18 +604,7 @@ public function create_item( $request ) { */ do_action( 'rest_insert_site', $site, $request, true ); - $schema = $this->get_item_schema(); - if ( isset( $request['meta'] ) ) { - if ( empty( $schema['properties']['meta'] ) ) { - return new WP_Error( - 'rest_site_meta_not_supported', - /* translators: %s: database table name */ - sprintf( __( 'The %s table is not installed. Please run the network database upgrade.' ), $GLOBALS['wpdb']->blogmeta ), - array( 'status' => 400 ) - ); - } - $meta_update = $this->meta->update_value( $request['meta'], $site_id ); if ( is_wp_error( $meta_update ) ) { @@ -731,6 +661,16 @@ public function update_item_permissions_check( $request ) { return new WP_Error( 'rest_unable_update_from_network', __( 'Sorry, you are not allowed to edit sites on another network.' ), array( 'status' => rest_authorization_required_code() ) ); } + // Archiving, spamming, deleting or moving the main site takes the whole network down. + if ( get_main_site_id( (int) $site->site_id ) === (int) $site->blog_id ) { + $takes_down = ! empty( $request['archived'] ) || ! empty( $request['spam'] ) || ! empty( $request['deleted'] ); + $moves = ( isset( $request['domain'] ) && $request['domain'] !== $site->domain ) || ( isset( $request['path'] ) && $request['path'] !== $site->path ); + + if ( $takes_down || $moves ) { + return new WP_Error( 'rest_cannot_edit_main_site', __( 'Sorry, the main site of a network cannot be archived, spammed, deleted or moved.' ), array( 'status' => 403 ) ); + } + } + return true; } @@ -750,6 +690,11 @@ public function update_item( $request ) { $id = (int) $site->blog_id; + $meta_check = $this->check_meta( $request ); + if ( is_wp_error( $meta_check ) ) { + return $meta_check; + } + $prepared_args = $this->prepare_item_for_database( $request ); if ( is_wp_error( $prepared_args ) ) { @@ -763,6 +708,18 @@ public function update_item( $request ) { return $result; } + + // Move home and siteurl along with the domain and path, as Network Admin does, + // unless they point elsewhere on purpose, e.g. to a mapped domain. + $new_site = get_site( $id ); + $old_url = untrailingslashit( $site->domain . $site->path ); + $new_url = untrailingslashit( $new_site->domain . $new_site->path ); + foreach ( array( 'home', 'siteurl' ) as $option ) { + $url = (string) get_blog_option( $id, $option ); + if ( untrailingslashit( (string) preg_replace( '#^https?://#i', '', $url ) ) === $old_url ) { + update_blog_option( $id, $option, wp_parse_url( $url, PHP_URL_SCHEME ) . '://' . $new_url ); + } + } } $site = get_site( $id ); @@ -770,18 +727,7 @@ public function update_item( $request ) { /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-sites-controller.php */ do_action( 'rest_insert_site', $site, $request, false ); - $schema = $this->get_item_schema(); - if ( isset( $request['meta'] ) ) { - if ( empty( $schema['properties']['meta'] ) ) { - return new WP_Error( - 'rest_site_meta_not_supported', - /* translators: %s: database table name */ - sprintf( __( 'The %s table is not installed. Please run the network database upgrade.' ), $GLOBALS['wpdb']->blogmeta ), - array( 'status' => 400 ) - ); - } - $meta_update = $this->meta->update_value( $request['meta'], $id ); if ( is_wp_error( $meta_update ) ) { @@ -1287,7 +1233,8 @@ public function validate_path( $value, $request, $param ) { return $valid; } - if ( $this->is_valid_path( $value ) ) { + // Arguments are validated before the permission check, and the path check needs the multisite functions. + if ( ! is_multisite() || $this->is_valid_path( $value ) ) { return true; } @@ -1327,7 +1274,8 @@ private function is_valid_path( $path ) { } // Sites are looked up case-insensitively, so /WP-JSON/ would take over /wp-json/ too. - $path = strtolower( $path ); + // The server decodes the URL to find a file, so /wp-login%2ephp/ would take over /wp-login.php too. + $path = strtolower( rawurldecode( $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 ); @@ -1386,6 +1334,50 @@ protected function check_url_is_available( $prepared_site, $request ) { 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. + * + * @since 7.2.0 + * + * @param WP_REST_Request $request The current request. + * @return true|WP_Error True if the meta can be written, WP_Error otherwise. + */ + protected function check_meta( $request ) { + if ( ! isset( $request['meta'] ) ) { + return true; + } + + $schema = $this->get_item_schema(); + + if ( empty( $schema['properties']['meta'] ) ) { + return new WP_Error( + 'rest_site_meta_not_supported', + /* translators: %s: database table name */ + sprintf( __( 'The %s table is not installed. Please run the network database upgrade.' ), $GLOBALS['wpdb']->blogmeta ), + array( 'status' => 400 ) + ); + } + + $properties = $schema['properties']['meta']['properties']; + + foreach ( (array) $request['meta'] as $name => $value ) { + // Null deletes the value, and update_value() ignores unregistered names. + if ( null === $value || ! isset( $properties[ $name ] ) ) { + continue; + } + + $valid = rest_validate_value_from_schema( $value, $properties[ $name ], 'meta.' . $name ); + if ( is_wp_error( $valid ) ) { + $valid->add_data( array( 'status' => 400 ) ); + + return $valid; + } + } + + return true; + } + /** * Retrieves the site's schema, conforming to JSON Schema. * @@ -1540,7 +1532,7 @@ public function get_item_schema() { if ( is_site_meta_supported() ) { $schema['properties']['meta'] = $this->meta->get_field_schema(); - // Registered site meta may hold sensitive data, so it is not shown to site members. + // Registered site meta may hold sensitive data, so it is only in the edit context. $schema['properties']['meta']['context'] = array( 'edit' ); } From b86475535d3ba30855683273e88c0e56b179cea6 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Mon, 2 Nov 2026 13:48:13 +0100 Subject: [PATCH 2/4] Tests for removed sites --- .../tests/core/wpRestSiteMetaFields.php | 60 +++- .../tests/core/wpRestSitesController.php | 312 ++++++++---------- 2 files changed, 194 insertions(+), 178 deletions(-) diff --git a/modules/RestSitesEndpoint/tests/core/wpRestSiteMetaFields.php b/modules/RestSitesEndpoint/tests/core/wpRestSiteMetaFields.php index da0232e..fbb21bd 100644 --- a/modules/RestSitesEndpoint/tests/core/wpRestSiteMetaFields.php +++ b/modules/RestSitesEndpoint/tests/core/wpRestSiteMetaFields.php @@ -456,6 +456,56 @@ public function test_update_item_rejects_meta_when_site_meta_is_unsupported() { $this->assertErrorResponse( 'rest_site_meta_not_supported', $response, 400 ); } + /** + * Invalid meta must not leave a half-created site behind. + * + * @covers ::create_item + */ + public function test_create_item_with_invalid_meta_creates_no_site() { + $this->grant_write_permission(); + + $request = new WP_REST_Request( 'POST', '/wp/v2/sites' ); + $request->set_param( 'domain', WP_TESTS_DOMAIN ); + $request->set_param( 'path', '/invalid-meta/' ); + $request->set_param( + 'meta', + array( + 'test_single' => 'valid', + 'test_custom_schema' => 'not a number', + ) + ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_invalid_type', $response, 400 ); + $this->assertNull( domain_exists( WP_TESTS_DOMAIN, '/invalid-meta/' ) ); + } + + /** + * Invalid meta must not leave a half-updated site behind. + * + * @covers ::update_item + */ + public function test_update_item_with_invalid_meta_changes_nothing() { + $this->grant_write_permission(); + + $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/sites/%d', self::$site_id ) ); + $request->set_param( 'archived', true ); + $request->set_param( + 'meta', + array( + 'test_single' => 'valid', + 'test_custom_schema_multi' => array( 'not a number' ), + ) + ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_invalid_type', $response, 400 ); + $this->assertSame( '0', get_site( self::$site_id )->archived ); + $this->assertSame( '', get_site_meta( self::$site_id, 'test_single', true ) ); + } + /** * @depends test_get_value * @covers ::update_item @@ -482,18 +532,16 @@ public function test_set_duplicate_single_value() { } /** - * Registered site meta may hold sensitive data, so site members reading - * their own site in the view context do not get it. + * Registered site meta may hold sensitive data, so it is left out of the + * view context. * * @ticket 40365 * @covers WP_REST_Sites_Controller::prepare_item_for_response */ - public function test_site_member_does_not_see_meta_in_view_context() { + public function test_meta_is_not_in_view_context() { update_site_meta( self::$site_id, 'test_single', 'testvalue' ); - $member_id = self::factory()->user->create(); - add_user_to_blog( self::$site_id, $member_id, 'subscriber' ); - wp_set_current_user( $member_id ); + wp_set_current_user( self::$superadmin_id ); $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/sites/%d', self::$site_id ) ); $response = rest_get_server()->dispatch( $request ); diff --git a/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php b/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php index 19028c9..a222937 100644 --- a/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php +++ b/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php @@ -217,8 +217,7 @@ public function test_get_items_forbidden_when_logged_out() { } /** - * The `user=me` own-sites filter requires being logged in, same as any - * other collection request - it is not an anonymous-access bypass. + * The `user=me` filter does not let a logged-out request through. * * @ticket 40365 * @covers ::get_items_permissions_check @@ -230,8 +229,7 @@ public function test_get_items_me_filter_forbidden_when_logged_out() { $response = rest_get_server()->dispatch( $request ); - // Logged out, "me" is no one, so it is treated like another user's ID. - $this->assertErrorResponse( 'rest_forbidden_user', $response, 401 ); + $this->assertErrorResponse( 'rest_forbidden_context', $response, 401 ); } /** @@ -308,14 +306,13 @@ public function test_get_item_edit_context_forbidden_when_logged_out() { } /** - * A member of the site (even without `manage_sites`) can view it in - * the default view context. + * Being a member of the site is not enough to read it, it needs `manage_sites`. * * @ticket 40365 * @covers ::get_item_permissions_check * @group ms-required */ - public function test_get_item_view_context_allowed_for_site_member() { + public function test_get_item_view_context_forbidden_for_site_member() { $blog_id = self::factory()->blog->create(); $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); add_user_to_blog( $blog_id, $user_id, 'subscriber' ); @@ -325,7 +322,7 @@ public function test_get_item_view_context_allowed_for_site_member() { $request = new WP_REST_Request( 'GET', '/wp/v2/sites/' . $blog_id ); $response = rest_get_server()->dispatch( $request ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertErrorResponse( 'rest_forbidden_context', $response, 403 ); } /** @@ -1010,6 +1007,63 @@ public function test_delete_main_site_is_not_allowed() { $this->assertNotNull( get_site( $main_site_id ) ); } + /** + * Archiving, spamming, deleting or moving the main site would take the whole network down. + * + * @ticket 40365 + * @covers ::update_item_permissions_check + * @group ms-required + * @dataProvider data_update_main_site + */ + public function test_update_main_site( $params, $is_allowed ) { + wp_set_current_user( self::$superadmin_id ); + + $main_site = get_site( get_main_site_id() ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/sites/' . $main_site->id ); + $request->set_body_params( $params ); + + $response = rest_get_server()->dispatch( $request ); + + if ( $is_allowed ) { + $this->assertSame( 200, $response->get_status() ); + } else { + $this->assertErrorResponse( 'rest_cannot_edit_main_site', $response, 403 ); + } + + $site = get_site( $main_site->id ); + $this->assertSame( '0', $site->archived ); + $this->assertSame( '0', $site->spam ); + $this->assertSame( '0', $site->deleted ); + $this->assertSame( $main_site->domain, $site->domain ); + $this->assertSame( $main_site->path, $site->path ); + } + + /** + * Data provider for test_update_main_site(). + * + * @return array + */ + public function data_update_main_site() { + return array( + 'archive' => array( array( 'archived' => true ), false ), + 'spam' => array( array( 'spam' => true ), false ), + 'delete' => array( array( 'deleted' => true ), false ), + 'other domain' => array( array( 'domain' => 'moved.example.org' ), false ), + 'other path' => array( array( 'path' => '/moved/' ), false ), + 'unchanged values' => array( + array( + 'archived' => false, + 'spam' => false, + 'deleted' => false, + 'domain' => WP_TESTS_DOMAIN, + 'path' => '/', + ), + true, + ), + ); + } + /** * A regular site administrator does not have the network-level * capability required to delete a site. @@ -1389,7 +1443,7 @@ public function test_create_item_does_not_accept_the_network_field() { */ public function test_get_items_me_filter_reports_the_filtered_total() { $blog_ids = self::factory()->blog->create_many( 3 ); - $user_id = self::factory()->user->create(); + $user_id = self::$superadmin_id; wp_set_current_user( $user_id ); @@ -1690,6 +1744,34 @@ public function test_update_item_keeps_the_domain() { $this->assertEquals( 0, get_site( $blog_id )->public ); } + /** + * Moving a site moves its home and siteurl along, keeping their scheme, + * but leaves URLs alone that point elsewhere on purpose. + * + * @ticket 40365 + * @covers ::update_item + * @group ms-required + */ + public function test_update_item_moves_home_and_siteurl() { + wp_set_current_user( self::$superadmin_id ); + + $blog_id = self::factory()->blog->create( array( 'path' => '/before-move/' ) ); + $domain = get_site( $blog_id )->domain; + update_blog_option( $blog_id, 'siteurl', 'https://' . $domain . '/before-move' ); + update_blog_option( $blog_id, 'home', 'https://mapped.example.org' ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/sites/' . $blog_id ); + $request->set_param( 'domain', 'moved.example.org' ); + $request->set_param( 'path', '/after-move/' ); + + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 200, $response->get_status() ); + $this->assertSame( 'https://moved.example.org/after-move', get_blog_option( $blog_id, 'siteurl' ) ); + $this->assertSame( 'https://mapped.example.org', get_blog_option( $blog_id, 'home' ) ); + $this->assertSame( get_site_url( $blog_id ), $response->get_data()['siteurl'] ); + } + /** * The `domain` parameter must be a valid hostname or IP address, * optionally followed by a port, when creating a site. @@ -1919,6 +2001,10 @@ public function data_path_validation() { '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 ), // 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 ), @@ -2036,7 +2122,7 @@ public function test_get_items_filter_user() { public function test_get_items_me_filter_user() { $blog_ids = self::factory()->blog->create_many( 5 ); - $user_id = self::factory()->user->create(); + $user_id = self::$superadmin_id; wp_set_current_user( $user_id ); foreach ( $blog_ids as $blog_id ) { add_user_to_blog( $blog_id, $user_id, 'subscriber' ); @@ -2070,7 +2156,7 @@ public function test_get_items_filter_user_no_access() { $request = new WP_REST_Request( 'GET', '/wp/v2/sites' ); $request->set_param( 'user', (string) $user_id ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_forbidden_user', $response, 403 ); + $this->assertErrorResponse( 'rest_forbidden_context', $response, 403 ); } /** @@ -2142,9 +2228,7 @@ public function test_delete_item_permissions_check_invalid_network_id() { */ public function test_get_item_permissions_check_allows_a_site_on_the_current_network() { $blog_id = self::factory()->blog->create(); - $user_id = self::factory()->user->create(); - add_user_to_blog( $blog_id, $user_id, 'subscriber' ); - wp_set_current_user( $user_id ); + wp_set_current_user( self::$superadmin_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/sites/' . $blog_id ); $request->set_param( 'id', $blog_id ); @@ -2607,8 +2691,8 @@ public function test_create_item_from_a_subsite_is_forbidden() { */ public function data_get_items_off_the_main_site() { return array( - 'own sites as me' => array( 'me', 'view', true ), - 'own sites by ID' => array( 'own', 'view', true ), + 'own sites as me' => array( 'me', 'view', 'rest_cannot_view_not_on_main_site' ), + 'own sites by ID' => array( 'own', 'view', 'rest_cannot_view_not_on_main_site' ), 'own sites in edit context' => array( 'me', 'edit', 'rest_cannot_view_not_on_main_site' ), 'own sites in embed' => array( 'me', 'embed', 'rest_cannot_view_not_on_main_site' ), 'every site' => array( '', 'view', 'rest_cannot_view_not_on_main_site' ), @@ -2617,8 +2701,8 @@ public function data_get_items_off_the_main_site() { } /** - * Only a user listing their own sites in the view context is let through away - * from the main site, even a super admin needs the main site for everything else. + * Away from the main site every listing is refused, even a super admin listing + * their own sites. * * @ticket 40365 * @covers ::get_items_permissions_check @@ -2659,13 +2743,13 @@ public function test_get_items_permissions_check_off_the_main_site( $user, $cont } /** - * A member lists their own sites through a request served by a subsite. + * A member cannot list their own sites through a request served by a subsite. * * @ticket 40365 - * @covers ::get_items + * @covers ::get_items_permissions_check * @group ms-required */ - public function test_get_items_me_filter_from_a_subsite() { + public function test_get_items_me_filter_from_a_subsite_is_forbidden() { $blog_ids = self::factory()->blog->create_many( 2 ); $user_id = self::factory()->user->create(); @@ -2682,8 +2766,7 @@ public function test_get_items_me_filter_from_a_subsite() { $response = rest_get_server()->dispatch( $request ); restore_current_blog(); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEqualSets( array_merge( array( 1 ), $blog_ids ), wp_list_pluck( $response->get_data(), 'id' ) ); + $this->assertErrorResponse( 'rest_cannot_view_not_on_main_site', $response, 403 ); } /** @@ -2720,8 +2803,8 @@ public function test_get_items_me_filter_needs_the_capability_outside_the_view_c } /** - * Away from the main site, only a member reading in the view context is let - * through, whether or not the site exists. + * Away from the main site every read is refused, members and super admins + * alike, whether or not the site exists. * * @ticket 40365 * @covers ::get_item_permissions_check @@ -2748,14 +2831,9 @@ public function test_get_item_permissions_check_off_the_main_site() { return $result; }; - $this->assertTrue( $check( $member_id, $target_id, 'view' ), 'A member may read the site in the view context.' ); - - $result = $check( $member_id, $target_id, 'edit' ); - - $this->assertWPError( $result ); - $this->assertSame( 'rest_forbidden_context', $result->get_error_code(), 'The edit context needs the capability before the site matters.' ); - $denied = array( + 'member in view context' => array( $member_id, $target_id, 'view' ), + 'member in edit context' => array( $member_id, $target_id, 'edit' ), 'member in embed context' => array( $member_id, $target_id, 'embed' ), 'non-member' => array( $outsider, $target_id, 'view' ), 'super admin' => array( self::$superadmin_id, $target_id, 'view' ), @@ -2774,17 +2852,15 @@ public function test_get_item_permissions_check_off_the_main_site() { } /** - * The fields that are not in the view context and the meta are left out for - * site members and super admins alike, and are part of the edit context. + * The fields that are not in the view context and the meta are left out of + * it, and are part of the edit context. * * @ticket 40365 * @covers ::prepare_item_for_response * @group ms-required */ public function test_edit_only_fields_are_left_out_of_the_view_context() { - $blog_id = self::factory()->blog->create( array( 'path' => '/contexts/' ) ); - $member_id = self::factory()->user->create(); - add_user_to_blog( $blog_id, $member_id, 'subscriber' ); + $blog_id = self::factory()->blog->create( array( 'path' => '/contexts/' ) ); $edit_only = array( 'registered', @@ -2800,20 +2876,18 @@ public function test_edit_only_fields_are_left_out_of_the_view_context() { 'meta', ); - foreach ( array( $member_id, self::$superadmin_id ) as $user_id ) { - wp_set_current_user( $user_id ); + wp_set_current_user( self::$superadmin_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/sites/' . $blog_id ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); + $request = new WP_REST_Request( 'GET', '/wp/v2/sites/' . $blog_id ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); - $this->assertEquals( 200, $response->get_status() ); - $this->assertSame( $blog_id, $data['id'] ); - $this->assertSame( get_admin_url( $blog_id ), $data['admin_url'] ); + $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( $blog_id, $data['id'] ); + $this->assertSame( get_admin_url( $blog_id ), $data['admin_url'] ); - foreach ( $edit_only as $field ) { - $this->assertArrayNotHasKey( $field, $data, "$field should not be in the view context." ); - } + foreach ( $edit_only as $field ) { + $this->assertArrayNotHasKey( $field, $data, "$field should not be in the view context." ); } $request = new WP_REST_Request( 'GET', '/wp/v2/sites/' . $blog_id ); @@ -2971,102 +3045,6 @@ public function test_update_item_ignores_the_creation_fields() { } } - /** - * Data provider for the collection parameters that only apply with the - * capability to manage sites. - * - * @return array[] - */ - public function data_get_items_hidden_field_params() { - return array( - 'archived' => array( 'archived', true ), - 'mature' => array( 'mature', true ), - 'spam' => array( 'spam', true ), - 'deleted' => array( 'deleted', true ), - 'lang_id' => array( 'lang_id', array( 7 ) ), - 'lang_id_exclude' => array( 'lang_id_exclude', array( 0 ) ), - 'before' => array( 'before', '2000-01-01T00:00:00' ), - 'after' => array( 'after', '2999-01-01T00:00:00' ), - 'spam false' => array( 'spam', false ), - 'orderby date' => array( 'orderby', 'registered' ), - 'orderby update' => array( 'orderby', 'last_updated' ), - ); - } - - /** - * Users listing their own sites cannot filter or order by the fields they - * cannot see, otherwise the results would give the hidden values away. - * - * @ticket 40365 - * @covers ::get_items_permissions_check - * @covers ::has_edit_only_filter - * @group ms-required - * @dataProvider data_get_items_hidden_field_params - * - * @param string $param Collection parameter. - * @param mixed $value Value that would narrow or reorder the results. - */ - public function test_get_items_me_filter_rejects_hidden_field_params( $param, $value ) { - $blog_id = self::factory()->blog->create( array( 'path' => '/hidden-params/' ) ); - $user_id = self::factory()->user->create(); - add_user_to_blog( $blog_id, $user_id, 'subscriber' ); - wp_set_current_user( $user_id ); - - $request = new WP_REST_Request( 'GET', '/wp/v2/sites' ); - $request->set_param( 'user', 'me' ); - $request->set_param( $param, $value ); - - $response = rest_get_server()->dispatch( $request ); - - $this->assertErrorResponse( 'rest_forbidden_param', $response, 403 ); - } - - /** - * A super admin listing their own sites can still use every filter. - * - * @ticket 40365 - * @covers ::get_items_permissions_check - * @group ms-required - * @dataProvider data_get_items_hidden_field_params - * - * @param string $param Collection parameter. - * @param mixed $value Value that would narrow or reorder the results. - */ - public function test_get_items_me_filter_allows_hidden_field_params_for_super_admins( $param, $value ) { - wp_set_current_user( self::$superadmin_id ); - - $request = new WP_REST_Request( 'GET', '/wp/v2/sites' ); - $request->set_param( 'user', 'me' ); - $request->set_param( $param, $value ); - - $response = rest_get_server()->dispatch( $request ); - - $this->assertEquals( 200, $response->get_status() ); - } - - /** - * The empty default of the language filters and the default order are not - * treated as filters, so a plain own-sites request is still allowed. - * - * @ticket 40365 - * @covers ::has_edit_only_filter - * @group ms-required - */ - public function test_get_items_me_filter_allows_the_default_params() { - $user_id = self::factory()->user->create(); - wp_set_current_user( $user_id ); - - $request = new WP_REST_Request( 'GET', '/wp/v2/sites' ); - $request->set_param( 'user', 'me' ); - $request->set_param( 'lang_id', array() ); - $request->set_param( 'orderby', 'domain' ); - $request->set_param( 'public', true ); - - $response = rest_get_server()->dispatch( $request ); - - $this->assertEquals( 200, $response->get_status() ); - } - /** * Data provider for user filters that match no sites. * @@ -3103,10 +3081,10 @@ public function test_get_items_user_filter_without_sites_skips_the_query( $metho add_user_to_blog( 1, $user_id, 'subscriber' ); } - wp_set_current_user( $user_id ); + wp_set_current_user( self::$superadmin_id ); $request = new WP_REST_Request( $method, '/wp/v2/sites' ); - $request->set_param( 'user', 'me' ); + $request->set_param( 'user', $user_id ); if ( $with_include ) { $request->set_param( 'include', array( $blog_id ) ); @@ -3155,7 +3133,7 @@ public function test_get_items_user_filter_without_sites_can_be_widened() { $blog_id = self::factory()->blog->create( array( 'path' => '/widened/' ) ); $user_id = self::factory()->user->create(); remove_user_from_blog( $user_id, 1 ); - wp_set_current_user( $user_id ); + wp_set_current_user( self::$superadmin_id ); $widen = static function ( $args ) use ( $blog_id ) { $args['site__in'] = array( $blog_id ); @@ -3165,7 +3143,7 @@ public function test_get_items_user_filter_without_sites_can_be_widened() { add_filter( 'rest_site_query', $widen ); $request = new WP_REST_Request( 'GET', '/wp/v2/sites' ); - $request->set_param( 'user', 'me' ); + $request->set_param( 'user', $user_id ); $response = rest_get_server()->dispatch( $request ); @@ -3387,34 +3365,23 @@ public function test_create_item_sets_the_user_as_administrator( $kind, $json ) } /** - * Without the capability to manage sites, a user may only filter by their - * own ID or "me", whichever site serves the request. + * Without the capability to manage sites, a user cannot list any sites, + * not even their own. * * @ticket 40365 * @covers ::get_items_permissions_check * @group ms-required */ - public function test_get_items_another_user_needs_the_capability() { - $subsite_id = self::factory()->blog->create( array( 'path' => '/other-user/' ) ); - $user_id = self::factory()->user->create(); - $other_id = self::factory()->user->create(); + public function test_get_items_user_filter_needs_the_capability() { + $user_id = self::factory()->user->create(); + $other_id = self::factory()->user->create(); wp_set_current_user( $user_id ); - $request = new WP_REST_Request( 'GET', '/wp/v2/sites' ); - $request->set_param( 'user', $other_id ); - - $this->assertErrorResponse( 'rest_forbidden_user', rest_get_server()->dispatch( $request ), 403 ); - - switch_to_blog( $subsite_id ); - $response = rest_get_server()->dispatch( $request ); - restore_current_blog(); - - $this->assertErrorResponse( 'rest_forbidden_user', $response, 403 ); - - foreach ( array( 'me', $user_id, (string) $user_id ) as $own ) { - $request->set_param( 'user', $own ); + foreach ( array( $other_id, 'me', $user_id, (string) $user_id ) as $user ) { + $request = new WP_REST_Request( 'GET', '/wp/v2/sites' ); + $request->set_param( 'user', $user ); - $this->assertEquals( 200, rest_get_server()->dispatch( $request )->get_status(), 'A user can list their own sites.' ); + $this->assertErrorResponse( 'rest_forbidden_context', rest_get_server()->dispatch( $request ), 403 ); } } @@ -3429,7 +3396,7 @@ public function test_get_items_user_filter_forbidden_when_logged_out() { $request = new WP_REST_Request( 'GET', '/wp/v2/sites' ); $request->set_param( 'user', self::$superadmin_id ); - $this->assertErrorResponse( 'rest_forbidden_user', rest_get_server()->dispatch( $request ), 401 ); + $this->assertErrorResponse( 'rest_forbidden_context', rest_get_server()->dispatch( $request ), 401 ); } /** @@ -3467,8 +3434,8 @@ public function data_edit_context_permission_checks() { } /** - * The edit context needs the capability to manage sites, even for a member - * reading their own site or listing their own sites. + * The edit context needs the capability to manage sites, and a member cannot + * get around it from their own site. * * @ticket 40365 * @covers ::get_items_permissions_check @@ -3510,7 +3477,8 @@ public function test_edit_context_needs_the_capability( $permission_check, $sing $result = $this->endpoint->$permission_check( $request ); restore_current_blog(); - $assert_forbidden( $result, 403, 'A member on their own site.' ); + $this->assertWPError( $result, 'A member on their own site.' ); + $this->assertSame( 'rest_cannot_view_not_on_main_site', $result->get_error_code(), 'A member on their own site.' ); wp_set_current_user( self::$superadmin_id ); From e2feea2f29fe615291e94c9d9174388f4f182840 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Thu, 5 Nov 2026 13:14:02 +0100 Subject: [PATCH 3/4] REST API: Trim redundant site path test cases --- .../class-wp-rest-sites-controller.php | 113 ++++++------ .../tests/core/wpRestSitesController.php | 163 ++++++++++++++---- 2 files changed, 191 insertions(+), 85 deletions(-) diff --git a/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php b/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php index 75c6414..8e81f4d 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-]+)/$#', 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. diff --git a/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php b/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php index a222937..c8402a4 100644 --- a/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php +++ b/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php @@ -520,6 +520,29 @@ public function test_create_item_rejects_an_existing_domain_and_path() { $this->assertErrorResponse( 'rest_site_taken', $response, 400 ); } + /** + * 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 +976,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 +1005,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 +1748,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 +1998,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 +2035,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 +2050,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 +2067,33 @@ 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. + // 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 ), From 5786b6cfc1decc7d4983bc0731d50f26a0733bf8 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Fri, 6 Nov 2026 16:50:48 +0100 Subject: [PATCH 4/4] Tests added --- .../class-wp-rest-sites-controller.php | 4 ++-- .../tests/core/wpRestSitesController.php | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php b/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php index 8e81f4d..a3c55f6 100644 --- a/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php +++ b/modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php @@ -1267,7 +1267,7 @@ private function is_valid_path( $path ) { $base = preg_quote( strtolower( get_network()->path ), '#' ); - if ( 1 !== preg_match( '#^' . $base . '([a-z0-9-]+)/$#', strtolower( $path ), $matches ) ) { + if ( 1 !== preg_match( '#^' . $base . '([a-z0-9-]+)/$#D', strtolower( $path ), $matches ) ) { return false; } @@ -1428,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 c8402a4..52707a9 100644 --- a/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php +++ b/modules/RestSitesEndpoint/tests/core/wpRestSitesController.php @@ -520,6 +520,25 @@ 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. * @@ -2081,6 +2100,8 @@ 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 ), + // 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 ),