mirror of
https://github.com/discourse/discourse.git
synced 2025-03-09 14:34:35 +00:00
FIX: Handle malformed URLs in TopicEmbed.absolutize_urls
.
This commit is contained in:
parent
d28808e866
commit
e4e37257cc
@ -208,14 +208,24 @@ class TopicEmbed < ActiveRecord::Base
|
|||||||
fragment = Nokogiri::HTML5.fragment("<div>#{contents}</div>")
|
fragment = Nokogiri::HTML5.fragment("<div>#{contents}</div>")
|
||||||
fragment.css('a').each do |a|
|
fragment.css('a').each do |a|
|
||||||
if a['href'].present?
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
fragment.css('img').each do |a|
|
fragment.css('img').each do |a|
|
||||||
if a['src'].present?
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
fragment.at('div').inner_html
|
fragment.at('div').inner_html
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -379,13 +379,25 @@ describe TopicEmbed do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe '.absolutize_urls' do
|
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
|
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)
|
raw = TopicEmbed.absolutize_urls(invalid_url, contents)
|
||||||
expect(raw).to eq("hello world new post <a href=\"http://source.com/hello\">hello</a>")
|
expect(raw).to eq("hello world new post <a href=\"http://source.com/hello\">hello</a>")
|
||||||
end
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user