BigW Consortium Gitlab

regex.rb 2.99 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_])'.freeze
6

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

11
    def namespace_regex_message
12
      "can contain only letters, digits, '_', '-' and '.'. " \
13
      "Cannot start with '-' or end in '.'." \
14 15 16
    end

    def namespace_name_regex
17
      @namespace_name_regex ||= /\A[\p{Alnum}\p{Pd}_\. ]*\z/.freeze
18 19
    end

20
    def namespace_name_regex_message
21
      "can contain only letters, digits, '_', '.', dash and space."
22 23
    end

24
    def project_name_regex
25
      @project_name_regex ||= /\A[\p{Alnum}_][\p{Alnum}\p{Pd}_\. ]*\z/.freeze
26 27
    end

28
    def project_name_regex_message
29
      "can contain only letters, digits, '_', '.', dash and space. " \
30 31 32
      "It must start with letter, digit or '_'."
    end

33
    def project_path_regex
34
      @project_path_regex ||= /\A[a-zA-Z0-9_.][a-zA-Z0-9_\-\.]*(?<!\.git|\.atom)\z/.freeze
35 36
    end

37 38
    def project_path_regex_message
      "can contain only letters, digits, '_', '-' and '.'. " \
39
      "Cannot start with '-', end in '.git' or end in '.atom'" \
40 41
    end

42
    def file_name_regex
43
      @file_name_regex ||= /\A[a-zA-Z0-9_\-\.\@]*\z/.freeze
44
    end
45

46
    def file_name_regex_message
47
      "can contain only letters, digits, '_', '-', '@' and '.'. "
48 49
    end

50
    def file_path_regex
51
      @file_path_regex ||= /\A[a-zA-Z0-9_\-\.\/\@]*\z/.freeze
52 53 54
    end

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

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

    def directory_traversal_regex_message
      "cannot include directory traversal. "
    end

66
    def archive_formats_regex
67 68
      #                           |zip|tar|    tar.gz    |         tar.bz2         |
      @archive_formats_regex ||= /(zip|tar|tar\.gz|tgz|gz|tar\.bz2|tbz|tbz2|tb2|bz2)/.freeze
69
    end
70

71 72 73 74
    def git_reference_regex
      # Valid git ref regex, see:
      # https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html

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

Kamil Trzcinski committed
94
    def container_registry_reference_regex
95 96
      git_reference_regex
    end
97 98 99 100 101 102 103 104

    def environment_name_regex
      @environment_name_regex ||= /\A[a-zA-Z0-9_-]+\z/.freeze
    end

    def environment_name_regex_message
      "can contain only letters, digits, '-' and '_'."
    end
105 106
  end
end