Skip to content

Commit

Permalink
Fix acct URIs with IDN domains not being resolved (mastodon#11520)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron authored and hiyuki2578 committed Oct 2, 2019
1 parent da03ed3 commit 7655cb6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
1 change: 0 additions & 1 deletion app/controllers/remote_interaction_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def set_status
@status = Status.find(params[:id])
authorize @status, :show?
rescue Mastodon::NotPermittedError
# Reraise in order to get a 404
raise ActiveRecord::RecordNotFound
end

Expand Down
36 changes: 30 additions & 6 deletions app/models/remote_follow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

class RemoteFollow
include ActiveModel::Validations
include RoutingHelper

attr_accessor :acct, :addressable_template

validates :acct, presence: true

def initialize(attrs = nil)
@acct = attrs[:acct].gsub(/\A@/, '').strip if !attrs.nil? && !attrs[:acct].nil?
def initialize(attrs = {})
@acct = normalize_acct(attrs[:acct])
end

def valid?
return false unless super

populate_template
fetch_template!

errors.empty?
end

Expand All @@ -28,8 +30,30 @@ def interact_address_for(status)

private

def populate_template
if acct.blank? || redirect_url_link.nil? || redirect_url_link.template.nil?
def normalize_acct(value)
return if value.blank?

username, domain = value.strip.gsub(/\A@/, '').split('@')

domain = begin
if TagManager.instance.local_domain?(domain)
nil
else
TagManager.instance.normalize_domain(domain)
end
end

[username, domain].compact.join('@')
end

def fetch_template!
return missing_resource if acct.blank?

_, domain = acct.split('@')

if domain.nil?
@addressable_template = Addressable::Template.new("#{authorize_interaction_url}?uri={uri}")
elsif redirect_url_link.nil? || redirect_url_link.template.nil?
missing_resource_error
else
@addressable_template = Addressable::Template.new(redirect_uri_template)
Expand All @@ -45,7 +69,7 @@ def redirect_url_link
end

def acct_resource
@_acct_resource ||= Goldfinger.finger("acct:#{acct}")
@acct_resource ||= Goldfinger.finger("acct:#{acct}")
rescue Goldfinger::Error, HTTP::ConnectionError
nil
end
Expand Down
14 changes: 10 additions & 4 deletions app/services/resolve_account_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,23 @@ def process_options!(uri, options)
@account = uri
@username = @account.username
@domain = @account.domain
@uri = [@username, @domain].compact.join('@')
else
@uri = uri
@username, @domain = uri.split('@')
end

@domain = nil if TagManager.instance.local_domain?(@domain)
@domain = begin
if TagManager.instance.local_domain?(@domain)
nil
else
TagManager.instance.normalize_domain(@domain)
end
end

@uri = [@username, @domain].compact.join('@')
end

def process_webfinger!(uri, redirected = false)
@webfinger = Goldfinger.finger("acct:#{@uri}")
@webfinger = Goldfinger.finger("acct:#{uri}")
confirmed_username, confirmed_domain = @webfinger.subject.gsub(/\Aacct:/, '').split('@')

if confirmed_username.casecmp(@username).zero? && confirmed_domain.casecmp(@domain).zero?
Expand Down

0 comments on commit 7655cb6

Please sign in to comment.