diff --git a/.github/changelog/fix-move-fep-7628 b/.github/changelog/fix-move-fep-7628 new file mode 100644 index 0000000000..a7aa2d8626 --- /dev/null +++ b/.github/changelog/fix-move-fep-7628 @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +When someone you follow moves their account, you now follow the new account, and your blocks carry over. diff --git a/includes/class-move.php b/includes/class-move.php index b921699d2d..aec4a05729 100644 --- a/includes/class-move.php +++ b/includes/class-move.php @@ -10,11 +10,16 @@ use Activitypub\Activity\Activity; use Activitypub\Activity\Actor; use Activitypub\Collection\Actors; +use Activitypub\Collection\Blocked_Actors; +use Activitypub\Collection\Followers; +use Activitypub\Collection\Following; +use Activitypub\Collection\Remote_Actors; use Activitypub\Model\Blog; use Activitypub\Model\User; use Activitypub\Scheduler\Actor as Actor_Scheduler; use function Activitypub\is_actor; +use function Activitypub\unescape_guid; /** * ActivityPub (Account) Move Class @@ -215,6 +220,86 @@ public static function internally( $from, $to ) { return $outbox_id; } + /** + * Move a remote actor to its new id. + * + * Our users who follow the old actor follow the new one (FEP-7628), and their blocks carry over. + * + * @since unreleased + * + * @param \WP_Post $origin The stored old actor. + * @param array $actor The new actor, fetched and verified. + * + * @return int|\WP_Error The post ID of the new actor, or WP_Error if it can not be stored. + */ + public static function remote( $origin, $actor ) { + // Read before the merge, which deletes the old actor. + $refollow = self::get_following_user_ids( $origin->ID ); + $blocked_by = \get_post_meta( $origin->ID, Moderation::BLOCKED_ACTORS_META_KEY, false ); + $origin_uri = unescape_guid( $origin->guid ); + $old_actor = $blocked_by ? Remote_Actors::get_actor( $origin ) : null; + + // Stored first, so a new actor that can not be stored (no inbox) leaves the old one as it was. + $post_id = Remote_Actors::upsert( $actor ); + + if ( \is_wp_error( $post_id ) ) { + return $post_id; + } + + // A merge into the old actor itself would delete it. + if ( ! $post_id || $post_id === $origin->ID ) { + return new \WP_Error( 'activitypub_actor_not_moved', \__( 'The actor could not be moved.', 'activitypub' ) ); + } + + // Users who already follow the new actor keep that follow. + $refollow = \array_diff( $refollow, self::get_following_user_ids( $post_id ) ); + + Remote_Actors::merge( $origin->ID, $post_id ); + + // The old account stays reachable, so it keeps its blocks. Stored again from our copy, without a fetch. + if ( $old_actor && ! \is_wp_error( $old_actor ) ) { + Remote_Actors::upsert( $old_actor ); + } + + // Through Blocked_Actors::add(), so the block hooks run and clean up the relationships. + foreach ( $blocked_by as $user_id ) { + Blocked_Actors::add( (int) $user_id, $actor['id'] ); + Blocked_Actors::add( (int) $user_id, $origin_uri ); + } + + // A user who blocks the new actor neither follows it nor keeps it as a follower. + foreach ( \get_post_meta( $post_id, Followers::FOLLOWER_META_KEY, false ) as $user_id ) { + if ( Moderation::is_actor_blocked( $actor['id'], (int) $user_id ) ) { + Followers::remove( $post_id, (int) $user_id ); + } + } + + // A user who blocks the new actor does not follow it; follow() refuses users who can no longer follow. + foreach ( $refollow as $user_id ) { + if ( ! Moderation::is_actor_blocked( $actor['id'], (int) $user_id ) ) { + Following::follow( $post_id, (int) $user_id ); + } + } + + return $post_id; + } + + /** + * Get the local users that follow or asked to follow a remote actor. + * + * @param int $post_id The remote actor post ID. + * + * @return string[] The user IDs. + */ + private static function get_following_user_ids( $post_id ) { + return \array_unique( + \array_merge( + \get_post_meta( $post_id, Following::FOLLOWING_META_KEY, false ), + \get_post_meta( $post_id, Following::PENDING_META_KEY, false ) + ) + ); + } + /** * Update the alsoKnownAs property of a user. * diff --git a/includes/collection/class-remote-actors.php b/includes/collection/class-remote-actors.php index 63f27c9f05..53d34989a9 100644 --- a/includes/collection/class-remote-actors.php +++ b/includes/collection/class-remote-actors.php @@ -212,6 +212,55 @@ public static function delete( $post_id ) { return $result; } + /** + * Merge a stored remote actor into another one. + * + * The followers, comments and Reader posts of the old actor go to the new one, and the old post + * is deleted. The follows and blocks of our users do not carry over; use `Move::remote()` for that. + * + * @since unreleased + * + * @param int $from The post ID of the old actor. + * @param int $to The post ID of the new actor. + * + * @return void + */ + public static function merge( $from, $to ) { + global $wpdb; + + foreach ( \array_diff( self::get_follower_ids( $from ), self::get_follower_ids( $to ) ) as $user_id ) { + \add_post_meta( $to, Followers::FOLLOWER_META_KEY, $user_id ); + } + + // Comments and Reader posts of the old actor belong to the new one now. + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value + $where = array( + 'meta_key' => '_activitypub_remote_actor_id', + 'meta_value' => (string) $from, + ); + + // The meta type and the cache group of its queries, which core names differently. + $types = array( + 'comment' => 'comment', + 'post' => 'posts', + ); + + foreach ( $types as $type => $cache_group ) { + $table = $wpdb->{ \sprintf( '%smeta', $type ) }; + $ids = $wpdb->get_col( $wpdb->prepare( 'SELECT %i FROM %i WHERE meta_key = %s AND meta_value = %s', \sprintf( '%s_id', $type ), $table, $where['meta_key'], $where['meta_value'] ) ); + + if ( $ids ) { + $wpdb->update( $table, array( 'meta_value' => (string) $to ), $where ); + \wp_cache_delete_multiple( $ids, \sprintf( '%s_meta', $type ) ); + \wp_cache_set_last_changed( $cache_group ); + } + } + // phpcs:enable + + // Clears the inbox caches of the followers, which now include the new actor. + self::delete( $from ); + } + /** * Get the IDs of the local users a remote actor follows. * diff --git a/includes/handler/class-move.php b/includes/handler/class-move.php index f4676b76c0..e818986bba 100644 --- a/includes/handler/class-move.php +++ b/includes/handler/class-move.php @@ -7,12 +7,14 @@ namespace Activitypub\Handler; -use Activitypub\Collection\Followers; +use Activitypub\Collection\Inbox; use Activitypub\Collection\Remote_Actors; +use Activitypub\Moderation; +use Activitypub\Move as Account_Move; use Activitypub\Proxy; -use function Activitypub\get_id_hash; -use function Activitypub\get_stored_id_forms; +use function Activitypub\is_actor; +use function Activitypub\is_same_domain; use function Activitypub\object_to_uri; /** @@ -29,18 +31,27 @@ class Move { * @return void */ public static function init() { - \add_action( 'activitypub_inbox_move', array( self::class, 'handle_move' ), 10, 2 ); + \add_action( 'activitypub_inbox_move', array( self::class, 'handle_move' ), 10, 4 ); + \add_action( 'activitypub_inbox_shared_move', array( self::class, 'handle_move' ), 10, 4 ); } /** * Handle Move requests. * - * @param array $activity The JSON "Move" Activity. - * @param int|int[] $user_ids The user ID(s). + * @param array $activity The JSON "Move" Activity. + * @param int|int[] $user_ids The user ID(s). + * @param \Activitypub\Activity\Activity|null $activity_object Optional. The activity object. Default null. + * @param string|null $context Optional. The inbox context. Default null. * * @return void */ - public static function handle_move( $activity, $user_ids ) { + public static function handle_move( $activity, $user_ids, $activity_object = null, $context = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + // The shared inbox invokes this once per resolved recipient and once on the shared hook; + // handle that path only on the shared hook, so one Move is handled once. + if ( Inbox::CONTEXT_SHARED_INBOX === $context && 'activitypub_inbox_shared_move' !== \current_filter() ) { + return; + } + $target_uri = self::extract_target( $activity ); $origin_uri = self::extract_origin( $activity ); @@ -48,83 +59,47 @@ public static function handle_move( $activity, $user_ids ) { return; } - $target_json = Proxy::get( $target_uri ); - $origin_json = Proxy::get( $origin_uri ); - - $verified = self::verify_move( $target_json, $origin_json ); - - if ( ! $verified ) { + // Only the old or the new actor may send the Move; checked before anything is fetched. + if ( ! \in_array( object_to_uri( $activity['actor'] ?? null ), array( $origin_uri, $target_uri ), true ) ) { return; } - $target_object = Remote_Actors::get_by_uri( $target_uri ); + // Only a stored old actor has relationships to move, so nothing else needs a fetch. $origin_object = Remote_Actors::get_by_uri( $origin_uri ); - $result = null; - $success = false; - - global $wpdb; - - // If the origin is followed but the target is not, update the origin to point to the target. - if ( \is_wp_error( $target_object ) && ! \is_wp_error( $origin_object ) ) { - // Written in the form an insert stores, which every guid lookup matches. - list( $guid ) = get_stored_id_forms( $target_uri ); - $data = array( 'guid' => $guid ); - $hash = get_id_hash( $target_uri ); - if ( $hash ) { - $data['post_name'] = $hash; - } - - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery - $wpdb->update( $wpdb->posts, $data, array( 'ID' => (int) $origin_object->ID ) ); - - // Clear the cache. - \wp_cache_delete( $origin_object->ID, 'posts' ); - - $success = true; - - // Proxy::get() already self-confirmed the target, so it is safe to cache. - $result = Remote_Actors::upsert( $target_json ); + if ( \is_wp_error( $origin_object ) ) { + return; } - // If both the target and origin are followed, merge them. - if ( ! \is_wp_error( $target_object ) && ! \is_wp_error( $origin_object ) ) { - $origin_users = \get_post_meta( $origin_object->ID, Followers::FOLLOWER_META_KEY, false ); - $target_users = \get_post_meta( $target_object->ID, Followers::FOLLOWER_META_KEY, false ); + // No request to our own site or to a blocked one; the confirmed id is checked again below. + if ( is_same_domain( $target_uri ) || Moderation::is_actor_blocked( $target_uri ) ) { + return; + } - // Get all user ids from $origin_users that are not in $target_users. - $users = \array_diff( $origin_users, $target_users ); + // Both are fetched fresh: a cached copy may predate the movedTo or the alsoKnownAs. + $target_json = Proxy::get( $target_uri, array( 'cached' => false ) ); + $origin_json = Proxy::get( $origin_uri, array( 'cached' => false ) ); - foreach ( $users as $follower_user_id ) { - \add_post_meta( $target_object->ID, Followers::FOLLOWER_META_KEY, $follower_user_id ); - } + $verified = self::verify_move( $activity, $target_json, $origin_json ); - // Comments and Reader posts of the origin belong to the target now. - // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value - $where = array( - 'meta_key' => '_activitypub_remote_actor_id', - 'meta_value' => (string) $origin_object->ID, - ); + if ( ! $verified ) { + return; + } - // The meta type and the cache group of its queries, which core names differently. - $types = array( - 'comment' => 'comment', - 'post' => 'posts', - ); + // The old actor must be the one we store, not one its URL redirects to. + $stored_origin = Remote_Actors::get_by_uri( $origin_json['id'] ); + if ( \is_wp_error( $stored_origin ) || $stored_origin->ID !== $origin_object->ID ) { + return; + } - foreach ( $types as $type => $cache_group ) { - $table = $wpdb->{ \sprintf( '%smeta', $type ) }; - $ids = $wpdb->get_col( $wpdb->prepare( 'SELECT %i FROM %i WHERE meta_key = %s AND meta_value = %s', \sprintf( '%s_id', $type ), $table, $where['meta_key'], $where['meta_value'] ) ); + // A remote actor can not move to one of ours, nor to an actor the site blocks. + if ( is_same_domain( $target_json['id'] ) || Moderation::is_actor_blocked( $target_json['id'] ) ) { + return; + } - if ( $ids ) { - $wpdb->update( $table, array( 'meta_value' => (string) $target_object->ID ), $where ); - \wp_cache_delete_multiple( $ids, \sprintf( '%s_meta', $type ) ); - \wp_cache_set_last_changed( $cache_group ); - } - } - // phpcs:enable + $result = Account_Move::remote( $origin_object, $target_json ); - $success = true; - $result = Remote_Actors::delete( $origin_object->ID ); + if ( \is_wp_error( $result ) ) { + return; } /** @@ -132,18 +107,17 @@ public static function handle_move( $activity, $user_ids ) { * * @param array $activity The ActivityPub activity data. * @param int[] $user_ids The local user IDs. - * @param bool $success True on success, false otherwise. - * @param mixed $result The result of the operation (e.g., post ID, WP_Error, or status). + * @param bool $success Always true; a Move that is not handled returns before this action. + * @param int $result The post ID of the new actor. */ - \do_action( 'activitypub_handled_move', $activity, (array) $user_ids, $success, $result ); + \do_action( 'activitypub_handled_move', $activity, (array) $user_ids, true, $result ); } /** * Extract the target from the activity. * - * The ActivityStreams spec define the `target` attribute as the - * destination of the activity, but Mastodon uses the `object` - * attribute to move profiles. + * FEP-7628 and Mastodon send the new actor as `target`. Without a `target`, + * the `object` is read as the new actor, the way older senders sent it. * * @param array $activity The JSON "Move" Activity. * @@ -164,15 +138,19 @@ private static function extract_target( $activity ) { /** * Extract the origin from the activity. * - * The ActivityStreams spec define the `origin` attribute as source - * of the activity, but Mastodon uses the `actor` attribute as source - * to move profiles. + * FEP-7628 and Mastodon send the old actor as `object` and the new one as `target`; the + * `actor` is whoever sends the Move, the old actor (push) or the new one (pull). Without a + * `target`, the `object` is the target, so the origin is the `origin` or the `actor`. * * @param array $activity The JSON "Move" Activity. * * @return string|null The origin URI or null if not found. */ private static function extract_origin( $activity ) { + if ( ! empty( $activity['target'] ) && ! empty( $activity['object'] ) ) { + return object_to_uri( $activity['object'] ); + } + if ( ! empty( $activity['origin'] ) ) { return object_to_uri( $activity['origin'] ); } @@ -187,19 +165,23 @@ private static function extract_origin( $activity ) { /** * Verify the move. * - * @param array $target_object The target object. - * @param array $origin_object The origin object. + * Both actors have to agree, each in its own document: the target lists the origin in + * `alsoKnownAs`, and the origin points at the target with `movedTo`. A `copiedTo` origin + * stays active (FEP-7628), so it is not merged away. + * + * @param array $activity The JSON "Move" Activity. + * @param array|\WP_Error $target_object The target object. + * @param array|\WP_Error $origin_object The origin object. * * @return bool True if the move is verified, false otherwise. */ - private static function verify_move( $target_object, $origin_object ) { - // Check if both objects are valid. - if ( \is_wp_error( $target_object ) || \is_wp_error( $origin_object ) ) { + private static function verify_move( $activity, $target_object, $origin_object ) { + // Check if both objects are actors. + if ( ! \is_array( $target_object ) || ! \is_array( $origin_object ) || ! is_actor( $target_object ) || ! is_actor( $origin_object ) ) { return false; } - // Check if both objects are persons. - if ( 'Person' !== $target_object['type'] || 'Person' !== $origin_object['type'] ) { + if ( ! \is_string( $target_object['id'] ?? null ) || ! \is_string( $origin_object['id'] ?? null ) ) { return false; } @@ -208,6 +190,11 @@ private static function verify_move( $target_object, $origin_object ) { return false; } + // Only one of the two actors may send the Move. + if ( ! \in_array( object_to_uri( $activity['actor'] ?? null ), array( $origin_object['id'], $target_object['id'] ), true ) ) { + return false; + } + // Normalize alsoKnownAs to a list of ids (some JSON-LD payloads use a string or objects). $also_known_as = \array_map( '\Activitypub\object_to_uri', (array) ( $target_object['alsoKnownAs'] ?? array() ) ); if ( empty( $also_known_as ) ) { @@ -223,16 +210,7 @@ private static function verify_move( $target_object, $origin_object ) { return false; } - // Check if the origin has a movedTo property. - if ( empty( $origin_object['movedTo'] ) ) { - return false; - } - - // Check if the movedTo property of the origin is the target. - if ( $origin_object['movedTo'] !== $target_object['id'] ) { - return false; - } - - return true; + // Check if the origin points at the target. + return object_to_uri( $origin_object['movedTo'] ?? null ) === $target_object['id']; } } diff --git a/tests/phpunit/tests/includes/class-test-move.php b/tests/phpunit/tests/includes/class-test-move.php index b606659e3c..6c9c107664 100644 --- a/tests/phpunit/tests/includes/class-test-move.php +++ b/tests/phpunit/tests/includes/class-test-move.php @@ -8,7 +8,11 @@ namespace Activitypub\Tests; use Activitypub\Collection\Actors; +use Activitypub\Collection\Followers; +use Activitypub\Collection\Following; use Activitypub\Collection\Outbox; +use Activitypub\Collection\Remote_Actors; +use Activitypub\Moderation; use Activitypub\Move; /** @@ -632,6 +636,181 @@ public function test_change_domain_with_valid_input() { \add_filter( 'option_home', '_config_wp_home' ); } + /** + * Our users who follow the old actor follow the new one, which is stored as its own post. + * + * @covers ::remote + */ + public function test_remote_follows_new_actor() { + $origin = 'https://old.example/users/alice'; + $target = 'https://new.example/users/alice'; + $user_id = self::factory()->user->create(); + + \get_user_by( 'id', $user_id )->add_cap( 'activitypub' ); + $id = Remote_Actors::upsert( $this->remote_actor( $origin ) ); + \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $user_id ); + + $post_id = Move::remote( \get_post( $id ), $this->remote_actor( $target ) ); + + $this->assertSame( Remote_Actors::get_by_uri( $target )->ID, $post_id ); + $this->assertNull( \get_post( $id ), 'The old actor must be merged away.' ); + $this->assertNotContains( (string) $user_id, \get_post_meta( $post_id, Following::FOLLOWING_META_KEY, false ), 'The new server has not accepted yet.' ); + $this->assertContains( (string) $user_id, \get_post_meta( $post_id, Following::PENDING_META_KEY, false ) ); + $this->assertCount( 1, $this->get_follows( $user_id, $target ), 'A Follow must be sent to the new actor.' ); + } + + /** + * A merge keeps blocks of the old actor and follows the new one for our users. + * + * @covers ::remote + */ + public function test_remote_merge_keeps_blocks_and_follows() { + $origin = 'https://old.example/users/bob'; + $target = 'https://new.example/users/bob'; + $user_id = self::factory()->user->create(); + $blocker = self::factory()->user->create(); + $origin_id = Remote_Actors::upsert( $this->remote_actor( $origin ) ); + $target_id = Remote_Actors::upsert( $this->remote_actor( $target ) ); + + \get_user_by( 'id', $user_id )->add_cap( 'activitypub' ); + \add_post_meta( $origin_id, Following::FOLLOWING_META_KEY, (string) $user_id ); + \add_post_meta( $origin_id, Moderation::BLOCKED_ACTORS_META_KEY, (string) $blocker ); + + $this->assertSame( $target_id, Move::remote( \get_post( $origin_id ), $this->remote_actor( $target ) ) ); + + $this->assertNull( \get_post( $origin_id ), 'The old actor must be merged away.' ); + $this->assertContains( (string) $blocker, \get_post_meta( $target_id, Moderation::BLOCKED_ACTORS_META_KEY, false ), 'The block must carry over.' ); + $this->assertTrue( Moderation::is_actor_blocked( $target, $blocker ) ); + $this->assertTrue( Moderation::is_actor_blocked( $origin, $blocker ), 'The old actor must stay blocked.' ); + $this->assertContains( (string) $user_id, \get_post_meta( $target_id, Following::PENDING_META_KEY, false ) ); + $this->assertCount( 1, $this->get_follows( $user_id, $target ) ); + } + + /** + * Our users do not follow a new actor they block. + * + * @covers ::remote + */ + public function test_remote_does_not_follow_blocked_actor() { + $origin = 'https://old.example/users/carol'; + $target = 'https://new.example/users/carol'; + $user_id = self::factory()->user->create(); + + \get_user_by( 'id', $user_id )->add_cap( 'activitypub' ); + Moderation::add_user_block( $user_id, Moderation::TYPE_DOMAIN, 'new.example' ); + $id = Remote_Actors::upsert( $this->remote_actor( $origin ) ); + \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $user_id ); + + $post_id = Move::remote( \get_post( $id ), $this->remote_actor( $target ) ); + + $this->assertEmpty( \get_post_meta( $post_id, Following::FOLLOWING_META_KEY, false ) ); + $this->assertEmpty( \get_post_meta( $post_id, Following::PENDING_META_KEY, false ) ); + $this->assertCount( 0, $this->get_follows( $user_id, $target ) ); + } + + /** + * A user who blocks the new actor does not keep it as a follower. + * + * @covers ::remote + */ + public function test_remote_drops_follower_blocked_by_user() { + $origin = 'https://old.example/users/jack'; + $target = 'https://new.example/users/jack'; + $blocker = self::factory()->user->create(); + $user_id = self::factory()->user->create(); + + Moderation::add_user_block( $blocker, Moderation::TYPE_DOMAIN, 'new.example' ); + $id = Remote_Actors::upsert( $this->remote_actor( $origin ) ); + \add_post_meta( $id, Followers::FOLLOWER_META_KEY, (string) $blocker ); + \add_post_meta( $id, Followers::FOLLOWER_META_KEY, (string) $user_id ); + + $post_id = Move::remote( \get_post( $id ), $this->remote_actor( $target ) ); + + $followers = \get_post_meta( $post_id, Followers::FOLLOWER_META_KEY, false ); + $this->assertNotContains( (string) $blocker, $followers ); + $this->assertContains( (string) $user_id, $followers ); + } + + /** + * A user who can no longer follow does not end up following the new actor without a Follow. + * + * @covers ::remote + */ + public function test_remote_does_not_keep_following_without_follow() { + $origin = 'https://old.example/users/mia'; + $target = 'https://new.example/users/mia'; + $user_id = self::factory()->user->create(); + + $id = Remote_Actors::upsert( $this->remote_actor( $origin ) ); + \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $user_id ); + + $post_id = Move::remote( \get_post( $id ), $this->remote_actor( $target ) ); + + $this->assertNotContains( (string) $user_id, \get_post_meta( $post_id, Following::FOLLOWING_META_KEY, false ), 'The new server never got a Follow.' ); + $this->assertCount( 0, $this->get_follows( $user_id, $target ) ); + } + + /** + * A move to the old actor itself keeps it, instead of merging it into itself and deleting it. + * + * @covers ::remote + */ + public function test_remote_to_itself_keeps_actor() { + $origin = 'https://old.example/users/cid'; + $id = Remote_Actors::upsert( $this->remote_actor( $origin ) ); + \add_post_meta( $id, Followers::FOLLOWER_META_KEY, '1' ); + + $this->assertWPError( Move::remote( \get_post( $id ), $this->remote_actor( $origin ) ) ); + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID ); + $this->assertSame( array( '1' ), \get_post_meta( $id, Followers::FOLLOWER_META_KEY, false ) ); + } + + /** + * Build a remote actor document. + * + * @param string $id The actor id. + * + * @return array The actor document. + */ + private function remote_actor( $id ) { + return array( + 'type' => 'Person', + 'id' => $id, + 'url' => $id, + 'name' => 'Actor', + 'inbox' => $id . '/inbox', + ); + } + + /** + * Get the Follow activities a user sent to an actor. + * + * @param int $user_id The user ID. + * @param string $actor_id The followed actor id. + * + * @return int[] The outbox post IDs. + */ + private function get_follows( $user_id, $actor_id ) { + return \get_posts( + array( + 'post_type' => Outbox::POST_TYPE, + 'post_status' => 'any', + 'author' => $user_id, + 'fields' => 'ids', + 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + array( + 'key' => '_activitypub_object_id', + 'value' => $actor_id, + ), + array( + 'key' => '_activitypub_activity_type', + 'value' => 'Follow', + ), + ), + ) + ); + } + /** * The profile Updates federated for an actor. * diff --git a/tests/phpunit/tests/includes/collection/class-test-remote-actors.php b/tests/phpunit/tests/includes/collection/class-test-remote-actors.php index 03aba80c23..8d232cb910 100644 --- a/tests/phpunit/tests/includes/collection/class-test-remote-actors.php +++ b/tests/phpunit/tests/includes/collection/class-test-remote-actors.php @@ -2804,4 +2804,36 @@ public function test_fetch_by_acct_ignores_case( $stored, $requested ) { $this->assertSame( 0, $requests ); $this->assertSame( $stored, \get_post_meta( $post_id, '_activitypub_acct', true ), 'The stored handle must keep its case.' ); } + + /** + * Followers, comments and Reader posts of the old actor go to the new one, and the old one is deleted. + * + * @covers ::merge + */ + public function test_merge() { + $origin_id = Remote_Actors::upsert( + array( + 'id' => 'https://old.example.com/users/ben', + 'type' => 'Person', + 'inbox' => 'https://old.example.com/users/ben/inbox', + ) + ); + $target_id = Remote_Actors::upsert( + array( + 'id' => 'https://new.example.com/users/ben', + 'type' => 'Person', + 'inbox' => 'https://new.example.com/users/ben/inbox', + ) + ); + \add_post_meta( $origin_id, Followers::FOLLOWER_META_KEY, '1' ); + $comment_id = self::factory()->comment->create( array( 'comment_meta' => array( '_activitypub_remote_actor_id' => (string) $origin_id ) ) ); + $reader_id = self::factory()->post->create( array( 'meta_input' => array( '_activitypub_remote_actor_id' => (string) $origin_id ) ) ); + + Remote_Actors::merge( $origin_id, $target_id ); + + $this->assertNull( \get_post( $origin_id ), 'The old actor must be deleted.' ); + $this->assertSame( array( '1' ), \get_post_meta( $target_id, Followers::FOLLOWER_META_KEY, false ) ); + $this->assertSame( (string) $target_id, \get_comment_meta( $comment_id, '_activitypub_remote_actor_id', true ) ); + $this->assertSame( (string) $target_id, \get_post_meta( $reader_id, '_activitypub_remote_actor_id', true ) ); + } } diff --git a/tests/phpunit/tests/includes/handler/class-test-move.php b/tests/phpunit/tests/includes/handler/class-test-move.php index 617afa140f..738cb5b928 100644 --- a/tests/phpunit/tests/includes/handler/class-test-move.php +++ b/tests/phpunit/tests/includes/handler/class-test-move.php @@ -9,9 +9,13 @@ use Activitypub\Activity\Actor; use Activitypub\Collection\Followers; +use Activitypub\Collection\Following; +use Activitypub\Collection\Inbox; +use Activitypub\Collection\Outbox; use Activitypub\Collection\Remote_Actors; use Activitypub\Collection\Remote_Posts; use Activitypub\Handler\Move; +use Activitypub\Moderation; use function Activitypub\get_id_hash; use function Activitypub\object_to_uri; @@ -642,7 +646,175 @@ public function test_handle_move_ignores_origin_webfinger() { } /** - * A move to a target id with `&` ends up on the moved actor, not on a second one. + * A Move sent by the new actor (pull mode) is accepted when the old actor points at it. + * + * @covers ::handle_move + */ + public function test_handle_move_pull_mode() { + $origin = 'https://old.example/users/dave'; + $target = 'https://new.example/users/dave'; + + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $target, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertNotWPError( Remote_Actors::get_by_uri( $target ) ); + $this->assertNull( \get_post( $id ), 'The old actor must be merged away.' ); + } + + /** + * A `copiedTo` origin stays active, so it is not merged away. + * + * @covers ::handle_move + */ + public function test_handle_move_ignores_copied_to() { + $origin = 'https://old.example/users/gina'; + $target = 'https://new.example/users/gina'; + + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'copiedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID ); + $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); + } + + /** + * A remote actor can not move to an actor on this site. + * + * @covers ::handle_move + */ + public function test_handle_move_rejects_local_target() { + $origin = 'https://old.example/users/hank'; + $target = \home_url( '/?author=' . $this->user_id ); + + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID ); + } + + /** + * A Move to an actor the site blocks is not applied. + * + * @covers ::handle_move + */ + public function test_handle_move_rejects_site_blocked_target() { + $origin = 'https://old.example/users/ivy'; + $target = 'https://blocked.example/users/ivy'; + + Moderation::add_site_block( Moderation::TYPE_DOMAIN, 'blocked.example' ); + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + Moderation::remove_site_block( Moderation::TYPE_DOMAIN, 'blocked.example' ); + + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID ); + $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); + } + + /** + * The shared inbox hands one Move to every recipient; it is handled once, on the shared hook. + * + * @covers ::handle_move + */ + public function test_handle_move_runs_once_from_shared_inbox() { + $origin = 'https://old.example/users/kim'; + $activity = array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => 'https://new.example/users/kim', + ); + + Remote_Actors::upsert( $this->actor( $origin ) ); + + $fetches = 0; + $filter = function ( $pre, $url_or_object ) use ( &$fetches, $origin ) { + ++$fetches; + return object_to_uri( $url_or_object ) === $origin ? $this->actor( $origin ) : $pre; + }; + \add_filter( 'activitypub_pre_http_get_remote_object', $filter, 10, 2 ); + + \do_action( 'activitypub_inbox_move', $activity, $this->user_id, null, Inbox::CONTEXT_SHARED_INBOX ); + \do_action( 'activitypub_inbox_move', $activity, $this->user_id_2, null, Inbox::CONTEXT_SHARED_INBOX ); + $per_recipient = $fetches; + + \do_action( 'activitypub_inbox_shared_move', $activity, array( $this->user_id, $this->user_id_2 ), null, Inbox::CONTEXT_SHARED_INBOX ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( 0, $per_recipient, 'The per-recipient calls must not fetch.' ); + $this->assertGreaterThan( 0, $fetches, 'The shared hook handles the Move.' ); + } + + /** + * A target id with a query string ends up on the moved post, not on a second one. * * @covers ::handle_move */ @@ -650,31 +822,198 @@ public function test_handle_move_target_with_query_string() { $origin = 'https://old.example/users/lena'; $target = 'https://new.example/?author=7&type=actor'; - $origin_object = array( - 'type' => 'Person', - 'id' => $origin, - 'url' => $origin, - 'inbox' => $origin . '/inbox', - 'movedTo' => $target, + \get_user_by( 'id', $this->user_id )->add_cap( 'activitypub' ); + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $this->user_id ); + + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) ); - $target_object = array( - 'type' => 'Person', - 'id' => $target, - 'url' => $target, - 'inbox' => 'https://new.example/inbox', - 'alsoKnownAs' => array( $origin ), + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id ); - $id = Remote_Actors::upsert( $origin_object ); + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); - $filter = static function ( $pre, $url_or_object ) use ( $origin, $origin_object, $target, $target_object ) { - $url = object_to_uri( $url_or_object ); - if ( $url === $origin ) { - return $origin_object; - } - if ( $url === $target ) { - return $target_object; - } + $moved = Remote_Actors::get_by_uri( $target ); + $this->assertNull( \get_post( $id ), 'The old actor must be merged away.' ); + $this->assertContains( (string) $this->user_id, \get_post_meta( $moved->ID, Following::PENDING_META_KEY, false ) ); + + $actors = \get_posts( + array( + 'post_type' => Remote_Actors::POST_TYPE, + 'post_status' => 'any', + 'fields' => 'ids', + 'numberposts' => -1, + ) + ); + $this->assertSame( array( $moved->ID ), $actors, 'Only the new actor may be left.' ); + } + + /** + * A new actor can not take over an old one that does not point at it. + * + * @covers ::handle_move + */ + public function test_handle_move_pull_mode_requires_origin_consent() { + $origin = 'https://old.example/users/erin'; + $target = 'https://new.example/users/erin'; + + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $target, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID ); + } + + /** + * Only the old or the new actor may send the Move. + * + * @covers ::handle_move + */ + public function test_handle_move_rejects_third_party_actor() { + $origin = 'https://old.example/users/frank'; + $target = 'https://new.example/users/frank'; + + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => 'https://third.example/users/mallory', + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID ); + } + + /** + * Any actor type can move, not only a Person. + * + * @covers ::handle_move + */ + public function test_handle_move_accepts_group() { + $origin = 'https://old.example/@blog'; + $target = 'https://new.example/@blog'; + + $id = Remote_Actors::upsert( $this->actor( $origin, array( 'type' => 'Group' ) ) ); + $filter = $this->mock_remote( + array( + $origin => $this->actor( + $origin, + array( + 'type' => 'Group', + 'movedTo' => $target, + ) + ), + $target => $this->actor( + $target, + array( + 'type' => 'Group', + 'alsoKnownAs' => array( $origin ), + ) + ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertNotWPError( Remote_Actors::get_by_uri( $target ) ); + $this->assertNull( \get_post( $id ), 'The old actor must be merged away.' ); + } + + /** + * A Move for an actor we do not store fetches nothing. + * + * @covers ::handle_move + */ + public function test_handle_move_skips_unknown_origin() { + $fetches = 0; + $filter = static function ( $pre ) use ( &$fetches ) { + ++$fetches; + return $pre; + }; + \add_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => 'https://old.example/users/unknown', + 'object' => 'https://old.example/users/unknown', + 'target' => 'https://new.example/users/unknown', + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( 0, $fetches ); + } + + /** + * A Move to a site-blocked target fetches nothing. + * + * @covers ::handle_move + */ + public function test_handle_move_does_not_fetch_blocked_target() { + $origin = 'https://old.example/users/noa'; + $target = 'https://blocked.example/users/noa'; + + Moderation::add_site_block( Moderation::TYPE_DOMAIN, 'blocked.example' ); + Remote_Actors::upsert( $this->actor( $origin ) ); + + $fetched = array(); + $filter = static function ( $pre, $url_or_object ) use ( &$fetched ) { + $fetched[] = object_to_uri( $url_or_object ); return $pre; }; \add_filter( 'activitypub_pre_http_get_remote_object', $filter, 10, 2 ); @@ -683,27 +1022,227 @@ public function test_handle_move_target_with_query_string() { array( 'type' => 'Move', 'actor' => $origin, - 'object' => $target, + 'object' => $origin, + 'target' => $target, ), $this->user_id ); \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + Moderation::remove_site_block( Moderation::TYPE_DOMAIN, 'blocked.example' ); - $this->assertSame( $id, Remote_Actors::get_by_uri( $target )->ID, 'The moved actor must be found by its new id.' ); - $this->assertSame( - 1, - \count( - \get_posts( - array( - 'post_type' => Remote_Actors::POST_TYPE, - 'post_status' => 'any', - 'fields' => 'ids', - 'numberposts' => -1, - ) - ) + $this->assertNotContains( $target, $fetched ); + } + + /** + * A target that can not be stored (no inbox) leaves the old actor as it was. + * + * @covers ::handle_move + */ + public function test_handle_move_keeps_origin_when_target_can_not_be_stored() { + $origin = 'https://old.example/users/ola'; + $target = 'https://new.example/users/ola'; + + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + $broken = $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ); + unset( $broken['inbox'] ); + + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $broken, + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, ), - 'No second actor may be created.' + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID, 'The old actor must stay where it was.' ); + $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); + } + + /** + * An update that fails without an error (wp_update_post() returns 0) leaves the old actor as it was. + * + * @covers ::handle_move + */ + public function test_handle_move_keeps_origin_when_update_fails() { + $origin = 'https://old.example/users/uma'; + $target = 'https://new.example/users/uma'; + + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + \add_filter( 'wp_insert_post_empty_content', '__return_true' ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'wp_insert_post_empty_content', '__return_true' ); + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID, 'The old actor must stay where it was.' ); + $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); + } + + /** + * A merge stores the fresh copy of the new actor, so the new Follows reach its current inbox. + * + * @covers ::handle_move + */ + public function test_handle_move_merge_refreshes_target() { + $origin = 'https://old.example/users/vic'; + $target = 'https://new.example/users/vic'; + + Remote_Actors::upsert( $this->actor( $origin ) ); + $target_id = Remote_Actors::upsert( $this->actor( $target, array( 'inbox' => 'https://new.example/old-inbox' ) ) ); + + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( $target . '/inbox', \get_post_meta( $target_id, '_activitypub_inbox', true ) ); + } + + /** + * A merge into a new actor whose fresh copy can not be stored (no inbox) keeps the old actor. + * + * @covers ::handle_move + */ + public function test_handle_move_merge_keeps_origin_when_target_can_not_be_stored() { + $origin = 'https://old.example/users/wes'; + $target = 'https://new.example/users/wes'; + + $origin_id = Remote_Actors::upsert( $this->actor( $origin ) ); + Remote_Actors::upsert( $this->actor( $target ) ); + \add_post_meta( $origin_id, Following::FOLLOWING_META_KEY, (string) $this->user_id ); + + $broken = $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ); + unset( $broken['inbox'] ); + + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $broken, + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( $origin_id, Remote_Actors::get_by_uri( $origin )->ID, 'The old actor must not be merged away.' ); + $this->assertContains( (string) $this->user_id, \get_post_meta( $origin_id, Following::FOLLOWING_META_KEY, false ) ); + $this->assertCount( 0, $this->get_follows( $this->user_id, $target ) ); + } + + /** + * Build a remote actor document. + * + * @param string $id The actor id. + * @param array $extra Extra properties. + * + * @return array The actor document. + */ + private function actor( $id, $extra = array() ) { + return \array_merge( + array( + 'type' => 'Person', + 'id' => $id, + 'url' => $id, + 'name' => 'Actor', + 'inbox' => $id . '/inbox', + ), + $extra + ); + } + + /** + * Serve remote documents by URL. + * + * @param array $documents The documents, keyed by URL. + * + * @return callable The filter, to remove it again. + */ + private function mock_remote( $documents ) { + $filter = static function ( $pre, $url_or_object ) use ( $documents ) { + return $documents[ object_to_uri( $url_or_object ) ] ?? $pre; + }; + \add_filter( 'activitypub_pre_http_get_remote_object', $filter, 10, 2 ); + + return $filter; + } + + /** + * Get the Follow activities a user sent to an actor. + * + * @param int $user_id The user ID. + * @param string $actor_id The followed actor id. + * + * @return int[] The outbox post IDs. + */ + private function get_follows( $user_id, $actor_id ) { + return \get_posts( + array( + 'post_type' => Outbox::POST_TYPE, + 'post_status' => 'any', + 'author' => $user_id, + 'fields' => 'ids', + 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + array( + 'key' => '_activitypub_object_id', + 'value' => $actor_id, + ), + array( + 'key' => '_activitypub_activity_type', + 'value' => 'Follow', + ), + ), + ) ); } }