diff --git a/app/models/embeddable_host.rb b/app/models/embeddable_host.rb index d831a85666f..edc7d7f6b06 100644 --- a/app/models/embeddable_host.rb +++ b/app/models/embeddable_host.rb @@ -17,20 +17,19 @@ class EmbeddableHost < ActiveRecord::Base host = uri.host return false unless host.present? - where("lower(host) = ?", host).first + path = uri.path + path << "?" << uri.query if uri.query.present? + + where("lower(host) = ?", host).each do |eh| + return eh if eh.path_whitelist.blank? || !Regexp.new(eh.path_whitelist).match(path).nil? + end + + nil end def self.url_allowed?(url) uri = URI(url) rescue nil - return false unless uri.present? - - path = uri.path - path << "?" << uri.query if uri.query.present? - - host = record_for_url(uri) - - return host.present? && - (host.path_whitelist.blank? || !Regexp.new(host.path_whitelist).match(path).nil?) + uri.present? && record_for_url(uri).present? end private diff --git a/spec/models/embeddable_host_spec.rb b/spec/models/embeddable_host_spec.rb index c8a47755de3..292ae7fe7a4 100644 --- a/spec/models/embeddable_host_spec.rb +++ b/spec/models/embeddable_host_spec.rb @@ -77,6 +77,13 @@ describe EmbeddableHost do expect(EmbeddableHost.url_allowed?('http://eviltrout.com/fp?test=1')).to eq(false) expect(EmbeddableHost.url_allowed?('http://eviltrout.com/fp')).to eq(true) end + + it "allows multiple records with different paths" do + Fabricate(:embeddable_host, path_whitelist: '/rick/.*') + Fabricate(:embeddable_host, path_whitelist: '/morty/.*') + expect(EmbeddableHost.url_allowed?('http://eviltrout.com/rick/smith')).to eq(true) + expect(EmbeddableHost.url_allowed?('http://eviltrout.com/morty/sanchez')).to eq(true) + end end end