FIX: Skip absolutizing URLs when source URI is invalid

This commit is contained in:
Penar Musaraj 2020-02-07 10:54:24 -05:00
parent 88a4d5a2c1
commit 99fd65328c
2 changed files with 15 additions and 1 deletions

View File

@ -186,7 +186,11 @@ class TopicEmbed < ActiveRecord::Base
# Convert any relative URLs to absolute. RSS is annoying for this.
def self.absolutize_urls(url, contents)
url = normalize_url(url)
uri = URI(UrlHelper.escape_uri(url))
begin
uri = URI(UrlHelper.escape_uri(url))
rescue URI::Error
return contents
end
prefix = "#{uri.scheme}://#{uri.host}"
prefix << ":#{uri.port}" if uri.port != 80 && uri.port != 443

View File

@ -310,4 +310,14 @@ describe TopicEmbed do
end
end
describe '.absolutize_urls' do
let(:invalid_url) { 'http://source.com/#double#anchor' }
let(:contents) { "hello world new post <a href='/hello'>hello</a>" }
it "does not attempt absolutizing on a bad URI" do
raw = TopicEmbed.absolutize_urls(invalid_url, contents)
expect(raw).to eq(contents)
end
end
end