namespace_validator.rb 2.58 KB
Newer Older
1
2
3
4
# NamespaceValidator
#
# Custom validator for GitLab namespace values.
#
5
6
# Values are checked for formatting and exclusion from a list of reserved path
# names.
7
class NamespaceValidator < ActiveModel::EachValidator
8
9
  RESERVED = %w[
    .well-known
10
11
12
13
14
15
16
17
18
19
20
    admin
    all
    assets
    ci
    dashboard
    files
    groups
    help
    hooks
    issues
    merge_requests
Robert Schilling's avatar
Robert Schilling committed
21
    new
22
23
24
25
26
    notes
    profile
    projects
    public
    repository
27
    robots.txt
28
29
30
31
32
33
34
35
    s
    search
    services
    snippets
    teams
    u
    unsubscribes
    users
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    api
    autocomplete
    search
    member
    explore
    uploads
    import
    notification_settings
    abuse_reports
    invites
    help
    koding
    health_check
    jwt
    oauth
    sent_notifications
52
  ].freeze
53

54
  WILDCARD_ROUTES = %w[tree commits wikis new edit create update logs_tree
55
                       preview blob blame raw files create_dir find_file
56
57
58
59
60
61
62
63
64
65
66
67
68
                       artifacts graphs refs badges info git-upload-pack
                       git-receive-pack gitlab-lfs autocomplete_sources
                       templates avatar commit pages compare network snippets
                       services mattermost deploy_keys forks import merge_requests
                       branches merged_branches tags protected_branches variables
                       triggers pipelines environments cycle_analytics builds
                       hooks container_registry milestones labels issues
                       project_members group_links notes noteable boards todos
                       uploads runners runner_projects settings repository
                       transfer remove_fork archive unarchive housekeeping
                       toggle_star preview_markdown export remove_export
                       generate_new_export download_export activity
                       new_issue_address registry].freeze
69
70
71

  STRICT_RESERVED = (RESERVED + WILDCARD_ROUTES).freeze

72
73
74
75
  def self.valid?(value)
    !reserved?(value) && follow_format?(value)
  end

76
77
78
79
80
81
  def self.reserved?(value, strict: false)
    if strict
      STRICT_RESERVED.include?(value)
    else
      RESERVED.include?(value)
    end
82
83
84
85
86
87
88
89
  end

  def self.follow_format?(value)
    value =~ Gitlab::Regex.namespace_regex
  end

  delegate :reserved?, :follow_format?, to: :class

90
  def validate_each(record, attribute, value)
91
    unless follow_format?(value)
92
93
94
      record.errors.add(attribute, Gitlab::Regex.namespace_regex_message)
    end

95
96
97
    strict = record.is_a?(Group) && record.parent_id

    if reserved?(value, strict: strict)
98
99
100
101
      record.errors.add(attribute, "#{value} is a reserved name")
    end
  end
end