BigW Consortium Gitlab

regex.rb 2.68 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 17
    end


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

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


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

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

35 36 37

    def project_path_regex
      @project_path_regex ||= /\A[a-zA-Z0-9_.][a-zA-Z0-9_\-\.]*(?<!\.git)\z/.freeze
38 39
    end

40 41 42
    def project_path_regex_message
      "can contain only letters, digits, '_', '-' and '.'. " \
      "Cannot start with '-' or end in '.git'" \
43 44
    end

45 46 47

    def file_name_regex
      @file_name_regex ||= /\A[a-zA-Z0-9_\-\.]*\z/.freeze
48
    end
49

50 51
    def file_name_regex_message
      "can contain only letters, digits, '_', '-' and '.'. "
52 53
    end

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    def file_path_regex
      @file_path_regex ||= /\A[a-zA-Z0-9_\-\.\/]*\z/.freeze
    end

    def file_path_regex_message
      "can contain only letters, digits, '_', '-' and '.'. Separate directories with a '/'. "
    end


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

    def directory_traversal_regex_message
      "cannot include directory traversal. "
    end

71

72
    def archive_formats_regex
73 74
      #                           |zip|tar|    tar.gz    |         tar.bz2         |
      @archive_formats_regex ||= /(zip|tar|tar\.gz|tgz|gz|tar\.bz2|tbz|tbz2|tb2|bz2)/.freeze
75
    end
76

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

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