diff --git a/app/models/embeddable_host.rb b/app/models/embeddable_host.rb index edc7d7f6b06..bc7f2f3e999 100644 --- a/app/models/embeddable_host.rb +++ b/app/models/embeddable_host.rb @@ -10,13 +10,17 @@ class EmbeddableHost < ActiveRecord::Base def self.record_for_url(uri) if uri.is_a?(String) - uri = URI(uri) rescue nil + uri = URI(URI.encode(uri)) rescue nil end return false unless uri.present? host = uri.host return false unless host.present? + if uri.port.present? && uri.port != 80 && uri.port != 443 + host << ":#{uri.port}" + end + path = uri.path path << "?" << uri.query if uri.query.present? @@ -28,7 +32,7 @@ class EmbeddableHost < ActiveRecord::Base end def self.url_allowed?(url) - uri = URI(url) rescue nil + uri = URI(URI.encode(url)) rescue nil uri.present? && record_for_url(uri).present? end diff --git a/spec/models/embeddable_host_spec.rb b/spec/models/embeddable_host_spec.rb index 292ae7fe7a4..fe731328513 100644 --- a/spec/models/embeddable_host_spec.rb +++ b/spec/models/embeddable_host_spec.rb @@ -49,12 +49,21 @@ describe EmbeddableHost do expect(eh).not_to be_valid end + describe "it works with ports" do + let!(:host) { Fabricate(:embeddable_host, host: 'localhost:8000') } + + it "works as expected" do + expect(EmbeddableHost.url_allowed?('http://localhost:8000/eviltrout')).to eq(true) + end + end + describe "url_allowed?" do let!(:host) { Fabricate(:embeddable_host) } it 'works as expected' do expect(EmbeddableHost.url_allowed?('http://eviltrout.com')).to eq(true) expect(EmbeddableHost.url_allowed?('https://eviltrout.com')).to eq(true) + expect(EmbeddableHost.url_allowed?('https://eviltrout.com/انگلیسی')).to eq(true) expect(EmbeddableHost.url_allowed?('https://not-eviltrout.com')).to eq(false) end