utils.rb 4.3 KB
Newer Older
G
gfyoung 已提交
1 2
# frozen_string_literal: true

3 4 5 6
module Gitlab
  module Utils
    extend self

7
    # Ensure that the relative path will not traverse outside the base directory
8 9 10 11 12 13 14 15
    # We url decode the path to avoid passing invalid paths forward in url encoded format.
    # We are ok to pass some double encoded paths to File.open since they won't resolve.
    # Also see https://gitlab.com/gitlab-org/gitlab/-/merge_requests/24223#note_284122580
    # It also checks for ALT_SEPARATOR aka '\' (forward slash)
    def check_path_traversal!(path, allowed_absolute: false)
      path = CGI.unescape(path)

      if path.start_with?("..#{File::SEPARATOR}", "..#{File::ALT_SEPARATOR}") ||
16
          path.include?("#{File::SEPARATOR}..#{File::SEPARATOR}") ||
17 18 19 20 21
          path.end_with?("#{File::SEPARATOR}..") ||
          (!allowed_absolute && Pathname.new(path).absolute?)

        raise StandardError.new("Invalid path")
      end
22 23 24 25

      path
    end

26
    def force_utf8(str)
27
      str.dup.force_encoding(Encoding::UTF_8)
28
    end
F
Felipe Artur 已提交
29

30
    def ensure_utf8_size(str, bytes:)
31 32
      raise ArgumentError, 'Empty string provided!' if str.empty?
      raise ArgumentError, 'Negative string size provided!' if bytes.negative?
33

34
      truncated = str.each_char.each_with_object(+'') do |char, object|
35 36 37 38 39 40 41
        if object.bytesize + char.bytesize > bytes
          break object
        else
          object.concat(char)
        end
      end

42
      truncated + ('0' * (bytes - truncated.bytesize))
43 44
    end

45 46 47 48 49
    # Append path to host, making sure there's one single / in between
    def append_path(host, path)
      "#{host.to_s.sub(%r{\/+$}, '')}/#{path.to_s.sub(%r{^\/+}, '')}"
    end

V
vanadium23 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62
    # A slugified version of the string, suitable for inclusion in URLs and
    # domain names. Rules:
    #
    #   * Lowercased
    #   * Anything not matching [a-z0-9-] is replaced with a -
    #   * Maximum length is 63 bytes
    #   * First/Last Character is not a hyphen
    def slugify(str)
      return str.downcase
        .gsub(/[^a-z0-9]/, '-')[0..62]
        .gsub(/(\A-+|-+\z)/, '')
    end

63 64 65 66 67 68
    # Wraps ActiveSupport's Array#to_sentence to convert the given array to a
    # comma-separated sentence joined with localized 'or' Strings instead of 'and'.
    def to_exclusive_sentence(array)
      array.to_sentence(two_words_connector: _(' or '), last_word_connector: _(', or '))
    end

69 70
    # Converts newlines into HTML line break elements
    def nlbr(str)
71
      ActionView::Base.full_sanitizer.sanitize(+str, tags: []).gsub(/\r?\n/, '<br>').html_safe
72 73
    end

R
Robert Speicher 已提交
74 75 76 77
    def remove_line_breaks(str)
      str.gsub(/\r?\n/, '')
    end

F
Felipe Artur 已提交
78 79 80 81 82 83 84
    def to_boolean(value)
      return value if [true, false].include?(value)
      return true if value =~ /^(true|t|yes|y|1|on)$/i
      return false if value =~ /^(false|f|no|n|0|off)$/i

      nil
    end
85 86 87 88 89 90 91 92

    def boolean_to_yes_no(bool)
      if bool
        'Yes'
      else
        'No'
      end
    end
B
Bob Van Landuyt 已提交
93 94 95 96

    def random_string
      Random.rand(Float::MAX.to_i).to_s(36)
    end
M
Michael Kozono 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

    # See: http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby
    # Cross-platform way of finding an executable in the $PATH.
    #
    #   which('ruby') #=> /usr/bin/ruby
    def which(cmd, env = ENV)
      exts = env['PATHEXT'] ? env['PATHEXT'].split(';') : ['']

      env['PATH'].split(File::PATH_SEPARATOR).each do |path|
        exts.each do |ext|
          exe = File.join(path, "#{cmd}#{ext}")
          return exe if File.executable?(exe) && !File.directory?(exe)
        end
      end

      nil
    end
114

115 116 117 118 119 120
    def try_megabytes_to_bytes(size)
      Integer(size).megabytes
    rescue ArgumentError
      size
    end

R
Rubén Dávila 已提交
121 122 123 124
    def bytes_to_megabytes(bytes)
      bytes.to_f / Numeric::MEGABYTE
    end

125 126 127 128 129 130 131
    # Used in EE
    # Accepts either an Array or a String and returns an array
    def ensure_array_from_string(string_or_array)
      return string_or_array if string_or_array.is_a?(Array)

      string_or_array.split(',').map(&:strip)
    end
132 133 134 135 136 137 138 139 140 141

    def deep_indifferent_access(data)
      if data.is_a?(Array)
        data.map(&method(:deep_indifferent_access))
      elsif data.is_a?(Hash)
        data.with_indifferent_access
      else
        data
      end
    end
142 143 144 145 146 147 148

    def string_to_ip_object(str)
      return unless str

      IPAddr.new(str)
    rescue IPAddr::InvalidAddressError
    end
149 150
  end
end