Skip to content

Commit 4a4fff1

Browse files
Gargronhiyuki2578
authored andcommitted
Add logging for rejected ActivityPub payloads and add tests (mastodon#10062)
1 parent 3bd9bc4 commit 4a4fff1

5 files changed

Lines changed: 525 additions & 313 deletions

File tree

app/lib/activitypub/activity.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,9 @@ def followed_by_local_accounts?
180180
def requested_through_relay?
181181
@options[:relayed_through_account] && Relay.find_by(inbox_url: @options[:relayed_through_account].inbox_url)&.enabled?
182182
end
183+
184+
def reject_payload!
185+
Rails.logger.info("Rejected #{@json['type']} activity #{@json['id']} from #{@account.uri}#{@options[:relayed_through_account] && "via #{@options[:relayed_through_account].uri}"}")
186+
nil
187+
end
183188
end

app/lib/activitypub/activity/announce.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
class ActivityPub::Activity::Announce < ActivityPub::Activity
44
def perform
5+
return reject_payload! if delete_arrived_first?(@json['id']) || !related_to_local_activity?
6+
57
original_status = status_from_object
68

7-
return if original_status.nil? || delete_arrived_first?(@json['id']) || !announceable?(original_status) || !related_to_local_activity?
9+
return reject_payload! if original_status.nil? || !announceable?(original_status)
810

911
status = Status.find_by(account: @account, reblog: original_status)
1012

app/lib/activitypub/activity/create.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class ActivityPub::Activity::Create < ActivityPub::Activity
44
def perform
5-
return if unsupported_object_type? || invalid_origin?(@object['id']) || Tombstone.exists?(uri: @object['id']) || !related_to_local_activity?
5+
return reject_payload! if unsupported_object_type? || invalid_origin?(@object['id']) || Tombstone.exists?(uri: @object['id']) || !related_to_local_activity?
66

77
RedisLock.acquire(lock_options) do |lock|
88
if lock.acquired?

spec/lib/activitypub/activity/announce_spec.rb

Lines changed: 99 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,63 @@
1818
subject { described_class.new(json, sender) }
1919

2020
before do
21-
Fabricate(:account).follow!(sender)
2221
sender.update(uri: ActivityPub::TagManager.instance.uri_for(sender))
2322
end
2423

2524
describe '#perform' do
26-
before do
27-
subject.perform
25+
context 'when sender is followed by a local account' do
26+
before do
27+
Fabricate(:account).follow!(sender)
28+
subject.perform
29+
end
30+
31+
context 'a known status' do
32+
let(:object_json) do
33+
ActivityPub::TagManager.instance.uri_for(status)
34+
end
35+
36+
it 'creates a reblog by sender of status' do
37+
expect(sender.reblogged?(status)).to be true
38+
end
39+
end
40+
41+
context 'self-boost of a previously unknown status with missing attributedTo' do
42+
let(:object_json) do
43+
{
44+
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
45+
type: 'Note',
46+
content: 'Lorem ipsum',
47+
to: 'http://example.com/followers',
48+
}
49+
end
50+
51+
it 'creates a reblog by sender of status' do
52+
expect(sender.reblogged?(sender.statuses.first)).to be true
53+
end
54+
end
55+
56+
context 'self-boost of a previously unknown status with correct attributedTo' do
57+
let(:object_json) do
58+
{
59+
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
60+
type: 'Note',
61+
content: 'Lorem ipsum',
62+
attributedTo: ActivityPub::TagManager.instance.uri_for(sender),
63+
to: 'http://example.com/followers',
64+
}
65+
end
66+
67+
it 'creates a reblog by sender of status' do
68+
expect(sender.reblogged?(sender.statuses.first)).to be true
69+
end
70+
end
2871
end
2972

30-
context 'a known status' do
73+
context 'when the status belongs to a local user' do
74+
before do
75+
subject.perform
76+
end
77+
3178
let(:object_json) do
3279
ActivityPub::TagManager.instance.uri_for(status)
3380
end
@@ -37,34 +84,68 @@
3784
end
3885
end
3986

40-
context 'self-boost of a previously unknown status with missing attributedTo' do
41-
let(:object_json) do
42-
{
43-
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
44-
type: 'Note',
45-
content: 'Lorem ipsum',
46-
to: 'http://example.com/followers',
47-
}
87+
context 'when the sender is relayed' do
88+
let!(:relay_account) { Fabricate(:account, inbox_url: 'https://relay.example.com/inbox') }
89+
let!(:relay) { Fabricate(:relay, inbox_url: 'https://relay.example.com/inbox') }
90+
91+
subject { described_class.new(json, sender, relayed_through_account: relay_account) }
92+
93+
context 'and the relay is enabled' do
94+
before do
95+
relay.update(state: :accepted)
96+
subject.perform
97+
end
98+
99+
let(:object_json) do
100+
{
101+
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
102+
type: 'Note',
103+
content: 'Lorem ipsum',
104+
to: 'http://example.com/followers',
105+
}
106+
end
107+
108+
it 'creates a reblog by sender of status' do
109+
expect(sender.statuses.count).to eq 2
110+
end
48111
end
49112

50-
it 'creates a reblog by sender of status' do
51-
expect(sender.reblogged?(sender.statuses.first)).to be true
113+
context 'and the relay is disabled' do
114+
before do
115+
subject.perform
116+
end
117+
118+
let(:object_json) do
119+
{
120+
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
121+
type: 'Note',
122+
content: 'Lorem ipsum',
123+
to: 'http://example.com/followers',
124+
}
125+
end
126+
127+
it 'does not create anything' do
128+
expect(sender.statuses.count).to eq 0
129+
end
52130
end
53131
end
54132

55-
context 'self-boost of a previously unknown status with correct attributedTo' do
133+
context 'when the sender has no relevance to local activity' do
134+
before do
135+
subject.perform
136+
end
137+
56138
let(:object_json) do
57139
{
58140
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
59141
type: 'Note',
60142
content: 'Lorem ipsum',
61-
attributedTo: ActivityPub::TagManager.instance.uri_for(sender),
62143
to: 'http://example.com/followers',
63144
}
64145
end
65146

66-
it 'creates a reblog by sender of status' do
67-
expect(sender.reblogged?(sender.statuses.first)).to be true
147+
it 'does not create anything' do
148+
expect(sender.statuses.count).to eq 0
68149
end
69150
end
70151
end

0 commit comments

Comments
 (0)