2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-08-18 17:15:46 -04:00
|
|
|
class EmbeddableHost < ActiveRecord::Base
|
2016-01-11 11:06:09 -05:00
|
|
|
validate :host_must_be_valid
|
2015-08-18 17:15:46 -04:00
|
|
|
belongs_to :category
|
2019-03-29 12:05:51 -04:00
|
|
|
after_destroy :reset_embedding_settings
|
2015-08-18 17:15:46 -04:00
|
|
|
|
|
|
|
before_validation do
|
|
|
|
self.host.sub!(/^https?:\/\//, '')
|
|
|
|
self.host.sub!(/\/.*$/, '')
|
|
|
|
end
|
|
|
|
|
2022-01-10 21:57:21 -05:00
|
|
|
# TODO(2021-07-23): Remove
|
|
|
|
self.ignored_columns = ["path_whitelist"]
|
|
|
|
|
2016-08-23 14:55:52 -04:00
|
|
|
def self.record_for_url(uri)
|
|
|
|
if uri.is_a?(String)
|
2018-03-28 04:20:08 -04:00
|
|
|
uri = begin
|
|
|
|
URI(UrlHelper.escape_uri(uri))
|
2022-02-06 22:25:42 -05:00
|
|
|
rescue URI::Error, Addressable::URI::InvalidURIError
|
2018-03-28 04:20:08 -04:00
|
|
|
end
|
2016-08-23 14:55:52 -04:00
|
|
|
end
|
2022-02-06 22:25:42 -05:00
|
|
|
|
2015-08-18 17:15:46 -04:00
|
|
|
return false unless uri.present?
|
|
|
|
|
|
|
|
host = uri.host
|
|
|
|
return false unless host.present?
|
|
|
|
|
2017-02-27 12:17:52 -05:00
|
|
|
if uri.port.present? && uri.port != 80 && uri.port != 443
|
|
|
|
host << ":#{uri.port}"
|
|
|
|
end
|
|
|
|
|
2016-08-26 12:47:21 -04:00
|
|
|
path = uri.path
|
|
|
|
path << "?" << uri.query if uri.query.present?
|
|
|
|
|
2017-02-17 12:39:33 -05:00
|
|
|
where("lower(host) = ?", host).each do |eh|
|
2020-07-26 20:23:54 -04:00
|
|
|
return eh if eh.allowed_paths.blank?
|
2017-12-12 11:56:28 -05:00
|
|
|
|
2020-07-26 20:23:54 -04:00
|
|
|
path_regexp = Regexp.new(eh.allowed_paths)
|
2019-12-11 21:49:21 -05:00
|
|
|
return eh if path_regexp.match(path) || path_regexp.match(UrlHelper.unencode(path))
|
2017-02-17 12:39:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
2016-08-26 12:47:21 -04:00
|
|
|
|
2017-02-17 12:39:33 -05:00
|
|
|
def self.url_allowed?(url)
|
2021-07-16 14:25:49 -04:00
|
|
|
return false if url.nil?
|
|
|
|
|
2017-08-02 16:43:31 -04:00
|
|
|
# Work around IFRAME reload on WebKit where the referer will be set to the Forum URL
|
2019-09-29 20:51:59 -04:00
|
|
|
return true if url&.starts_with?(Discourse.base_url) && EmbeddableHost.exists?
|
2017-08-02 16:43:31 -04:00
|
|
|
|
2018-03-28 04:20:08 -04:00
|
|
|
uri = begin
|
|
|
|
URI(UrlHelper.escape_uri(url))
|
2018-08-14 06:23:32 -04:00
|
|
|
rescue URI::Error
|
2018-03-28 04:20:08 -04:00
|
|
|
end
|
|
|
|
|
2017-02-17 12:39:33 -05:00
|
|
|
uri.present? && record_for_url(uri).present?
|
2015-08-18 17:15:46 -04:00
|
|
|
end
|
|
|
|
|
2016-01-11 11:06:09 -05:00
|
|
|
private
|
|
|
|
|
2019-03-29 12:05:51 -04:00
|
|
|
def reset_embedding_settings
|
|
|
|
unless EmbeddableHost.exists?
|
|
|
|
Embedding.settings.each { |s| SiteSetting.set(s.to_s, SiteSetting.defaults[s]) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-11 11:06:09 -05:00
|
|
|
def host_must_be_valid
|
2020-06-18 13:58:47 -04:00
|
|
|
if host !~ /\A[a-z0-9]+([\-\.]+{1}[a-z0-9]+)*\.[a-z]{2,24}(:[0-9]{1,5})?(\/.*)?\Z/i &&
|
2017-03-15 17:16:34 -04:00
|
|
|
host !~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(:[0-9]{1,5})?(\/.*)?\Z/ &&
|
2016-07-22 15:33:21 -04:00
|
|
|
host !~ /\A([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.)?localhost(\:[0-9]{1,5})?(\/.*)?\Z/i
|
2016-01-11 11:06:09 -05:00
|
|
|
errors.add(:host, I18n.t('errors.messages.invalid'))
|
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2015-08-18 17:15:46 -04:00
|
|
|
end
|
2015-09-17 20:41:10 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: embeddable_hosts
|
|
|
|
#
|
2020-08-20 23:36:53 -04:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# host :string not null
|
|
|
|
# category_id :integer not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# class_name :string
|
|
|
|
# allowed_paths :string
|
2015-09-17 20:41:10 -04:00
|
|
|
#
|