A platform for community discussion. Free, open, simple.
FIX: Handle addressable error when parsing an invalid URL. (#15836)
Alan Guo Xiang Tanb7eacaed21d7
diff --git a/app/models/embeddable_host.rb b/app/models/embeddable_host.rbindex 212348bc3d1..6dd77cbb2bd 100644--- a/app/models/embeddable_host.rb+++ b/app/models/embeddable_host.rb@@ -14,13 +14,13 @@ class EmbeddableHost < ActiveRecord::Baseself.ignored_columns = ["path_whitelist"]def self.record_for_url(uri)-if uri.is_a?(String)uri = beginURI(UrlHelper.escape_uri(uri))- rescue URI::Error+ rescue URI::Error, Addressable::URI::InvalidURIErrorendend+return false unless uri.present?host = uri.hostdiff --git a/spec/models/embeddable_host_spec.rb b/spec/models/embeddable_host_spec.rbindex 2a76688553c..c9306f63480 100644--- a/spec/models/embeddable_host_spec.rb+++ b/spec/models/embeddable_host_spec.rb@@ -3,7 +3,6 @@require 'rails_helper'describe EmbeddableHost do-it "trims http" doeh = EmbeddableHost.new(host: 'http://example.com')expect(eh).to be_valid@@ -149,4 +148,16 @@ describe EmbeddableHost doexpect(SiteSetting.embed_post_limit).to eq(SiteSetting.defaults[:embed_post_limit])endend++ describe '.record_for_url' do+ fab!(:embeddable_host) { Fabricate(:embeddable_host) }++ it 'returns the right record if given URL matches host' do+ expect(EmbeddableHost.record_for_url("https://#{embeddable_host.host}")).to eq(embeddable_host)+ end++ it 'returns false if URL is malformed' do+ expect(EmbeddableHost.record_for_url("@@@@@")).to eq(false)+ end+ endend