From ed818a4a19f4fcf8636b3f2bfce37df56d1aa45c Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Fri, 30 Apr 2021 11:10:19 +0200 Subject: [PATCH] FIX: prevents malformed href to crash TopicEmbed (#12910) If the associated page of a remote url passed to `TopicEmber.new(remote_url)` contained a malformed link like: `Baz` it would raise an uncaught exception: ``` Job exception: Invalid scheme format: (http ``` --- app/models/topic_embed.rb | 2 +- spec/models/topic_embed_spec.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb index e86a5bd1fa5..d3ed0f1a311 100644 --- a/app/models/topic_embed.rb +++ b/app/models/topic_embed.rb @@ -165,7 +165,7 @@ class TopicEmbed < ActiveRecord::Base uri.host = original_uri.host node[url_param] = uri.to_s end - rescue URI::Error + rescue URI::Error, Addressable::URI::InvalidURIError # If there is a mistyped URL, just do nothing end end diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rb index 9cf293a9c83..d73135a333a 100644 --- a/spec/models/topic_embed_spec.rb +++ b/spec/models/topic_embed_spec.rb @@ -343,6 +343,22 @@ describe TopicEmbed do expect(response.body).to have_tag('a', with: { href: 'mailto:bar@example.com' }) end end + + context "malformed href" do + let(:url) { 'http://example.com/foo' } + let(:contents) { '

Baz

' } + let!(:file) { StringIO.new } + + before do + file.stubs(:read).returns contents + TopicEmbed.stubs(:open).returns file + end + + it "doesn’t raise an exception" do + stub_request(:head, url) + expect { TopicEmbed.find_remote(url) }.not_to raise_error + end + end end describe '.absolutize_urls' do