Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/RestSitesEndpoint/About.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);
}
Expand Down
198 changes: 95 additions & 103 deletions modules/RestSitesEndpoint/patches/class-wp-rest-sites-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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'] );
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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 ) ) {
Expand All @@ -763,25 +708,26 @@ 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 );

/** 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 ) ) {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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' );
}

Expand Down
60 changes: 54 additions & 6 deletions modules/RestSitesEndpoint/tests/core/wpRestSiteMetaFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 );
Expand Down
Loading
Loading