Skip to content

Commit 8a4842b

Browse files
Gargronhiyuki2578
authored andcommitted
Add HTTP signatures to all outgoing ActivityPub GET requests (mastodon#11284)
1 parent 9a8c14e commit 8a4842b

6 files changed

Lines changed: 43 additions & 40 deletions

File tree

app/helpers/jsonld_helper.rb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,12 @@ def fetch_resource(uri, id, on_behalf_of = nil)
7777
end
7878

7979
def fetch_resource_without_id_validation(uri, on_behalf_of = nil, raise_on_temporary_error = false)
80-
build_request(uri, on_behalf_of).perform do |response|
81-
raise Mastodon::UnexpectedResponseError, response unless response_successful?(response) || response_error_unsalvageable?(response) || !raise_on_temporary_error
82-
83-
return body_to_json(response.body_with_limit) if response.code == 200
84-
end
85-
86-
# If request failed, retry without doing it on behalf of a user
87-
return if on_behalf_of.nil?
80+
on_behalf_of ||= Account.representative
8881

89-
build_request(uri).perform do |response|
82+
build_request(uri, on_behalf_of).perform do |response|
9083
raise Mastodon::UnexpectedResponseError, response unless response_successful?(response) || response_error_unsalvageable?(response) || !raise_on_temporary_error
9184

92-
response.code == 200 ? body_to_json(response.body_with_limit) : nil
85+
body_to_json(response.body_with_limit) if response.code == 200
9386
end
9487
end
9588

app/lib/request.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def initialize(verb, url, **options)
4040
set_digest! if options.key?(:body)
4141
end
4242

43-
def on_behalf_of(account, key_id_format = :acct, sign_with: nil)
44-
raise ArgumentError, 'account must be local' unless account&.local?
43+
def on_behalf_of(account, key_id_format = :uri, sign_with: nil)
44+
raise ArgumentError, 'account must not be nil' if account.nil?
4545

4646
@account = account
4747
@keypair = sign_with.present? ? OpenSSL::PKey::RSA.new(sign_with) : @account.keypair

app/services/fetch_resource_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def process(url, terminal = false)
2323
end
2424

2525
def perform_request(&block)
26-
Request.new(:get, @url).add_headers('Accept' => ACCEPT_HEADER).perform(&block)
26+
Request.new(:get, @url).add_headers('Accept' => ACCEPT_HEADER).on_behalf_of(Account.representative).perform(&block)
2727
end
2828

2929
def process_response(response, terminal = false)

spec/controllers/concerns/signature_verification_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def alternative_success
3838
end
3939

4040
context 'with signature header' do
41-
let!(:author) { Fabricate(:account) }
41+
let!(:author) { Fabricate(:account, domain: 'example.com', uri: 'https://example.com/actor') }
4242

4343
context 'without body' do
4444
before do

spec/services/fetch_remote_account_service_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
let(:url) { 'https://example.com/alice' }
55
let(:prefetched_body) { nil }
66
let(:protocol) { :ostatus }
7+
let!(:representative) { Fabricate(:account) }
78

89
subject { FetchRemoteAccountService.new.call(url, prefetched_body, protocol) }
910

spec/services/fetch_resource_service_spec.rb

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,87 +5,96 @@
55

66
describe '#call' do
77
let(:url) { 'http://example.com' }
8+
89
subject { described_class.new.call(url) }
910

10-
context 'url is blank' do
11+
context 'with blank url' do
1112
let(:url) { '' }
1213
it { is_expected.to be_nil }
1314
end
1415

15-
context 'request failed' do
16+
context 'when request fails' do
1617
before do
17-
WebMock.stub_request(:get, url).to_return(status: 500, body: '', headers: {})
18+
stub_request(:get, url).to_return(status: 500, body: '', headers: {})
1819
end
1920

2021
it { is_expected.to be_nil }
2122
end
2223

23-
context 'raise OpenSSL::SSL::SSLError' do
24+
context 'when OpenSSL::SSL::SSLError is raised' do
2425
before do
25-
allow(Request).to receive_message_chain(:new, :add_headers, :perform).and_raise(OpenSSL::SSL::SSLError)
26+
allow(Request).to receive_message_chain(:new, :add_headers, :on_behalf_of, :perform).and_raise(OpenSSL::SSL::SSLError)
2627
end
2728

28-
it 'return nil' do
29-
is_expected.to be_nil
30-
end
29+
it { is_expected.to be_nil }
3130
end
3231

33-
context 'raise HTTP::ConnectionError' do
32+
context 'when HTTP::ConnectionError is raised' do
3433
before do
35-
allow(Request).to receive_message_chain(:new, :add_headers, :perform).and_raise(HTTP::ConnectionError)
34+
allow(Request).to receive_message_chain(:new, :add_headers, :on_behalf_of, :perform).and_raise(HTTP::ConnectionError)
3635
end
3736

38-
it 'return nil' do
39-
is_expected.to be_nil
40-
end
37+
it { is_expected.to be_nil }
4138
end
4239

43-
context 'response success' do
40+
context 'when request succeeds' do
4441
let(:body) { '' }
45-
let(:headers) { { 'Content-Type' => content_type } }
46-
let(:json) {
47-
{ id: 1,
42+
43+
let(:content_type) { 'application/json' }
44+
45+
let(:headers) do
46+
{ 'Content-Type' => content_type }
47+
end
48+
49+
let(:json) do
50+
{
51+
id: 1,
4852
'@context': ActivityPub::TagManager::CONTEXT,
4953
type: 'Note',
5054
}.to_json
51-
}
55+
end
5256

5357
before do
54-
WebMock.stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
58+
stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
59+
end
60+
61+
it 'signs request' do
62+
subject
63+
expect(a_request(:get, url).with(headers: { 'Signature' => /keyId="#{Regexp.escape(ActivityPub::TagManager.instance.uri_for(representative) + '#main-key')}"/ })).to have_been_made
5564
end
5665

57-
context 'content type is application/atom+xml' do
66+
context 'when content type is application/atom+xml' do
5867
let(:content_type) { 'application/atom+xml' }
5968

6069
it { is_expected.to eq nil }
6170
end
6271

63-
context 'content_type is activity+json' do
72+
context 'when content type is activity+json' do
6473
let(:content_type) { 'application/activity+json; charset=utf-8' }
6574
let(:body) { json }
6675

6776
it { is_expected.to eq [1, { prefetched_body: body, id: true }, :activitypub] }
6877
end
6978

70-
context 'content_type is ld+json with profile' do
79+
context 'when content type is ld+json with profile' do
7180
let(:content_type) { 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' }
7281
let(:body) { json }
7382

7483
it { is_expected.to eq [1, { prefetched_body: body, id: true }, :activitypub] }
7584
end
7685

7786
before do
78-
WebMock.stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
79-
WebMock.stub_request(:get, 'http://example.com/foo').to_return(status: 200, body: json, headers: { 'Content-Type' => 'application/activity+json' })
87+
stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
88+
stub_request(:get, 'http://example.com/foo').to_return(status: 200, body: json, headers: { 'Content-Type' => 'application/activity+json' })
8089
end
8190

82-
context 'has link header' do
91+
context 'when link header is present' do
8392
let(:headers) { { 'Link' => '<http://example.com/foo>; rel="alternate"; type="application/activity+json"', } }
8493

8594
it { is_expected.to eq [1, { prefetched_body: json, id: true }, :activitypub] }
8695
end
8796

88-
context 'content type is text/html' do
97+
context 'when content type is text/html' do
8998
let(:content_type) { 'text/html' }
9099
let(:body) { '<html><head><link rel="alternate" href="http://example.com/foo" type="application/activity+json"/></head></html>' }
91100

0 commit comments

Comments
 (0)