2017-04-12 09:28:23 +00:00
|
|
|
# DynamicPathValidator
|
2015-12-07 21:17:12 +00:00
|
|
|
#
|
2017-04-12 09:28:23 +00:00
|
|
|
# Custom validator for GitLab path values.
|
|
|
|
# These paths are assigned to `Namespace` (& `Group` as a subclass) & `Project`
|
2015-12-07 21:17:12 +00:00
|
|
|
#
|
2015-12-07 21:29:39 +00:00
|
|
|
# Values are checked for formatting and exclusion from a list of reserved path
|
|
|
|
# names.
|
2017-04-12 09:28:23 +00:00
|
|
|
class DynamicPathValidator < ActiveModel::EachValidator
|
2017-04-05 13:41:00 +00:00
|
|
|
# All routes that appear on the top level must be listed here.
|
|
|
|
# This will make sure that groups cannot be created with these names
|
|
|
|
# as these routes would be masked by the paths already in place.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# /api/api-project
|
|
|
|
#
|
|
|
|
# the path `api` shouldn't be allowed because it would be masked by `api/*`
|
|
|
|
#
|
|
|
|
TOP_LEVEL_ROUTES = Set.new(%w[
|
2017-04-24 10:38:09 +00:00
|
|
|
-
|
2016-09-29 08:36:38 +00:00
|
|
|
.well-known
|
2017-04-24 10:38:09 +00:00
|
|
|
abuse_reports
|
2015-12-07 21:29:39 +00:00
|
|
|
admin
|
|
|
|
all
|
2017-04-24 10:38:09 +00:00
|
|
|
api
|
2015-12-07 21:29:39 +00:00
|
|
|
assets
|
2017-04-24 10:38:09 +00:00
|
|
|
autocomplete
|
2015-12-07 21:29:39 +00:00
|
|
|
ci
|
|
|
|
dashboard
|
2017-04-24 10:38:09 +00:00
|
|
|
explore
|
2015-12-07 21:29:39 +00:00
|
|
|
files
|
|
|
|
groups
|
2017-04-24 10:38:09 +00:00
|
|
|
health_check
|
2015-12-07 21:29:39 +00:00
|
|
|
help
|
|
|
|
hooks
|
2017-04-24 10:38:09 +00:00
|
|
|
import
|
|
|
|
invites
|
2015-12-07 21:29:39 +00:00
|
|
|
issues
|
2017-04-24 10:38:09 +00:00
|
|
|
jwt
|
|
|
|
koding
|
|
|
|
member
|
2015-12-07 21:29:39 +00:00
|
|
|
merge_requests
|
2016-01-08 09:19:22 +00:00
|
|
|
new
|
2015-12-07 21:29:39 +00:00
|
|
|
notes
|
2017-04-24 10:38:09 +00:00
|
|
|
notification_settings
|
|
|
|
oauth
|
2015-12-07 21:29:39 +00:00
|
|
|
profile
|
|
|
|
projects
|
|
|
|
public
|
|
|
|
repository
|
2016-10-11 01:58:26 +00:00
|
|
|
robots.txt
|
2015-12-07 21:29:39 +00:00
|
|
|
s
|
|
|
|
search
|
2017-04-24 10:38:09 +00:00
|
|
|
sent_notifications
|
2015-12-07 21:29:39 +00:00
|
|
|
services
|
|
|
|
snippets
|
|
|
|
teams
|
|
|
|
u
|
|
|
|
unsubscribes
|
2017-04-03 12:07:15 +00:00
|
|
|
uploads
|
2017-04-24 10:38:09 +00:00
|
|
|
users
|
2017-04-05 13:41:00 +00:00
|
|
|
]).freeze
|
2015-12-07 21:29:39 +00:00
|
|
|
|
2017-04-05 13:41:00 +00:00
|
|
|
# All project routes with wildcard argument must be listed here.
|
|
|
|
# Otherwise it can lead to routing issues when route considered as project name.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# /group/project/tree/deploy_keys
|
|
|
|
#
|
|
|
|
# without tree as reserved name routing can match 'group/project' as group name,
|
|
|
|
# 'tree' as project name and 'deploy_keys' as route.
|
|
|
|
#
|
2017-04-24 10:38:09 +00:00
|
|
|
WILDCARD_ROUTES = Set.new(%w[
|
|
|
|
badges
|
|
|
|
blame
|
|
|
|
blob
|
2017-04-28 15:53:28 +00:00
|
|
|
builds
|
2017-04-24 10:38:09 +00:00
|
|
|
commits
|
|
|
|
create
|
|
|
|
create_dir
|
|
|
|
edit
|
|
|
|
environments/folders
|
|
|
|
files
|
|
|
|
find_file
|
|
|
|
gitlab-lfs/objects
|
|
|
|
info/lfs/objects
|
|
|
|
new
|
|
|
|
preview
|
|
|
|
raw
|
2017-04-28 15:53:28 +00:00
|
|
|
refs
|
2017-04-24 10:38:09 +00:00
|
|
|
tree
|
|
|
|
update
|
|
|
|
wikis
|
|
|
|
]).freeze
|
2017-03-06 18:26:58 +00:00
|
|
|
|
2017-04-11 14:21:52 +00:00
|
|
|
STRICT_RESERVED = (TOP_LEVEL_ROUTES | WILDCARD_ROUTES).freeze
|
2017-03-06 18:26:58 +00:00
|
|
|
|
2017-04-18 14:27:11 +00:00
|
|
|
def self.valid?(path)
|
|
|
|
path_segments = path.split('/')
|
2017-04-11 13:51:33 +00:00
|
|
|
|
2017-04-18 14:27:11 +00:00
|
|
|
!reserved?(path) && path_segments.all? { |value| follow_format?(value) }
|
2017-04-11 13:51:33 +00:00
|
|
|
end
|
|
|
|
|
2017-04-18 14:27:11 +00:00
|
|
|
def self.reserved?(path)
|
|
|
|
path = path.to_s.downcase
|
|
|
|
top_level, wildcard_part = path.split('/', 2)
|
|
|
|
|
|
|
|
includes_reserved_top_level?(top_level) || includes_reserved_wildcard?(wildcard_part)
|
2016-11-14 14:55:31 +00:00
|
|
|
end
|
|
|
|
|
2017-04-18 14:27:11 +00:00
|
|
|
def self.includes_reserved_wildcard?(path)
|
|
|
|
WILDCARD_ROUTES.any? do |reserved_word|
|
|
|
|
contains_path_part?(path, reserved_word)
|
|
|
|
end
|
2017-04-05 13:41:00 +00:00
|
|
|
end
|
|
|
|
|
2017-04-18 14:27:11 +00:00
|
|
|
def self.includes_reserved_top_level?(path)
|
|
|
|
TOP_LEVEL_ROUTES.any? do |reserved_route|
|
|
|
|
contains_path_part?(path, reserved_route)
|
2017-03-06 18:26:58 +00:00
|
|
|
end
|
2016-11-14 14:55:31 +00:00
|
|
|
end
|
|
|
|
|
2017-04-18 14:27:11 +00:00
|
|
|
def self.contains_path_part?(path, part)
|
2017-04-24 10:22:51 +00:00
|
|
|
path =~ %r{(/|\A)#{Regexp.quote(part)}(/|\z)}
|
2017-04-18 14:27:11 +00:00
|
|
|
end
|
|
|
|
|
2016-11-14 14:55:31 +00:00
|
|
|
def self.follow_format?(value)
|
|
|
|
value =~ Gitlab::Regex.namespace_regex
|
|
|
|
end
|
|
|
|
|
|
|
|
delegate :reserved?, :follow_format?, to: :class
|
|
|
|
|
2015-12-07 21:17:12 +00:00
|
|
|
def validate_each(record, attribute, value)
|
2016-11-14 14:55:31 +00:00
|
|
|
unless follow_format?(value)
|
2015-12-07 21:17:12 +00:00
|
|
|
record.errors.add(attribute, Gitlab::Regex.namespace_regex_message)
|
|
|
|
end
|
|
|
|
|
2017-04-18 14:27:11 +00:00
|
|
|
full_path = record.respond_to?(:full_path) ? record.full_path : value
|
2017-04-05 13:41:00 +00:00
|
|
|
|
2017-04-18 14:27:11 +00:00
|
|
|
if reserved?(full_path)
|
|
|
|
record.errors.add(attribute, "#{value} is a reserved name")
|
2017-04-05 13:41:00 +00:00
|
|
|
end
|
|
|
|
end
|
2015-12-07 21:17:12 +00:00
|
|
|
end
|