2015-08-18 16:15:46 -05:00
|
|
|
class EmbeddableHost < ActiveRecord::Base
|
2016-01-11 10:06:09 -06:00
|
|
|
validate :host_must_be_valid
|
2015-08-18 16:15:46 -05:00
|
|
|
belongs_to :category
|
|
|
|
|
|
|
|
before_validation do
|
|
|
|
self.host.sub!(/^https?:\/\//, '')
|
|
|
|
self.host.sub!(/\/.*$/, '')
|
|
|
|
end
|
|
|
|
|
2016-08-23 13:55:52 -05:00
|
|
|
def self.record_for_url(uri)
|
|
|
|
|
|
|
|
if uri.is_a?(String)
|
2017-02-27 11:17:52 -06:00
|
|
|
uri = URI(URI.encode(uri)) rescue nil
|
2016-08-23 13:55:52 -05:00
|
|
|
end
|
2015-08-18 16:15:46 -05:00
|
|
|
return false unless uri.present?
|
|
|
|
|
|
|
|
host = uri.host
|
|
|
|
return false unless host.present?
|
|
|
|
|
2017-02-27 11:17:52 -06:00
|
|
|
if uri.port.present? && uri.port != 80 && uri.port != 443
|
|
|
|
host << ":#{uri.port}"
|
|
|
|
end
|
|
|
|
|
2016-08-26 11:47:21 -05:00
|
|
|
path = uri.path
|
|
|
|
path << "?" << uri.query if uri.query.present?
|
|
|
|
|
2017-02-17 11:39:33 -06:00
|
|
|
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
|
2016-08-26 11:47:21 -05:00
|
|
|
|
2017-02-17 11:39:33 -06:00
|
|
|
def self.url_allowed?(url)
|
2017-08-02 15:43:31 -05:00
|
|
|
# Work around IFRAME reload on WebKit where the referer will be set to the Forum URL
|
|
|
|
return true if url&.starts_with?(Discourse.base_url)
|
|
|
|
|
2017-02-27 11:17:52 -06:00
|
|
|
uri = URI(URI.encode(url)) rescue nil
|
2017-02-17 11:39:33 -06:00
|
|
|
uri.present? && record_for_url(uri).present?
|
2015-08-18 16:15:46 -05:00
|
|
|
end
|
|
|
|
|
2016-01-11 10:06:09 -06:00
|
|
|
private
|
|
|
|
|
|
|
|
def host_must_be_valid
|
2017-04-15 06:06:51 -05:00
|
|
|
if host !~ /\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,10}(:[0-9]{1,5})?(\/.*)?\Z/i &&
|
2017-03-15 16:16:34 -05:00
|
|
|
host !~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(:[0-9]{1,5})?(\/.*)?\Z/ &&
|
2016-07-22 14:33:21 -05:00
|
|
|
host !~ /\A([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.)?localhost(\:[0-9]{1,5})?(\/.*)?\Z/i
|
2016-01-11 10:06:09 -06:00
|
|
|
errors.add(:host, I18n.t('errors.messages.invalid'))
|
|
|
|
end
|
|
|
|
end
|
2015-08-18 16:15:46 -05:00
|
|
|
end
|
2015-09-17 19:41:10 -05:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: embeddable_hosts
|
|
|
|
#
|
2016-10-31 04:32:11 -05:00
|
|
|
# id :integer not null, primary key
|
2017-12-05 09:29:14 -06:00
|
|
|
# host :string(255) not null
|
2016-10-31 04:32:11 -05:00
|
|
|
# category_id :integer not null
|
2017-12-05 09:29:14 -06:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2016-10-31 04:32:11 -05:00
|
|
|
# path_whitelist :string
|
2017-05-12 13:46:57 -05:00
|
|
|
# class_name :string
|
2015-09-17 19:41:10 -05:00
|
|
|
#
|