From dad03c0cf1d2215ea76cd83909df4a227b7dd4c5 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Mon, 2 Nov 2026 13:23:15 +0100 Subject: [PATCH 1/2] 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/2] 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 );