From e5e1c907c01b53194f77e8d8de53554ba1824e7c Mon Sep 17 00:00:00 2001 From: George Koltsov Date: Fri, 26 Jul 2019 11:21:52 +0100 Subject: Add outbound requests setting for system hooks This MR adds new application setting to network section `allow_local_requests_from_system_hooks`. Prior to this change system hooks were allowed to do local network requests by default and we are adding an ability for admins to control it. --- app/validators/addressable_url_validator.rb | 2 +- app/validators/system_hook_url_validator.rb | 30 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 app/validators/system_hook_url_validator.rb (limited to 'app/validators') diff --git a/app/validators/addressable_url_validator.rb b/app/validators/addressable_url_validator.rb index 273e15ef925..bb445499cee 100644 --- a/app/validators/addressable_url_validator.rb +++ b/app/validators/addressable_url_validator.rb @@ -107,6 +107,6 @@ class AddressableUrlValidator < ActiveModel::EachValidator # calls this validator. # # See https://gitlab.com/gitlab-org/gitlab-ee/issues/9833 - ApplicationSetting.current&.allow_local_requests_from_hooks_and_services? + ApplicationSetting.current&.allow_local_requests_from_web_hooks_and_services? end end diff --git a/app/validators/system_hook_url_validator.rb b/app/validators/system_hook_url_validator.rb new file mode 100644 index 00000000000..c8c0007e35b --- /dev/null +++ b/app/validators/system_hook_url_validator.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +# SystemHookUrlValidator +# +# Custom validator specifically for SystemHook URLs. This validator works like AddressableUrlValidator but +# 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 = { + allow_localhost: true, + allow_local_network: true + }.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 -- cgit v1.2.3