FIX: Handle malformed URLs in `TopicEmbed.absolutize_urls`.

This commit is contained in:
Alan Guo Xiang Tan 2022-01-21 10:03:49 +08:00
parent d28808e866
commit e4e37257cc
2 changed files with 27 additions and 5 deletions

View File

@ -208,14 +208,24 @@ class TopicEmbed < ActiveRecord::Base
fragment = Nokogiri::HTML5.fragment("<div>#{contents}</div>")
fragment.css('a').each do |a|
if a['href'].present?
a['href'] = URI.join(prefix, a['href']).to_s
begin
a['href'] = URI.join(prefix, a['href']).to_s
rescue URI::InvalidURIError
# NOOP, URL is malformed
end
end
end
fragment.css('img').each do |a|
if a['src'].present?
a['src'] = URI.join(prefix, a['src']).to_s
begin
a['src'] = URI.join(prefix, a['src']).to_s
rescue URI::InvalidURIError
# NOOP, URL is malformed
end
end
end
fragment.at('div').inner_html
end

View File

@ -379,13 +379,25 @@ describe TopicEmbed do
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 "handles badly formed URIs" do
invalid_url = 'http://source.com/#double#anchor'
contents = "hello world new post <a href='/hello'>hello</a>"
raw = TopicEmbed.absolutize_urls(invalid_url, contents)
expect(raw).to eq("hello world new post <a href=\"http://source.com/hello\">hello</a>")
end
it "handles malformed links" do
url = "https://somesource.com"
contents = <<~CONTENT
hello world new post <a href="mailto:somemail@somewhere.org>">hello</a>
some image <img src="https:/><invalidimagesrc/">
CONTENT
raw = TopicEmbed.absolutize_urls(url, contents)
expect(raw).to eq(contents)
end
end
end