-
Couldn't load subscription status.
- Fork 84
Fix infinite recursion when storing remote actors with mentions #2369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
818f517
Fix infinite recursion when storing remote actors with mentions
obenland cb4f4d7
Add changelog
matticbot 785fe1a
Use explicit filter removal in recursion tests
obenland a83b392
Merge branch 'trunk' into fix/infinite-recursion-actor-mentions
obenland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: patch | ||
| Type: fixed | ||
|
|
||
| Fix infinite recursion when storing remote actors with mentions in their bios |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| namespace Activitypub\Tests\Collection; | ||
|
|
||
| use Activitypub\Collection\Remote_Actors; | ||
| use Activitypub\Mention; | ||
|
|
||
| /** | ||
| * Class Test_Remote_Actors | ||
|
|
@@ -873,6 +874,199 @@ function ( $preempt, $parsed_args, $url ) { | |
| \wp_delete_post( $post_id4, true ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that saving a remote actor with a self-mention doesn't cause infinite recursion. | ||
| * | ||
| * @covers ::create | ||
| * @covers ::prepare_custom_post_type | ||
| */ | ||
| public function test_create_actor_with_self_mention_no_recursion() { | ||
| // Ensure the Mention filter is active to test for recursion. | ||
| Mention::init(); | ||
|
|
||
| // Create an actor with a self-mention in their summary. | ||
| $actor = array( | ||
| 'id' => 'https://remote.example.com/actor/self-mention', | ||
| 'type' => 'Person', | ||
| 'url' => 'https://remote.example.com/actor/self-mention', | ||
| 'inbox' => 'https://remote.example.com/actor/self-mention/inbox', | ||
| 'name' => 'Self Mention User', | ||
| 'preferredUsername' => 'selfmention', | ||
| 'summary' => 'Hello, I am @[email protected] and I like to mention myself!', | ||
| 'endpoints' => array( | ||
| 'sharedInbox' => 'https://remote.example.com/inbox', | ||
| ), | ||
| ); | ||
|
|
||
| // Mock webfinger to resolve the mention. | ||
| $webfinger_callback = function ( $preempt, $parsed_args, $url ) { | ||
| if ( strpos( $url, '.well-known/webfinger' ) !== false ) { | ||
| return array( | ||
| 'response' => array( 'code' => 200 ), | ||
| 'body' => wp_json_encode( | ||
| array( | ||
| 'subject' => 'acct:[email protected]', | ||
| 'links' => array( | ||
| array( | ||
| 'rel' => 'self', | ||
| 'type' => 'application/activity+json', | ||
| 'href' => 'https://remote.example.com/actor/self-mention', | ||
| ), | ||
| ), | ||
| ) | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| return $preempt; | ||
| }; | ||
| \add_filter( 'pre_http_request', $webfinger_callback, 10, 3 ); | ||
|
|
||
| // Mock remote actor fetch to return the same actor (creating potential recursion). | ||
| $actor_fetch_callback = function ( $pre, $url_or_object ) use ( $actor ) { | ||
| if ( $url_or_object === $actor['id'] ) { | ||
| return $actor; | ||
| } | ||
|
|
||
| return $pre; | ||
| }; | ||
| \add_filter( 'activitypub_pre_http_get_remote_object', $actor_fetch_callback, 10, 2 ); | ||
|
|
||
| // This should not cause infinite recursion. | ||
| $post_id = Remote_Actors::create( $actor ); | ||
|
|
||
| $this->assertIsInt( $post_id ); | ||
| $this->assertGreaterThan( 0, $post_id ); | ||
|
|
||
| $post = \get_post( $post_id ); | ||
| $this->assertInstanceOf( '\WP_Post', $post ); | ||
| $this->assertEquals( 'https://remote.example.com/actor/self-mention', $post->guid ); | ||
|
|
||
| // Verify the summary was stored correctly (without being processed for mentions). | ||
| $this->assertStringContainsString( '@[email protected]', $post->post_excerpt ); | ||
|
|
||
| // Clean up - remove only the specific filters we added. | ||
| \remove_filter( 'pre_http_request', $webfinger_callback ); | ||
| \remove_filter( 'activitypub_pre_http_get_remote_object', $actor_fetch_callback ); | ||
| \remove_filter( 'activitypub_activity_object_array', array( 'Activitypub\Mention', 'filter_activity_object' ), 99 ); | ||
| \remove_filter( 'activitypub_activity_object_array', array( 'Activitypub\Hashtag', 'filter_activity_object' ), 99 ); | ||
| \remove_filter( 'activitypub_activity_object_array', array( 'Activitypub\Link', 'filter_activity_object' ), 99 ); | ||
| \wp_delete_post( $post_id, true ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that saving a remote actor with mentions of other actors doesn't cause recursion. | ||
| * | ||
| * @covers ::create | ||
| * @covers ::prepare_custom_post_type | ||
| */ | ||
| public function test_create_actor_with_cross_mentions_no_recursion() { | ||
| // Ensure the Mention filter is active to test for recursion. | ||
| Mention::init(); | ||
|
|
||
| // Create two actors that mention each other in their bios. | ||
| $actor_a = array( | ||
| 'id' => 'https://remote.example.com/actor/alice-cross', | ||
| 'type' => 'Person', | ||
| 'url' => 'https://remote.example.com/actor/alice-cross', | ||
| 'inbox' => 'https://remote.example.com/actor/alice-cross/inbox', | ||
| 'name' => 'Alice', | ||
| 'preferredUsername' => 'alice', | ||
| 'summary' => 'Best friends with @[email protected]', | ||
| 'endpoints' => array( | ||
| 'sharedInbox' => 'https://remote.example.com/inbox', | ||
| ), | ||
| ); | ||
|
|
||
| $actor_b = array( | ||
| 'id' => 'https://remote.example.com/actor/bob-cross', | ||
| 'type' => 'Person', | ||
| 'url' => 'https://remote.example.com/actor/bob-cross', | ||
| 'inbox' => 'https://remote.example.com/actor/bob-cross/inbox', | ||
| 'name' => 'Bob', | ||
| 'preferredUsername' => 'bob', | ||
| 'summary' => 'Best friends with @[email protected]', | ||
| 'endpoints' => array( | ||
| 'sharedInbox' => 'https://remote.example.com/inbox', | ||
| ), | ||
| ); | ||
|
|
||
| // Mock webfinger to resolve the mentions. | ||
| $webfinger_callback = function ( $preempt, $parsed_args, $url ) { | ||
| if ( strpos( $url, '.well-known/webfinger' ) !== false ) { | ||
| if ( strpos( $url, '[email protected]' ) !== false ) { | ||
| return array( | ||
| 'response' => array( 'code' => 200 ), | ||
| 'body' => wp_json_encode( | ||
| array( | ||
| 'subject' => 'acct:[email protected]', | ||
| 'links' => array( | ||
| array( | ||
| 'rel' => 'self', | ||
| 'type' => 'application/activity+json', | ||
| 'href' => 'https://remote.example.com/actor/bob-cross', | ||
| ), | ||
| ), | ||
| ) | ||
| ), | ||
| ); | ||
| } elseif ( strpos( $url, '[email protected]' ) !== false ) { | ||
| return array( | ||
| 'response' => array( 'code' => 200 ), | ||
| 'body' => wp_json_encode( | ||
| array( | ||
| 'subject' => 'acct:[email protected]', | ||
| 'links' => array( | ||
| array( | ||
| 'rel' => 'self', | ||
| 'type' => 'application/activity+json', | ||
| 'href' => 'https://remote.example.com/actor/alice-cross', | ||
| ), | ||
| ), | ||
| ) | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return $preempt; | ||
| }; | ||
| \add_filter( 'pre_http_request', $webfinger_callback, 10, 3 ); | ||
|
|
||
| // Mock the remote fetch to return the cross-mentioned actors. | ||
| $actor_fetch_callback = function ( $pre, $url_or_object ) use ( $actor_a, $actor_b ) { | ||
| if ( $url_or_object === $actor_a['id'] ) { | ||
| return $actor_a; | ||
| } | ||
| if ( $url_or_object === $actor_b['id'] ) { | ||
| return $actor_b; | ||
| } | ||
|
|
||
| return $pre; | ||
| }; | ||
| \add_filter( 'activitypub_pre_http_get_remote_object', $actor_fetch_callback, 10, 2 ); | ||
|
|
||
| // This should not cause infinite recursion when creating both actors. | ||
| $post_id_a = Remote_Actors::create( $actor_a ); | ||
| $this->assertIsInt( $post_id_a ); | ||
|
|
||
| $post_id_b = Remote_Actors::create( $actor_b ); | ||
| $this->assertIsInt( $post_id_b ); | ||
|
|
||
| // Verify both were created successfully. | ||
| $this->assertGreaterThan( 0, $post_id_a ); | ||
| $this->assertGreaterThan( 0, $post_id_b ); | ||
|
|
||
| // Clean up - remove only the specific filters we added. | ||
| \remove_filter( 'pre_http_request', $webfinger_callback ); | ||
| \remove_filter( 'activitypub_pre_http_get_remote_object', $actor_fetch_callback ); | ||
| \remove_filter( 'activitypub_activity_object_array', array( 'Activitypub\Mention', 'filter_activity_object' ), 99 ); | ||
| \remove_filter( 'activitypub_activity_object_array', array( 'Activitypub\Hashtag', 'filter_activity_object' ), 99 ); | ||
| \remove_filter( 'activitypub_activity_object_array', array( 'Activitypub\Link', 'filter_activity_object' ), 99 ); | ||
| \wp_delete_post( $post_id_a, true ); | ||
| \wp_delete_post( $post_id_b, true ); | ||
| } | ||
|
|
||
| /** | ||
| * Pre get remote metadata by actor. | ||
| * | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.