Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Fix mastodon#2706 - Always respond with 200 to PuSH payloads (mastodo…
Browse files Browse the repository at this point in the history
…n#2733)

Fix mastodon#2196 - Respond with 201 when Salmon accepted, 400 when unverified
Fix mastodon#2629 - Correctly handle confirm_domain? for local accounts
Unify rules for extracting author acct from XML, prefer <email>, fall back
to <name> + <uri> (see also mastodon#2017, mastodon#2172)
  • Loading branch information
Gargron authored May 3, 2017
1 parent dd9d573 commit bafd22e
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 85 deletions.
14 changes: 9 additions & 5 deletions app/controllers/api/salmon_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ class Api::SalmonController < ApiController
respond_to :txt

def update
body = request.body.read
payload = request.body.read

if body.nil?
head 200
else
SalmonWorker.perform_async(@account.id, body.force_encoding('UTF-8'))
if !payload.nil? && verify?(payload)
SalmonWorker.perform_async(@account.id, payload.force_encoding('UTF-8'))
head 201
else
head 202
end
end

Expand All @@ -20,4 +20,8 @@ def update
def set_account
@account = Account.find(params[:id])
end

def verify?(payload)
VerifySalmonService.new.call(payload)
end
end
5 changes: 2 additions & 3 deletions app/controllers/api/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ def update

if subscription.verify(body, request.headers['HTTP_X_HUB_SIGNATURE'])
ProcessingWorker.perform_async(@account.id, body.force_encoding('UTF-8'))
head 201
else
head 202
end

head 200
end

private
Expand Down
21 changes: 21 additions & 0 deletions app/services/concerns/author_extractor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module AuthorExtractor
def author_from_xml(xml)
# Try <email> for acct
acct = xml.at_xpath('./xmlns:author/xmlns:email', xmlns: TagManager::XMLNS)&.content

# Try <name> + <uri>
if acct.blank?
username = xml.at_xpath('./xmlns:author/xmlns:name', xmlns: TagManager::XMLNS)&.content
uri = xml.at_xpath('./xmlns:author/xmlns:uri', xmlns: TagManager::XMLNS)&.content

return nil if username.blank? || uri.blank?

domain = Addressable::URI.parse(uri).normalize.host
acct = "#{username}@#{domain}"
end

FollowRemoteAccountService.new.call(acct)
end
end
17 changes: 4 additions & 13 deletions app/services/fetch_remote_account_service.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class FetchRemoteAccountService < BaseService
include AuthorExtractor

def call(url, prefetched_body = nil)
if prefetched_body.nil?
atom_url, body = FetchAtomService.new.call(url)
Expand All @@ -19,21 +21,10 @@ def process_atom(url, body)
xml = Nokogiri::XML(body)
xml.encoding = 'utf-8'

email = xml.at_xpath('//xmlns:author/xmlns:email').try(:content)
if email.nil?
url_parts = Addressable::URI.parse(url).normalize
username = xml.at_xpath('//xmlns:author/xmlns:name').try(:content)
domain = url_parts.host
else
username, domain = email.split('@')
end

return nil if username.nil? || domain.nil?

Rails.logger.debug "Going to webfinger #{username}@#{domain}"
account = author_from_xml(xml.at_xpath('/xmlns:feed', xmlns: TagManager::XMLNS))

account = FollowRemoteAccountService.new.call("#{username}@#{domain}")
UpdateRemoteProfileService.new.call(xml, account) unless account.nil?

account
rescue TypeError
Rails.logger.debug "Unparseable URL given: #{url}"
Expand Down
28 changes: 6 additions & 22 deletions app/services/fetch_remote_status_service.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class FetchRemoteStatusService < BaseService
include AuthorExtractor

def call(url, prefetched_body = nil)
if prefetched_body.nil?
atom_url, body = FetchAtomService.new.call(url)
Expand All @@ -21,37 +23,19 @@ def process_atom(url, body)
xml = Nokogiri::XML(body)
xml.encoding = 'utf-8'

account = extract_author(url, xml)
account = author_from_xml(xml.at_xpath('/xmlns:entry', xmlns: TagManager::XMLNS))
domain = Addressable::URI.parse(url).normalize.host

return nil if account.nil?
return nil unless !account.nil? && confirmed_domain?(domain, account)

statuses = ProcessFeedService.new.call(body, account)

statuses.first
end

def extract_author(url, xml)
url_parts = Addressable::URI.parse(url).normalize
username = xml.at_xpath('//xmlns:author/xmlns:name').try(:content)
domain = url_parts.host

return nil if username.nil?

Rails.logger.debug "Going to webfinger #{username}@#{domain}"

account = FollowRemoteAccountService.new.call("#{username}@#{domain}")

# If the author's confirmed URLs do not match the domain of the URL
# we are reading this from, abort
return nil unless confirmed_domain?(domain, account)

account
rescue Nokogiri::XML::XPath::SyntaxError
Rails.logger.debug 'Invalid XML or missing namespace'
nil
end

def confirmed_domain?(domain, account)
domain.casecmp(account.domain).zero? || domain.casecmp(Addressable::URI.parse(account.remote_url).normalize.host).zero?
account.domain.nil? || domain.casecmp(account.domain).zero? || domain.casecmp(Addressable::URI.parse(account.remote_url).normalize.host).zero?
end
end
16 changes: 3 additions & 13 deletions app/services/process_feed_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def process_entries(xml, account)
end

class ProcessEntry
include AuthorExtractor

def call(xml, account)
@account = account
@xml = xml
Expand Down Expand Up @@ -108,7 +110,7 @@ def status_from_xml(entry)
# If that author cannot be found, don't record the status (do not misattribute)
if account?(entry)
begin
account = find_or_resolve_account(acct(entry))
account = author_from_xml(entry)
return [nil, false] if account.nil?
rescue Goldfinger::Error
return [nil, false]
Expand Down Expand Up @@ -143,10 +145,6 @@ def status_from_xml(entry)
[status, true]
end

def find_or_resolve_account(acct)
FollowRemoteAccountService.new.call(acct)
end

def find_or_resolve_status(parent, uri, url)
status = find_status(uri)

Expand Down Expand Up @@ -275,13 +273,5 @@ def thread(xml = @xml)
def account?(xml = @xml)
!xml.at_xpath('./xmlns:author', xmlns: TagManager::XMLNS).nil?
end

def acct(xml = @xml)
username = xml.at_xpath('./xmlns:author/xmlns:name', xmlns: TagManager::XMLNS).content
url = xml.at_xpath('./xmlns:author/xmlns:uri', xmlns: TagManager::XMLNS).content
domain = Addressable::URI.parse(url).normalize.host

"#{username}@#{domain}"
end
end
end
31 changes: 4 additions & 27 deletions app/services/process_interaction_service.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class ProcessInteractionService < BaseService
include AuthorExtractor

# Record locally the remote interaction with our user
# @param [String] envelope Salmon envelope
# @param [Account] target_account Account the Salmon was addressed to
Expand All @@ -10,18 +12,9 @@ def call(envelope, target_account)
xml = Nokogiri::XML(body)
xml.encoding = 'utf-8'

return unless contains_author?(xml)

username = xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:name', xmlns: TagManager::XMLNS).content
url = xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:uri', xmlns: TagManager::XMLNS).content
domain = Addressable::URI.parse(url).normalize.host
account = Account.find_by(username: username, domain: domain)

if account.nil?
account = follow_remote_account_service.call("#{username}@#{domain}")
end
account = author_from_xml(xml.at_xpath('/xmlns:entry', xmlns: TagManager::XMLNS))

return if account.suspended?
return if account.nil? || account.suspended?

if salmon.verify(envelope, account.keypair)
RemoteProfileUpdateWorker.perform_async(account.id, body.force_encoding('UTF-8'), true)
Expand Down Expand Up @@ -59,10 +52,6 @@ def call(envelope, target_account)

private

def contains_author?(xml)
!(xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:name', xmlns: TagManager::XMLNS).nil? || xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:uri', xmlns: TagManager::XMLNS).nil?)
end

def mentions_account?(xml, account)
xml.xpath('/xmlns:entry/xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each { |mention_link| return true if [TagManager.instance.uri_for(account), TagManager.instance.url_for(account)].include?(mention_link.attribute('href').value) }
false
Expand Down Expand Up @@ -144,16 +133,4 @@ def activity_id(xml)
def salmon
@salmon ||= OStatus2::Salmon.new
end

def follow_remote_account_service
@follow_remote_account_service ||= FollowRemoteAccountService.new
end

def process_feed_service
@process_feed_service ||= ProcessFeedService.new
end

def remove_status_service
@remove_status_service ||= RemoveStatusService.new
end
end
26 changes: 26 additions & 0 deletions app/services/verify_salmon_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class VerifySalmonService < BaseService
include AuthorExtractor

def call(payload)
body = salmon.unpack(payload)

xml = Nokogiri::XML(body)
xml.encoding = 'utf-8'

account = author_from_xml(xml.at_xpath('/xmlns:entry', xmlns: TagManager::XMLNS))

if account.nil?
false
else
salmon.verify(payload, account.keypair)
end
end

private

def salmon
@salmon ||= OStatus2::Salmon.new
end
end
4 changes: 2 additions & 2 deletions spec/controllers/api/subscriptions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
post :update, params: { id: account.id }
end

it 'returns http created' do
expect(response).to have_http_status(:created)
it 'returns http success' do
expect(response).to have_http_status(:success)
end

it 'creates statuses for feed' do
Expand Down

0 comments on commit bafd22e

Please sign in to comment.