From 34c4f481f0d56e6a57fd5dd0856572b3b7614469 Mon Sep 17 00:00:00 2001 From: Jeong Arm Date: Thu, 15 Feb 2024 14:13:36 +0900 Subject: [PATCH 1/5] Ignore if mentioned account was not processable --- app/lib/activitypub/activity/create.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/lib/activitypub/activity/create.rb b/app/lib/activitypub/activity/create.rb index 85195f4c395..766fa52caf7 100644 --- a/app/lib/activitypub/activity/create.rb +++ b/app/lib/activitypub/activity/create.rb @@ -225,7 +225,11 @@ class ActivityPub::Activity::Create < ActivityPub::Activity return if tag['href'].blank? account = account_from_uri(tag['href']) - account = ActivityPub::FetchRemoteAccountService.new.call(tag['href'], request_id: @options[:request_id]) if account.nil? + begin + account = ActivityPub::FetchRemoteAccountService.new.call(tag['href'], request_id: @options[:request_id]) if account.nil? + rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, HTTP::OpenSSL::SSLError + account = nil + end return if account.nil? From b8307f5d6daa9fb9bcd5e5d3efa02fefecd8e724 Mon Sep 17 00:00:00 2001 From: Jeong Arm Date: Thu, 15 Feb 2024 14:55:32 +0900 Subject: [PATCH 2/5] Add test --- spec/lib/activitypub/activity/create_spec.rb | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/spec/lib/activitypub/activity/create_spec.rb b/spec/lib/activitypub/activity/create_spec.rb index e4966cffa33..2e303f82c27 100644 --- a/spec/lib/activitypub/activity/create_spec.rb +++ b/spec/lib/activitypub/activity/create_spec.rb @@ -63,6 +63,24 @@ RSpec.describe ActivityPub::Activity::Create do } end + let(:invalid_mention_json) do + { + id: [ActivityPub::TagManager.instance.uri_for(sender), 'post2'].join('/'), + type: 'Note', + to: [ + 'https://www.w3.org/ns/activitystreams#Public', + ActivityPub::TagManager.instance.uri_for(follower), + ], + content: '@bob lorem ipsum', + published: 1.hour.ago.utc.iso8601, + updated: 1.hour.ago.utc.iso8601, + tag: { + type: 'Mention', + href: 'http://notexisting.dontexistingtld/actor', + }, + } + end + def activity_for_object(json) { '@context': 'https://www.w3.org/ns/activitystreams', @@ -117,6 +135,21 @@ RSpec.describe ActivityPub::Activity::Create do # Creates two notifications expect(Notification.count).to eq 2 end + + it 'ignores unprocessable mention', :aggregate_failures do + # When receiving the post that contains an invalid mention… + described_class.new(activity_for_object(invalid_mention_json), sender, delivery: true).perform + + # NOTE: Refering explicitly to the workers is a bit awkward + DistributionWorker.drain + FeedInsertWorker.drain + + # …it creates a status with an unknown parent + status = Status.find_by(uri: invalid_mention_json[:id]) + + # Check the process did not crash + expect(status.nil?).to be false + end end describe '#perform' do From 31052e3fbe67a81538423a4a805036ca6d5a2a93 Mon Sep 17 00:00:00 2001 From: Jeong Arm Date: Thu, 15 Feb 2024 15:03:07 +0900 Subject: [PATCH 3/5] Fix little typo --- app/lib/activitypub/activity/create.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/lib/activitypub/activity/create.rb b/app/lib/activitypub/activity/create.rb index 766fa52caf7..decebc9d820 100644 --- a/app/lib/activitypub/activity/create.rb +++ b/app/lib/activitypub/activity/create.rb @@ -227,7 +227,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity account = account_from_uri(tag['href']) begin account = ActivityPub::FetchRemoteAccountService.new.call(tag['href'], request_id: @options[:request_id]) if account.nil? - rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, HTTP::OpenSSL::SSLError + rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError account = nil end From ebb94eb7db7d1d62f902bab004c7bb9fb67abf15 Mon Sep 17 00:00:00 2001 From: Jeong Arm Date: Thu, 15 Feb 2024 15:03:19 +0900 Subject: [PATCH 4/5] Fix test --- spec/lib/activitypub/activity/create_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/lib/activitypub/activity/create_spec.rb b/spec/lib/activitypub/activity/create_spec.rb index 2e303f82c27..80bdce49bac 100644 --- a/spec/lib/activitypub/activity/create_spec.rb +++ b/spec/lib/activitypub/activity/create_spec.rb @@ -137,6 +137,7 @@ RSpec.describe ActivityPub::Activity::Create do end it 'ignores unprocessable mention', :aggregate_failures do + stub_request(:get, invalid_mention_json[:tag][:href]).to_return(status: 500) # When receiving the post that contains an invalid mention… described_class.new(activity_for_object(invalid_mention_json), sender, delivery: true).perform From f6bbd046e93ca14959b84956e46d21c1c3d8cb30 Mon Sep 17 00:00:00 2001 From: Jeong Arm Date: Thu, 15 Feb 2024 15:20:56 +0900 Subject: [PATCH 5/5] Let test raise connection error instead of returning 500 --- spec/lib/activitypub/activity/create_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lib/activitypub/activity/create_spec.rb b/spec/lib/activitypub/activity/create_spec.rb index 80bdce49bac..5beb47830c8 100644 --- a/spec/lib/activitypub/activity/create_spec.rb +++ b/spec/lib/activitypub/activity/create_spec.rb @@ -137,7 +137,7 @@ RSpec.describe ActivityPub::Activity::Create do end it 'ignores unprocessable mention', :aggregate_failures do - stub_request(:get, invalid_mention_json[:tag][:href]).to_return(status: 500) + stub_request(:get, invalid_mention_json[:tag][:href]).to_raise(HTTP::ConnectionError) # When receiving the post that contains an invalid mention… described_class.new(activity_for_object(invalid_mention_json), sender, delivery: true).perform