Handle relative URLs when fetching OEmbed/OpenGraph cards (#8669)

pull/724/head
ThibG 2018-09-10 18:26:28 +02:00 committed by Eugen Rochko
parent 25dd523887
commit bd9e47e9be
2 changed files with 11 additions and 9 deletions

View File

@ -87,34 +87,36 @@ class FetchLinkCardService < BaseService
end end
def attempt_oembed def attempt_oembed
embed = FetchOEmbedService.new.call(@url, html: @html) service = FetchOEmbedService.new
embed = service.call(@url, html: @html)
url = Addressable::URI.parse(service.endpoint_url)
return false if embed.nil? return false if embed.nil?
@card.type = embed[:type] @card.type = embed[:type]
@card.title = embed[:title] || '' @card.title = embed[:title] || ''
@card.author_name = embed[:author_name] || '' @card.author_name = embed[:author_name] || ''
@card.author_url = embed[:author_url] || '' @card.author_url = embed[:author_url].present? ? (url + embed[:author_url]).to_s : ''
@card.provider_name = embed[:provider_name] || '' @card.provider_name = embed[:provider_name] || ''
@card.provider_url = embed[:provider_url] || '' @card.provider_url = embed[:provider_url].present? ? (url + embed[:provider_url]).to_s : ''
@card.width = 0 @card.width = 0
@card.height = 0 @card.height = 0
case @card.type case @card.type
when 'link' when 'link'
@card.image_remote_url = embed[:thumbnail_url] if embed[:thumbnail_url].present? @card.image_remote_url = (url + embed[:thumbnail_url]).to_s if embed[:thumbnail_url].present?
when 'photo' when 'photo'
return false if embed[:url].blank? return false if embed[:url].blank?
@card.embed_url = embed[:url] @card.embed_url = (url + embed[:url]).to_s
@card.image_remote_url = embed[:url] @card.image_remote_url = (url + embed[:url]).to_s
@card.width = embed[:width].presence || 0 @card.width = embed[:width].presence || 0
@card.height = embed[:height].presence || 0 @card.height = embed[:height].presence || 0
when 'video' when 'video'
@card.width = embed[:width].presence || 0 @card.width = embed[:width].presence || 0
@card.height = embed[:height].presence || 0 @card.height = embed[:height].presence || 0
@card.html = Formatter.instance.sanitize(embed[:html], Sanitize::Config::MASTODON_OEMBED) @card.html = Formatter.instance.sanitize(embed[:html], Sanitize::Config::MASTODON_OEMBED)
@card.image_remote_url = embed[:thumbnail_url] if embed[:thumbnail_url].present? @card.image_remote_url = (url + embed[:thumbnail_url]).to_s if embed[:thumbnail_url].present?
when 'rich' when 'rich'
# Most providers rely on <script> tags, which is a no-no # Most providers rely on <script> tags, which is a no-no
return false return false
@ -146,7 +148,7 @@ class FetchLinkCardService < BaseService
@card.title = meta_property(page, 'og:title').presence || page.at_xpath('//title')&.content || '' @card.title = meta_property(page, 'og:title').presence || page.at_xpath('//title')&.content || ''
@card.description = meta_property(page, 'og:description').presence || meta_property(page, 'description') || '' @card.description = meta_property(page, 'og:description').presence || meta_property(page, 'description') || ''
@card.image_remote_url = meta_property(page, 'og:image') if meta_property(page, 'og:image') @card.image_remote_url = (Addressable::URI.parse(@url) + meta_property(page, 'og:image')).to_s if meta_property(page, 'og:image')
return if @card.title.blank? && @card.html.blank? return if @card.title.blank? && @card.html.blank?

View File

@ -31,7 +31,7 @@ class FetchOEmbedService
return if @endpoint_url.blank? return if @endpoint_url.blank?
@endpoint_url = Addressable::URI.parse(@endpoint_url).to_s @endpoint_url = (Addressable::URI.parse(@url) + @endpoint_url).to_s
rescue Addressable::URI::InvalidURIError rescue Addressable::URI::InvalidURIError
@endpoint_url = nil @endpoint_url = nil
end end