2019-07-26 10:21:52 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# SystemHookUrlValidator
|
|
|
|
#
|
2019-07-26 13:03:06 +00:00
|
|
|
# Custom validator specific to SystemHook URLs. This validator works like AddressableUrlValidator but
|
2019-07-26 10:21:52 +00:00
|
|
|
# it blocks urls pointing to localhost or the local network depending on
|
|
|
|
# ApplicationSetting.allow_local_requests_from_system_hooks
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# class SystemHook < WebHook
|
|
|
|
# validates :url, system_hook_url: { allow_localhost: true, allow_local_network: true }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
class SystemHookUrlValidator < AddressableUrlValidator
|
|
|
|
DEFAULT_OPTIONS = {
|
2019-07-26 13:03:06 +00:00
|
|
|
allow_localhost: false,
|
|
|
|
allow_local_network: false
|
2019-07-26 10:21:52 +00:00
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def initialize(options)
|
|
|
|
options.reverse_merge!(DEFAULT_OPTIONS)
|
|
|
|
|
|
|
|
super(options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.allow_setting_local_requests?
|
|
|
|
ApplicationSetting.current&.allow_local_requests_from_system_hooks?
|
|
|
|
end
|
|
|
|
end
|