regex.rb 3.17 KB
Newer Older
1 2 3 4
module Gitlab
  module Regex
    extend self

5
    NAMESPACE_REGEX_STR = '(?:[a-zA-Z0-9_\.][a-zA-Z0-9_\-\.]*[a-zA-Z0-9_\-]|[a-zA-Z0-9_])(?<!\.git|\.atom)'.freeze
6

7 8
    def namespace_regex
      @namespace_regex ||= /\A#{NAMESPACE_REGEX_STR}\z/.freeze
9 10
    end

11 12 13 14
    def namespace_route_regex
      @namespace_route_regex ||= /#{NAMESPACE_REGEX_STR}/.freeze
    end

15
    def namespace_regex_message
Douwe Maan's avatar
Douwe Maan committed
16
      "can contain only letters, digits, '_', '-' and '.'. " \
17
      "Cannot start with '-' or end in '.', '.git' or '.atom'." \
Douwe Maan's avatar
Douwe Maan committed
18 19 20
    end

    def namespace_name_regex
21
      @namespace_name_regex ||= /\A[\p{Alnum}\p{Pd}_\. ]*\z/.freeze
22 23
    end

Douwe Maan's avatar
Douwe Maan committed
24
    def namespace_name_regex_message
25
      "can contain only letters, digits, '_', '.', dash and space."
Douwe Maan's avatar
Douwe Maan committed
26 27
    end

28
    def project_name_regex
29
      @project_name_regex ||= /\A[\p{Alnum}_][\p{Alnum}\p{Pd}_\. ]*\z/.freeze
30 31
    end

Douwe Maan's avatar
Douwe Maan committed
32
    def project_name_regex_message
33
      "can contain only letters, digits, '_', '.', dash and space. " \
34 35 36
      "It must start with letter, digit or '_'."
    end

Douwe Maan's avatar
Douwe Maan committed
37
    def project_path_regex
38
      @project_path_regex ||= /\A[a-zA-Z0-9_.][a-zA-Z0-9_\-\.]*(?<!\.git|\.atom)\z/.freeze
39 40
    end

Douwe Maan's avatar
Douwe Maan committed
41 42
    def project_path_regex_message
      "can contain only letters, digits, '_', '-' and '.'. " \
43
      "Cannot start with '-', end in '.git' or end in '.atom'" \
44 45
    end

Douwe Maan's avatar
Douwe Maan committed
46
    def file_name_regex
47
      @file_name_regex ||= /\A[a-zA-Z0-9_\-\.\@]*\z/.freeze
48
    end
49

Douwe Maan's avatar
Douwe Maan committed
50
    def file_name_regex_message
51
      "can contain only letters, digits, '_', '-', '@' and '.'."
52 53
    end

54
    def file_path_regex
55
      @file_path_regex ||= /\A[a-zA-Z0-9_\-\.\/\@]*\z/.freeze
56 57 58
    end

    def file_path_regex_message
59
      "can contain only letters, digits, '_', '-', '@' and '.'. Separate directories with a '/'."
60 61 62 63 64 65 66
    end

    def directory_traversal_regex
      @directory_traversal_regex ||= /\.{2}/.freeze
    end

    def directory_traversal_regex_message
67
      "cannot include directory traversal."
68 69
    end

70
    def archive_formats_regex
Douwe Maan's avatar
Douwe Maan committed
71 72
      #                           |zip|tar|    tar.gz    |         tar.bz2         |
      @archive_formats_regex ||= /(zip|tar|tar\.gz|tgz|gz|tar\.bz2|tbz|tbz2|tb2|bz2)/.freeze
73
    end
74

75 76 77 78
    def git_reference_regex
      # Valid git ref regex, see:
      # https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html

Douwe Maan's avatar
Douwe Maan committed
79
      @git_reference_regex ||= %r{
80
        (?!
81 82 83
           (?# doesn't begins with)
           \/|                    (?# rule #6)
           (?# doesn't contain)
84
           .*(?:
85 86 87 88
              [\/.]\.|            (?# rule #1,3)
              \/\/|               (?# rule #6)
              @\{|                (?# rule #8)
              \\                  (?# rule #9)
89 90
           )
        )
91 92 93 94
        [^\000-\040\177~^:?*\[]+  (?# rule #4-5)
        (?# doesn't end with)
        (?<!\.lock)               (?# rule #1)
        (?<![\/.])                (?# rule #6-7)
Douwe Maan's avatar
Douwe Maan committed
95
      }x.freeze
96
    end
97

Kamil Trzcinski's avatar
WIP  
Kamil Trzcinski committed
98
    def container_registry_reference_regex
99 100
      git_reference_regex
    end
101 102

    def environment_name_regex
103
      @environment_name_regex ||= /\A[a-zA-Z0-9_\\\/\${}. -]+\z/.freeze
104 105 106
    end

    def environment_name_regex_message
107
      "can contain only letters, digits, '-', '_', '/', '$', '{', '}', '.' and spaces"
108
    end
109 110
  end
end