Skip to content

A platform for community discussion. Free, open, simple.

FIX: Skip absolutizing URLs when source URI is invalid

Penar Musaraj99fd65328cd8
diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb
index 12ff86a646f..b71726d4134 100644
--- a/app/models/topic_embed.rb
+++ b/app/models/topic_embed.rb
@@ -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
diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rb
index 1ff69046c0e..4e931f4f99c 100644
--- a/spec/models/topic_embed_spec.rb
+++ b/spec/models/topic_embed_spec.rb
@@ -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