Skip to content

Commit

Permalink
Handle links with no href in VerifyLinkService (mastodon#20741)
Browse files Browse the repository at this point in the history
Before this change, the following error would cause VerifyAccountLinksWorker to fail:

NoMethodError: undefined method `downcase' for nil:NilClass
  [PROJECT_ROOT]/app/services/verify_link_service.rb:31 :in `block in link_back_present?`
  • Loading branch information
joshuap authored Nov 17, 2022
1 parent cbb0153 commit daf6f34
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/services/verify_link_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def link_back_present?

links = Nokogiri::HTML(@body).xpath('//a[contains(concat(" ", normalize-space(@rel), " "), " me ")]|//link[contains(concat(" ", normalize-space(@rel), " "), " me ")]')

if links.any? { |link| link['href'].downcase == @link_back.downcase }
if links.any? { |link| link['href']&.downcase == @link_back.downcase }
true
elsif links.empty?
false
Expand All @@ -38,6 +38,8 @@ def link_back_present?
end

def link_redirects_back?(test_url)
return false if test_url.blank?

redirect_to_url = Request.new(:head, test_url, follow: false).perform do |res|
res.headers['Location']
end
Expand Down
20 changes: 19 additions & 1 deletion spec/services/verify_link_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,25 @@
context 'when a link does not contain a link back' do
let(:html) { '' }

it 'marks the field as verified' do
it 'does not mark the field as verified' do
expect(field.verified?).to be false
end
end

context 'when link has no `href` attribute' do
let(:html) do
<<-HTML
<!doctype html>
<head>
<link type="text/html" rel="me" />
</head>
<body>
<a rel="me" target="_blank">Follow me on Mastodon</a>
</body>
HTML
end

it 'does not mark the field as verified' do
expect(field.verified?).to be false
end
end
Expand Down

0 comments on commit daf6f34

Please sign in to comment.