Refactor ActivityPub handling to prepare for non-Account actors (#19212)
* Move ActivityPub::FetchRemoteAccountService to ActivityPub::FetchRemoteActorService ActivityPub::FetchRemoteAccountService is kept as a wrapper for when the actor is specifically required to be an Account * Refactor SignatureVerification to allow non-Account actors * fixup! Move ActivityPub::FetchRemoteAccountService to ActivityPub::FetchRemoteActorService * Refactor ActivityPub::FetchRemoteKeyService to potentially return non-Account actors * Refactor inbound ActivityPub payload processing to accept non-Account actors * Refactor inbound ActivityPub processing to accept activities relayed through non-Account * Refactor how Account key URIs are built * Refactor Request and drop unused key_id_format parameter * Rename ActivityPub::Dereferencer `signature_account` to `signature_actor`th-downstream
parent
2b97fbbd88
commit
e1d78575c4
@ -1,80 +1,12 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class ActivityPub::FetchRemoteAccountService < BaseService
|
class ActivityPub::FetchRemoteAccountService < ActivityPub::FetchRemoteActorService
|
||||||
include JsonLdHelper
|
|
||||||
include DomainControlHelper
|
|
||||||
include WebfingerHelper
|
|
||||||
|
|
||||||
class Error < StandardError; end
|
|
||||||
|
|
||||||
SUPPORTED_TYPES = %w(Application Group Organization Person Service).freeze
|
|
||||||
|
|
||||||
# Does a WebFinger roundtrip on each call, unless `only_key` is true
|
# Does a WebFinger roundtrip on each call, unless `only_key` is true
|
||||||
def call(uri, id: true, prefetched_body: nil, break_on_redirect: false, only_key: false, suppress_errors: true)
|
def call(uri, id: true, prefetched_body: nil, break_on_redirect: false, only_key: false, suppress_errors: true)
|
||||||
return if domain_not_allowed?(uri)
|
actor = super
|
||||||
return ActivityPub::TagManager.instance.uri_to_resource(uri, Account) if ActivityPub::TagManager.instance.local_uri?(uri)
|
return actor if actor.nil? || actor.is_a?(Account)
|
||||||
|
|
||||||
@json = begin
|
|
||||||
if prefetched_body.nil?
|
|
||||||
fetch_resource(uri, id)
|
|
||||||
else
|
|
||||||
body_to_json(prefetched_body, compare_id: id ? uri : nil)
|
|
||||||
end
|
|
||||||
rescue Oj::ParseError
|
|
||||||
raise Error, "Error parsing JSON-LD document #{uri}"
|
|
||||||
end
|
|
||||||
|
|
||||||
raise Error, "Error fetching actor JSON at #{uri}" if @json.nil?
|
|
||||||
raise Error, "Unsupported JSON-LD context for document #{uri}" unless supported_context?
|
|
||||||
raise Error, "Unexpected object type for actor #{uri} (expected any of: #{SUPPORTED_TYPES})" unless expected_type?
|
|
||||||
raise Error, "Actor #{uri} has moved to #{@json['movedTo']}" if break_on_redirect && @json['movedTo'].present?
|
|
||||||
|
|
||||||
@uri = @json['id']
|
|
||||||
@username = @json['preferredUsername']
|
|
||||||
@domain = Addressable::URI.parse(@uri).normalized_host
|
|
||||||
|
|
||||||
check_webfinger! unless only_key
|
|
||||||
|
|
||||||
ActivityPub::ProcessAccountService.new.call(@username, @domain, @json, only_key: only_key, verified_webfinger: !only_key)
|
|
||||||
rescue Error => e
|
|
||||||
Rails.logger.debug "Fetching account #{uri} failed: #{e.message}"
|
|
||||||
raise unless suppress_errors
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def check_webfinger!
|
|
||||||
webfinger = webfinger!("acct:#{@username}@#{@domain}")
|
|
||||||
confirmed_username, confirmed_domain = split_acct(webfinger.subject)
|
|
||||||
|
|
||||||
if @username.casecmp(confirmed_username).zero? && @domain.casecmp(confirmed_domain).zero?
|
|
||||||
raise Error, "Webfinger response for #{@username}@#{@domain} does not loop back to #{@uri}" if webfinger.link('self', 'href') != @uri
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
webfinger = webfinger!("acct:#{confirmed_username}@#{confirmed_domain}")
|
|
||||||
@username, @domain = split_acct(webfinger.subject)
|
|
||||||
|
|
||||||
unless confirmed_username.casecmp(@username).zero? && confirmed_domain.casecmp(@domain).zero?
|
|
||||||
raise Webfinger::RedirectError, "Too many webfinger redirects for URI #{uri} (stopped at #{@username}@#{@domain})"
|
|
||||||
end
|
|
||||||
|
|
||||||
raise Error, "Webfinger response for #{@username}@#{@domain} does not loop back to #{@uri}" if webfinger.link('self', 'href') != @uri
|
|
||||||
rescue Webfinger::RedirectError => e
|
|
||||||
raise Error, e.message
|
|
||||||
rescue Webfinger::Error => e
|
|
||||||
raise Error, "Webfinger error when resolving #{@username}@#{@domain}: #{e.message}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def split_acct(acct)
|
|
||||||
acct.gsub(/\Aacct:/, '').split('@')
|
|
||||||
end
|
|
||||||
|
|
||||||
def supported_context?
|
|
||||||
super(@json)
|
|
||||||
end
|
|
||||||
|
|
||||||
def expected_type?
|
Rails.logger.debug "Fetching account #{uri} failed: Expected Account, got #{actor.class.name}"
|
||||||
equals_or_includes_any?(@json['type'], SUPPORTED_TYPES)
|
raise Error, "Expected Account, got #{actor.class.name}" unless suppress_errors
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -0,0 +1,80 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class ActivityPub::FetchRemoteActorService < BaseService
|
||||||
|
include JsonLdHelper
|
||||||
|
include DomainControlHelper
|
||||||
|
include WebfingerHelper
|
||||||
|
|
||||||
|
class Error < StandardError; end
|
||||||
|
|
||||||
|
SUPPORTED_TYPES = %w(Application Group Organization Person Service).freeze
|
||||||
|
|
||||||
|
# Does a WebFinger roundtrip on each call, unless `only_key` is true
|
||||||
|
def call(uri, id: true, prefetched_body: nil, break_on_redirect: false, only_key: false, suppress_errors: true)
|
||||||
|
return if domain_not_allowed?(uri)
|
||||||
|
return ActivityPub::TagManager.instance.uri_to_actor(uri) if ActivityPub::TagManager.instance.local_uri?(uri)
|
||||||
|
|
||||||
|
@json = begin
|
||||||
|
if prefetched_body.nil?
|
||||||
|
fetch_resource(uri, id)
|
||||||
|
else
|
||||||
|
body_to_json(prefetched_body, compare_id: id ? uri : nil)
|
||||||
|
end
|
||||||
|
rescue Oj::ParseError
|
||||||
|
raise Error, "Error parsing JSON-LD document #{uri}"
|
||||||
|
end
|
||||||
|
|
||||||
|
raise Error, "Error fetching actor JSON at #{uri}" if @json.nil?
|
||||||
|
raise Error, "Unsupported JSON-LD context for document #{uri}" unless supported_context?
|
||||||
|
raise Error, "Unexpected object type for actor #{uri} (expected any of: #{SUPPORTED_TYPES})" unless expected_type?
|
||||||
|
raise Error, "Actor #{uri} has moved to #{@json['movedTo']}" if break_on_redirect && @json['movedTo'].present?
|
||||||
|
|
||||||
|
@uri = @json['id']
|
||||||
|
@username = @json['preferredUsername']
|
||||||
|
@domain = Addressable::URI.parse(@uri).normalized_host
|
||||||
|
|
||||||
|
check_webfinger! unless only_key
|
||||||
|
|
||||||
|
ActivityPub::ProcessAccountService.new.call(@username, @domain, @json, only_key: only_key, verified_webfinger: !only_key)
|
||||||
|
rescue Error => e
|
||||||
|
Rails.logger.debug "Fetching actor #{uri} failed: #{e.message}"
|
||||||
|
raise unless suppress_errors
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def check_webfinger!
|
||||||
|
webfinger = webfinger!("acct:#{@username}@#{@domain}")
|
||||||
|
confirmed_username, confirmed_domain = split_acct(webfinger.subject)
|
||||||
|
|
||||||
|
if @username.casecmp(confirmed_username).zero? && @domain.casecmp(confirmed_domain).zero?
|
||||||
|
raise Error, "Webfinger response for #{@username}@#{@domain} does not loop back to #{@uri}" if webfinger.link('self', 'href') != @uri
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
webfinger = webfinger!("acct:#{confirmed_username}@#{confirmed_domain}")
|
||||||
|
@username, @domain = split_acct(webfinger.subject)
|
||||||
|
|
||||||
|
unless confirmed_username.casecmp(@username).zero? && confirmed_domain.casecmp(@domain).zero?
|
||||||
|
raise Webfinger::RedirectError, "Too many webfinger redirects for URI #{uri} (stopped at #{@username}@#{@domain})"
|
||||||
|
end
|
||||||
|
|
||||||
|
raise Error, "Webfinger response for #{@username}@#{@domain} does not loop back to #{@uri}" if webfinger.link('self', 'href') != @uri
|
||||||
|
rescue Webfinger::RedirectError => e
|
||||||
|
raise Error, e.message
|
||||||
|
rescue Webfinger::Error => e
|
||||||
|
raise Error, "Webfinger error when resolving #{@username}@#{@domain}: #{e.message}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def split_acct(acct)
|
||||||
|
acct.gsub(/\Aacct:/, '').split('@')
|
||||||
|
end
|
||||||
|
|
||||||
|
def supported_context?
|
||||||
|
super(@json)
|
||||||
|
end
|
||||||
|
|
||||||
|
def expected_type?
|
||||||
|
equals_or_includes_any?(@json['type'], SUPPORTED_TYPES)
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,180 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe ActivityPub::FetchRemoteActorService, type: :service do
|
||||||
|
subject { ActivityPub::FetchRemoteActorService.new }
|
||||||
|
|
||||||
|
let!(:actor) do
|
||||||
|
{
|
||||||
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||||
|
id: 'https://example.com/alice',
|
||||||
|
type: 'Person',
|
||||||
|
preferredUsername: 'alice',
|
||||||
|
name: 'Alice',
|
||||||
|
summary: 'Foo bar',
|
||||||
|
inbox: 'http://example.com/alice/inbox',
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#call' do
|
||||||
|
let(:account) { subject.call('https://example.com/alice', id: true) }
|
||||||
|
|
||||||
|
shared_examples 'sets profile data' do
|
||||||
|
it 'returns an account' do
|
||||||
|
expect(account).to be_an Account
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sets display name' do
|
||||||
|
expect(account.display_name).to eq 'Alice'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sets note' do
|
||||||
|
expect(account.note).to eq 'Foo bar'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sets URL' do
|
||||||
|
expect(account.url).to eq 'https://example.com/alice'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the account does not have a inbox' do
|
||||||
|
let!(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice' }] } }
|
||||||
|
|
||||||
|
before do
|
||||||
|
actor[:inbox] = nil
|
||||||
|
|
||||||
|
stub_request(:get, 'https://example.com/alice').to_return(body: Oj.dump(actor))
|
||||||
|
stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'fetches resource' do
|
||||||
|
account
|
||||||
|
expect(a_request(:get, 'https://example.com/alice')).to have_been_made.once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'looks up webfinger' do
|
||||||
|
account
|
||||||
|
expect(a_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')).to have_been_made.once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns nil' do
|
||||||
|
expect(account).to be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when URI and WebFinger share the same host' do
|
||||||
|
let!(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/alice' }] } }
|
||||||
|
|
||||||
|
before do
|
||||||
|
stub_request(:get, 'https://example.com/alice').to_return(body: Oj.dump(actor))
|
||||||
|
stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'fetches resource' do
|
||||||
|
account
|
||||||
|
expect(a_request(:get, 'https://example.com/alice')).to have_been_made.once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'looks up webfinger' do
|
||||||
|
account
|
||||||
|
expect(a_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')).to have_been_made.once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sets username and domain from webfinger' do
|
||||||
|
expect(account.username).to eq 'alice'
|
||||||
|
expect(account.domain).to eq 'example.com'
|
||||||
|
end
|
||||||
|
|
||||||
|
include_examples 'sets profile data'
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when WebFinger presents different domain than URI' do
|
||||||
|
let!(:webfinger) { { subject: 'acct:alice@iscool.af', links: [{ rel: 'self', href: 'https://example.com/alice' }] } }
|
||||||
|
|
||||||
|
before do
|
||||||
|
stub_request(:get, 'https://example.com/alice').to_return(body: Oj.dump(actor))
|
||||||
|
stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
|
||||||
|
stub_request(:get, 'https://iscool.af/.well-known/webfinger?resource=acct:alice@iscool.af').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'fetches resource' do
|
||||||
|
account
|
||||||
|
expect(a_request(:get, 'https://example.com/alice')).to have_been_made.once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'looks up webfinger' do
|
||||||
|
account
|
||||||
|
expect(a_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')).to have_been_made.once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'looks up "redirected" webfinger' do
|
||||||
|
account
|
||||||
|
expect(a_request(:get, 'https://iscool.af/.well-known/webfinger?resource=acct:alice@iscool.af')).to have_been_made.once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'sets username and domain from final webfinger' do
|
||||||
|
expect(account.username).to eq 'alice'
|
||||||
|
expect(account.domain).to eq 'iscool.af'
|
||||||
|
end
|
||||||
|
|
||||||
|
include_examples 'sets profile data'
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when WebFinger returns a different URI' do
|
||||||
|
let!(:webfinger) { { subject: 'acct:alice@example.com', links: [{ rel: 'self', href: 'https://example.com/bob' }] } }
|
||||||
|
|
||||||
|
before do
|
||||||
|
stub_request(:get, 'https://example.com/alice').to_return(body: Oj.dump(actor))
|
||||||
|
stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'fetches resource' do
|
||||||
|
account
|
||||||
|
expect(a_request(:get, 'https://example.com/alice')).to have_been_made.once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'looks up webfinger' do
|
||||||
|
account
|
||||||
|
expect(a_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')).to have_been_made.once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not create account' do
|
||||||
|
expect(account).to be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when WebFinger returns a different URI after a redirection' do
|
||||||
|
let!(:webfinger) { { subject: 'acct:alice@iscool.af', links: [{ rel: 'self', href: 'https://example.com/bob' }] } }
|
||||||
|
|
||||||
|
before do
|
||||||
|
stub_request(:get, 'https://example.com/alice').to_return(body: Oj.dump(actor))
|
||||||
|
stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
|
||||||
|
stub_request(:get, 'https://iscool.af/.well-known/webfinger?resource=acct:alice@iscool.af').to_return(body: Oj.dump(webfinger), headers: { 'Content-Type': 'application/jrd+json' })
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'fetches resource' do
|
||||||
|
account
|
||||||
|
expect(a_request(:get, 'https://example.com/alice')).to have_been_made.once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'looks up webfinger' do
|
||||||
|
account
|
||||||
|
expect(a_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:alice@example.com')).to have_been_made.once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'looks up "redirected" webfinger' do
|
||||||
|
account
|
||||||
|
expect(a_request(:get, 'https://iscool.af/.well-known/webfinger?resource=acct:alice@iscool.af')).to have_been_made.once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not create account' do
|
||||||
|
expect(account).to be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with wrong id' do
|
||||||
|
it 'does not create account' do
|
||||||
|
expect(subject.call('https://fake.address/@foo', prefetched_body: Oj.dump(actor))).to be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in new issue