Skip to content

Commit

Permalink
Add size limit for link preview URLs (mastodon#30854)
Browse files Browse the repository at this point in the history
  • Loading branch information
oneiros authored and AgathaSorceress committed Dec 22, 2024
1 parent 3cfb504 commit 4679f79
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/services/fetch_link_card_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class FetchLinkCardService < BaseService
)
}iox

# URL size limit to safely store in PosgreSQL's unique indexes
BYTESIZE_LIMIT = 2692

def call(status)
@status = status
@original_url = parse_urls
Expand Down Expand Up @@ -85,7 +88,7 @@ def parse_urls

def bad_url?(uri)
# Avoid local instance URLs and invalid URLs
uri.host.blank? || TagManager.instance.local_url?(uri.to_s) || !%w(http https).include?(uri.scheme)
uri.host.blank? || TagManager.instance.local_url?(uri.to_s) || !%w(http https).include?(uri.scheme) || uri.to_s.bytesize > BYTESIZE_LIMIT
end

def mention_link?(anchor)
Expand Down
13 changes: 13 additions & 0 deletions spec/services/fetch_link_card_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@
end
end

context 'with an URL too long for PostgreSQL unique indexes' do
let(:url) { "http://example.com/#{'a' * 2674}" }
let(:status) { Fabricate(:status, text: url) }

it 'does not fetch the URL' do
expect(a_request(:get, url)).to_not have_been_made
end

it 'does not create a preview card' do
expect(status.preview_card).to be_nil
end
end

context 'with a URL of a page with oEmbed support' do
let(:html) { '<!doctype html><title>Hello world</title><link rel="alternate" type="application/json+oembed" href="http://example.com/oembed?url=http://example.com/html">' }
let(:status) { Fabricate(:status, text: 'http://example.com/html') }
Expand Down

0 comments on commit 4679f79

Please sign in to comment.