summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorGeorge Koltsov <gkoltsov@gitlab.com>2019-07-30 11:53:23 +0100
committerGeorge Koltsov <gkoltsov@gitlab.com>2019-08-02 15:39:18 +0100
commit8abf920d1f55e9117dd3b05d81ee9ebf7721f2bd (patch)
treebcdc229c8b9819f0ff6f5dc99095fe40bbeff51c /app
parentac7661924eebd6eb0fa72848e2b4bf4391ebf113 (diff)
downloadgitlab-ce-8abf920d1f55e9117dd3b05d81ee9ebf7721f2bd.tar.gz
Refactor SystemHookUrlValidator and specs
Simplify SystemHookUrlValidator to inherit from PublicUrlValidator Refactor specs to move out shared examples to be used in both system hooks and public url validators.
Diffstat (limited to 'app')
-rw-r--r--app/models/hooks/system_hook.rb3
-rw-r--r--app/models/hooks/web_hook.rb4
-rw-r--r--app/validators/system_hook_url_validator.rb16
3 files changed, 5 insertions, 18 deletions
diff --git a/app/models/hooks/system_hook.rb b/app/models/hooks/system_hook.rb
index b83913c845f..3d54d17e787 100644
--- a/app/models/hooks/system_hook.rb
+++ b/app/models/hooks/system_hook.rb
@@ -14,8 +14,7 @@ class SystemHook < WebHook
default_value_for :repository_update_events, true
default_value_for :merge_requests_events, false
- validates :url, presence: true, public_url: false, system_hook_url: { allow_localhost: lambda(&:allow_local_requests?),
- allow_local_network: lambda(&:allow_local_requests?) }
+ validates :url, system_hook_url: true
# Allow urls pointing localhost and the local network
def allow_local_requests?
diff --git a/app/models/hooks/web_hook.rb b/app/models/hooks/web_hook.rb
index 3203bb024ab..16fc7fdbd48 100644
--- a/app/models/hooks/web_hook.rb
+++ b/app/models/hooks/web_hook.rb
@@ -15,8 +15,8 @@ class WebHook < ApplicationRecord
has_many :web_hook_logs, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
- validates :url, presence: true, public_url: { allow_localhost: lambda(&:allow_local_requests?),
- allow_local_network: lambda(&:allow_local_requests?) }
+ validates :url, presence: true
+ validates :url, public_url: true, unless: ->(hook) { hook.is_a?(SystemHook) }
validates :token, format: { without: /\n/ }
validates :push_events_branch_filter, branch_filter: true
diff --git a/app/validators/system_hook_url_validator.rb b/app/validators/system_hook_url_validator.rb
index e482828685d..f4253006dad 100644
--- a/app/validators/system_hook_url_validator.rb
+++ b/app/validators/system_hook_url_validator.rb
@@ -7,23 +7,11 @@
# ApplicationSetting.allow_local_requests_from_system_hooks
#
# Example:
-#
# class SystemHook < WebHook
-# validates :url, system_hook_url: { allow_localhost: true, allow_local_network: true }
+# validates :url, system_hook_url: true
# end
#
-class SystemHookUrlValidator < AddressableUrlValidator
- DEFAULT_OPTIONS = {
- allow_localhost: false,
- allow_local_network: false
- }.freeze
-
- def initialize(options)
- options.reverse_merge!(DEFAULT_OPTIONS)
-
- super(options)
- end
-
+class SystemHookUrlValidator < PublicUrlValidator
def self.allow_setting_local_requests?
ApplicationSetting.current&.allow_local_requests_from_system_hooks?
end