2018-06-27 16:20:23 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-01 23:45:36 +00:00
|
|
|
# UrlValidator
|
|
|
|
#
|
|
|
|
# Custom validator for URLs.
|
|
|
|
#
|
|
|
|
# By default, only URLs for the HTTP(S) protocols will be considered valid.
|
|
|
|
# Provide a `:protocols` option to configure accepted protocols.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# class User < ActiveRecord::Base
|
|
|
|
# validates :personal_url, url: true
|
|
|
|
#
|
|
|
|
# validates :ftp_url, url: { protocols: %w(ftp) }
|
|
|
|
#
|
|
|
|
# validates :git_url, url: { protocols: %w(http https ssh git) }
|
|
|
|
# end
|
|
|
|
#
|
2018-06-01 11:43:53 +00:00
|
|
|
# This validator can also block urls pointing to localhost or the local network to
|
|
|
|
# protect against Server-side Request Forgery (SSRF), or check for the right port.
|
|
|
|
#
|
2018-06-11 13:29:37 +00:00
|
|
|
# The available options are:
|
|
|
|
# - protocols: Allowed protocols. Default: http and https
|
|
|
|
# - allow_localhost: Allow urls pointing to localhost. Default: true
|
|
|
|
# - allow_local_network: Allow urls pointing to private network addresses. Default: true
|
|
|
|
# - ports: Allowed ports. Default: all.
|
|
|
|
# - enforce_user: Validate user format. Default: false
|
|
|
|
#
|
2018-06-01 11:43:53 +00:00
|
|
|
# Example:
|
|
|
|
# class User < ActiveRecord::Base
|
|
|
|
# validates :personal_url, url: { allow_localhost: false, allow_local_network: false}
|
|
|
|
#
|
|
|
|
# validates :web_url, url: { ports: [80, 443] }
|
|
|
|
# end
|
2015-12-01 23:45:36 +00:00
|
|
|
class UrlValidator < ActiveModel::EachValidator
|
2018-06-01 11:43:53 +00:00
|
|
|
DEFAULT_PROTOCOLS = %w(http https).freeze
|
|
|
|
|
|
|
|
attr_reader :record
|
|
|
|
|
2015-12-01 23:45:36 +00:00
|
|
|
def validate_each(record, attribute, value)
|
2018-06-01 11:43:53 +00:00
|
|
|
@record = record
|
|
|
|
|
2018-09-17 15:11:12 +00:00
|
|
|
unless value.present?
|
2018-06-11 13:29:37 +00:00
|
|
|
record.errors.add(attribute, 'must be a valid URL')
|
2018-09-17 15:11:12 +00:00
|
|
|
return
|
2015-12-01 23:45:36 +00:00
|
|
|
end
|
2018-06-01 11:43:53 +00:00
|
|
|
|
2018-09-17 15:11:12 +00:00
|
|
|
value = strip_value!(record, attribute, value)
|
|
|
|
|
2018-06-01 11:43:53 +00:00
|
|
|
Gitlab::UrlBlocker.validate!(value, blocker_args)
|
|
|
|
rescue Gitlab::UrlBlocker::BlockedUrlError => e
|
|
|
|
record.errors.add(attribute, "is blocked: #{e.message}")
|
2015-12-01 23:45:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-09-17 15:11:12 +00:00
|
|
|
def strip_value!(record, attribute, value)
|
|
|
|
new_value = value.strip
|
|
|
|
return value if new_value == value
|
|
|
|
|
|
|
|
record.public_send("#{attribute}=", new_value) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
|
|
|
|
2015-12-01 23:45:36 +00:00
|
|
|
def default_options
|
2018-06-01 11:43:53 +00:00
|
|
|
# By default the validator doesn't block any url based on the ip address
|
|
|
|
{
|
|
|
|
protocols: DEFAULT_PROTOCOLS,
|
|
|
|
ports: [],
|
|
|
|
allow_localhost: true,
|
2018-06-11 13:29:37 +00:00
|
|
|
allow_local_network: true,
|
|
|
|
enforce_user: false
|
2018-06-01 11:43:53 +00:00
|
|
|
}
|
2015-12-01 23:45:36 +00:00
|
|
|
end
|
|
|
|
|
2018-06-01 11:43:53 +00:00
|
|
|
def current_options
|
|
|
|
options = self.options.map do |option, value|
|
|
|
|
[option, value.is_a?(Proc) ? value.call(record) : value]
|
|
|
|
end.to_h
|
|
|
|
|
|
|
|
default_options.merge(options)
|
|
|
|
end
|
2016-02-22 12:55:36 +00:00
|
|
|
|
2018-06-01 11:43:53 +00:00
|
|
|
def blocker_args
|
2018-06-11 13:29:37 +00:00
|
|
|
current_options.slice(*default_options.keys).tap do |args|
|
2018-06-01 11:43:53 +00:00
|
|
|
if allow_setting_local_requests?
|
|
|
|
args[:allow_localhost] = args[:allow_local_network] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-12-01 23:45:36 +00:00
|
|
|
|
2018-06-01 11:43:53 +00:00
|
|
|
def allow_setting_local_requests?
|
|
|
|
ApplicationSetting.current&.allow_local_requests_from_hooks_and_services?
|
2015-12-01 23:45:36 +00:00
|
|
|
end
|
|
|
|
end
|