From 99fd65328cd845f1e217de42e33f7f690a70dc99 Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Fri, 7 Feb 2020 10:54:24 -0500 Subject: [PATCH] FIX: Skip absolutizing URLs when source URI is invalid --- app/models/topic_embed.rb | 6 +++++- spec/models/topic_embed_spec.rb | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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 hello" } + + 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