Skip to content
Open
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
4 changes: 4 additions & 0 deletions .github/changelog/fix-cleanup-remote-actors-refetch
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Ensure remote actor cleanup re-fetches metadata with cache bypassed, deletes only on 410 or confirmed tombstones, and schedules cleanups with post ID.
47 changes: 42 additions & 5 deletions includes/class-scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,20 +368,57 @@ public static function cleanup_remote_actors() {
$actors = Remote_Actors::get_faulty( $number );

foreach ( $actors as $actor ) {
$meta = get_remote_metadata_by_actor( unescape_guid( $actor->guid ), false );
$guid = unescape_guid( $actor->guid );

if ( Tombstone::exists( $meta ) ) {
/*
* Use Proxy::get() directly with cache bypassed.
* get_remote_metadata_by_actor() short-circuits to the locally cached
* ap_actor post via Remote_Actors::fetch_by_various() whenever one exists,
* so faulty actors would always rewrite stale local data and clear errors.
*/
$meta = Proxy::get( $guid, array( 'cached' => false ) );

/*
* Only delete immediately on HTTP 410 Gone or a confirmed Tombstone object.
* HTTP 404 is treated as a regular error below so that a transient
* misconfiguration or temporary server issue does not purge followers.
*/
$is_tombstone = false;
if ( \is_wp_error( $meta ) ) {
$status = (int) ( $meta->get_error_data()['status'] ?? $meta->get_error_code() );
$is_tombstone = 410 === $status;
} elseif ( ! empty( $meta ) && Tombstone::exists( $meta ) ) {
$is_tombstone = true;
}

if ( $is_tombstone ) {
\wp_schedule_single_event( \time(), 'activitypub_delete_remote_actor_interactions', array( $actor->ID ) );
\wp_schedule_single_event( \time(), 'activitypub_delete_remote_actor_posts', array( $actor->ID ) );
Remote_Actors::delete( $actor->ID );
} elseif ( empty( $meta ) || ! \is_array( $meta ) || \is_wp_error( $meta ) ) {
if ( Remote_Actors::count_errors( $actor->ID ) >= 5 ) {
\wp_schedule_single_event( \time(), 'activitypub_delete_remote_actor_interactions', array( $actor->guid ) );
\wp_schedule_single_event( \time(), 'activitypub_delete_remote_actor_posts', array( $actor->guid ) );
\wp_schedule_single_event( \time(), 'activitypub_delete_remote_actor_interactions', array( $actor->ID ) );
\wp_schedule_single_event( \time(), 'activitypub_delete_remote_actor_posts', array( $actor->ID ) );
Remote_Actors::delete( $actor->ID );
} else {
Remote_Actors::add_error( $actor->ID, $meta );
}
} else {
$id = Remote_Actors::upsert( $meta );
/*
* Guard against identity drift: if the remote document declares an id
* different from the actor's guid, skip it to avoid colliding with another
* actor or rewriting the identity unexpectedly.
*/
$fetched_id = isset( $meta['id'] ) && \is_string( $meta['id'] ) ? \esc_url_raw( $meta['id'] ) : '';
if ( $fetched_id !== $guid ) {
continue;
}

/*
* Update the existing actor in place using the known ID, avoiding
* the redundant get_by_uri() query upsert() would do.
*/
$id = Remote_Actors::update( $actor->ID, $meta );
if ( \is_wp_error( $id ) ) {
Remote_Actors::add_error( $actor->ID, $id );
} else {
Expand Down
245 changes: 233 additions & 12 deletions tests/phpunit/tests/includes/class-test-scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ public function test_purge_inbox_with_different_purge_days() {
}

/**
* Test cleanup_remote_actors method.
* Test cleanup_remote_actors method schedules deletion events with post ID when error threshold is reached.
*
* @covers ::cleanup_remote_actors
*/
Expand All @@ -714,7 +714,10 @@ public function test_cleanup_remote_actors() {
// Track scheduled events.
$scheduled_events = array();
$schedule_event_callback = function ( $event ) use ( &$scheduled_events ) {
if ( 'activitypub_delete_remote_actor_interactions' === $event->hook ) {
if (
'activitypub_delete_remote_actor_interactions' === $event->hook ||
'activitypub_delete_remote_actor_posts' === $event->hook
) {
$scheduled_events[] = array(
'hook' => $event->hook,
'args' => $event->args,
Expand All @@ -724,29 +727,247 @@ public function test_cleanup_remote_actors() {
return $event;
};
\add_filter( 'schedule_event', $schedule_event_callback );
$pre_get_remote_metadata_by_actor_callback = function () {
return new \WP_Error( 'no_actor', 'No actor found' );

\remove_filter( 'activitypub_pre_http_get_remote_object', $activitypub_pre_http_get_remote_object_callback );
$pre_http_error_callback = function () {
return new \WP_Error( 500, 'Internal Server Error', array( 'status' => 500 ) );
};
\add_filter( 'pre_get_remote_metadata_by_actor', $pre_get_remote_metadata_by_actor_callback );
\add_filter( 'activitypub_pre_http_get_remote_object', $pre_http_error_callback );

// Run the cleanup function.
Scheduler::cleanup_remote_actors();

// Verify that the event was scheduled with the actor URL as parameter.
$this->assertCount( 1, $scheduled_events, 'Should schedule 1 event' );
$this->assertEquals( 'activitypub_delete_remote_actor_interactions', $scheduled_events[0]['hook'], 'Should schedule the correct hook' );
$this->assertCount( 1, $scheduled_events[0]['args'], 'Should have 1 argument' );
$this->assertEquals( 'https://example.com/users/test', $scheduled_events[0]['args'][0], 'Should pass actor URL as parameter' );
// Verify that deletion events were scheduled with the actor post ID as parameter.
$this->assertCount( 2, $scheduled_events, 'Should schedule 2 events (interactions and posts)' );
$this->assertEquals( 'activitypub_delete_remote_actor_interactions', $scheduled_events[0]['hook'] );
$this->assertEquals( array( $actor->ID ), $scheduled_events[0]['args'], 'Should pass actor post ID to interactions deletion' );
$this->assertEquals( 'activitypub_delete_remote_actor_posts', $scheduled_events[1]['hook'] );
$this->assertEquals( array( $actor->ID ), $scheduled_events[1]['args'], 'Should pass actor post ID to posts deletion' );

// Verify the actor was deleted.
$this->assertNull( \get_post( $actor->ID ), 'Actor should be deleted' );

// Clean up.
\remove_filter( 'activitypub_pre_http_get_remote_object', $activitypub_pre_http_get_remote_object_callback );
\remove_filter( 'pre_get_remote_metadata_by_actor', $pre_get_remote_metadata_by_actor_callback );
\remove_filter( 'activitypub_pre_http_get_remote_object', $pre_http_error_callback );
\remove_filter( 'schedule_event', $schedule_event_callback );
}

/**
* Test that HTTP 404 is treated as a regular error and does not immediately delete the actor.
*
* @covers ::cleanup_remote_actors
*/
public function test_cleanup_remote_actors_404_increments_error_without_immediate_deletion() {
$actor_data = array(
'type' => 'Person',
'name' => 'Test 404 User',
'preferredUsername' => 'test404',
'id' => 'https://example.com/users/test404',
'url' => 'https://example.com/@test404',
'inbox' => 'https://example.com/users/test404/inbox',
);
$initial_mock = function () use ( $actor_data ) {
return $actor_data;
};
\add_filter( 'activitypub_pre_http_get_remote_object', $initial_mock );
$actor = Remote_Actors::fetch_by_uri( $actor_data['id'] );
\remove_filter( 'activitypub_pre_http_get_remote_object', $initial_mock );

Remote_Actors::add_error( $actor->ID, 'Previous failure' );
$this->assertSame( 1, Remote_Actors::count_errors( $actor->ID ) );

// Mock 404 response on cleanup refetch.
$mock_404 = function () {
return new \WP_Error( 404, 'Not Found', array( 'status' => 404 ) );
};
\add_filter( 'activitypub_pre_http_get_remote_object', $mock_404 );

Scheduler::cleanup_remote_actors();

// Actor must NOT be deleted on a single 404.
$this->assertNotNull( \get_post( $actor->ID ), 'Actor should not be deleted on 404' );
// Error count must increment.
$this->assertSame( 2, Remote_Actors::count_errors( $actor->ID ), 'Error count should increment on 404' );

\remove_filter( 'activitypub_pre_http_get_remote_object', $mock_404 );
}

/**
* Test that HTTP 410 Gone immediately deletes the actor and schedules cleanups.
*
* @covers ::cleanup_remote_actors
*/
public function test_cleanup_remote_actors_410_immediately_deletes_actor() {
$actor_data = array(
'type' => 'Person',
'name' => 'Test 410 User',
'preferredUsername' => 'test410',
'id' => 'https://example.com/users/test410',
'url' => 'https://example.com/@test410',
'inbox' => 'https://example.com/users/test410/inbox',
);
$initial_mock = function () use ( $actor_data ) {
return $actor_data;
};
\add_filter( 'activitypub_pre_http_get_remote_object', $initial_mock );
$actor = Remote_Actors::fetch_by_uri( $actor_data['id'] );
\remove_filter( 'activitypub_pre_http_get_remote_object', $initial_mock );

Remote_Actors::add_error( $actor->ID, 'Previous failure' );

$scheduled_events = array();
$schedule_event_callback = function ( $event ) use ( &$scheduled_events ) {
if (
'activitypub_delete_remote_actor_interactions' === $event->hook ||
'activitypub_delete_remote_actor_posts' === $event->hook
) {
$scheduled_events[] = array(
'hook' => $event->hook,
'args' => $event->args,
);
}
return $event;
};
\add_filter( 'schedule_event', $schedule_event_callback );

// Mock 410 Gone response on cleanup refetch.
$mock_410 = function () {
return new \WP_Error( 410, 'Gone', array( 'status' => 410 ) );
};
\add_filter( 'activitypub_pre_http_get_remote_object', $mock_410 );

Scheduler::cleanup_remote_actors();

// Actor must be deleted immediately on 410.
$this->assertNull( \get_post( $actor->ID ), 'Actor should be deleted immediately on 410' );
$this->assertCount( 2, $scheduled_events, 'Should schedule interactions and posts deletion on 410' );
$this->assertEquals( array( $actor->ID ), $scheduled_events[0]['args'] );
$this->assertEquals( array( $actor->ID ), $scheduled_events[1]['args'] );

\remove_filter( 'activitypub_pre_http_get_remote_object', $mock_410 );
\remove_filter( 'schedule_event', $schedule_event_callback );
}

/**
* Test that a confirmed Tombstone object immediately deletes the actor.
*
* @covers ::cleanup_remote_actors
*/
public function test_cleanup_remote_actors_tombstone_object_immediately_deletes_actor() {
$actor_data = array(
'type' => 'Person',
'name' => 'Test Tombstone User',
'preferredUsername' => 'testtombstone',
'id' => 'https://example.com/users/testtombstone',
'url' => 'https://example.com/@testtombstone',
'inbox' => 'https://example.com/users/testtombstone/inbox',
);
$initial_mock = function () use ( $actor_data ) {
return $actor_data;
};
\add_filter( 'activitypub_pre_http_get_remote_object', $initial_mock );
$actor = Remote_Actors::fetch_by_uri( $actor_data['id'] );
\remove_filter( 'activitypub_pre_http_get_remote_object', $initial_mock );

Remote_Actors::add_error( $actor->ID, 'Previous failure' );

// Mock real Tombstone object on cleanup refetch.
$mock_tombstone = function () use ( $actor_data ) {
return array(
'type' => 'Tombstone',
'id' => $actor_data['id'],
);
};
\add_filter( 'activitypub_pre_http_get_remote_object', $mock_tombstone );

Scheduler::cleanup_remote_actors();

// Actor must be deleted immediately on Tombstone object.
$this->assertNull( \get_post( $actor->ID ), 'Actor should be deleted on Tombstone object' );

\remove_filter( 'activitypub_pre_http_get_remote_object', $mock_tombstone );
}

/**
* Test that a successful refetch updates the actor and clears errors.
*
* @covers ::cleanup_remote_actors
*/
public function test_cleanup_remote_actors_successful_refetch_clears_errors() {
$actor_data = array(
'type' => 'Person',
'name' => 'Original User',
'preferredUsername' => 'testrecover',
'id' => 'https://example.com/users/testrecover',
'url' => 'https://example.com/@testrecover',
'inbox' => 'https://example.com/users/testrecover/inbox',
);
$initial_mock = function () use ( $actor_data ) {
return $actor_data;
};
\add_filter( 'activitypub_pre_http_get_remote_object', $initial_mock );
$actor = Remote_Actors::fetch_by_uri( $actor_data['id'] );
\remove_filter( 'activitypub_pre_http_get_remote_object', $initial_mock );

Remote_Actors::add_error( $actor->ID, 'Temporary error' );
$this->assertSame( 1, Remote_Actors::count_errors( $actor->ID ) );

// Mock updated metadata with new name.
$updated_data = array_merge( $actor_data, array( 'name' => 'Recovered User' ) );
$recovery_mock = function () use ( $updated_data ) {
return $updated_data;
};
\add_filter( 'activitypub_pre_http_get_remote_object', $recovery_mock );

Scheduler::cleanup_remote_actors();

$this->assertNotNull( \get_post( $actor->ID ), 'Actor should remain present' );
$this->assertSame( 0, Remote_Actors::count_errors( $actor->ID ), 'Errors should be cleared upon successful recovery' );
$this->assertSame( 'Recovered User', \get_post( $actor->ID )->post_title );

\remove_filter( 'activitypub_pre_http_get_remote_object', $recovery_mock );
}

/**
* Test that identity drift (different ID returned) is skipped to avoid collision or corrupting guid.
*
* @covers ::cleanup_remote_actors
*/
public function test_cleanup_remote_actors_id_mismatch_is_skipped() {
$actor_data = array(
'type' => 'Person',
'name' => 'Original User',
'preferredUsername' => 'testmismatch',
'id' => 'https://example.com/users/testmismatch',
'url' => 'https://example.com/@testmismatch',
'inbox' => 'https://example.com/users/testmismatch/inbox',
);
$initial_mock = function () use ( $actor_data ) {
return $actor_data;
};
\add_filter( 'activitypub_pre_http_get_remote_object', $initial_mock );
$actor = Remote_Actors::fetch_by_uri( $actor_data['id'] );
\remove_filter( 'activitypub_pre_http_get_remote_object', $initial_mock );

Remote_Actors::add_error( $actor->ID, 'Original error' );

// Mock a response with a different ID.
$drifted_data = array_merge( $actor_data, array( 'id' => 'https://example.com/users/different-person' ) );
$mismatch_mock = function () use ( $drifted_data ) {
return $drifted_data;
};
\add_filter( 'activitypub_pre_http_get_remote_object', $mismatch_mock );

Scheduler::cleanup_remote_actors();

$post = \get_post( $actor->ID );
$this->assertNotNull( $post, 'Actor should remain present' );
$this->assertSame( 'https://example.com/users/testmismatch', $post->guid, 'Guid must not be overwritten on ID mismatch' );
$this->assertSame( 1, Remote_Actors::count_errors( $actor->ID ), 'Errors should not be cleared when skipped' );

\remove_filter( 'activitypub_pre_http_get_remote_object', $mismatch_mock );
}

/**
* Test purge_ap_posts method with more than 200 posts.
*
Expand Down
Loading