2018-11-19 21:01:13 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-09-21 08:05:37 -04:00
|
|
|
# Searches and reads file present on GitLab installation directory
|
2016-06-24 15:43:46 -04:00
|
|
|
module Gitlab
|
|
|
|
module Template
|
|
|
|
module Finders
|
|
|
|
class GlobalTemplateFinder < BaseTemplateFinder
|
2021-01-19 16:10:45 -05:00
|
|
|
def initialize(base_dir, extension, categories = {}, include_categories_for_file = {}, excluded_patterns: [])
|
2016-06-24 15:43:46 -04:00
|
|
|
@categories = categories
|
|
|
|
@extension = extension
|
2021-01-19 16:10:45 -05:00
|
|
|
@include_categories_for_file = include_categories_for_file
|
2020-09-08 08:08:41 -04:00
|
|
|
@excluded_patterns = excluded_patterns
|
2020-02-18 10:08:51 -05:00
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
super(base_dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read(path)
|
|
|
|
File.read(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find(key)
|
2020-02-18 10:08:51 -05:00
|
|
|
return if excluded?(key)
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
file_name = "#{key}#{@extension}"
|
|
|
|
|
2018-12-04 10:59:01 -05:00
|
|
|
# The key is untrusted input, so ensure we can't be directed outside
|
|
|
|
# of base_dir
|
|
|
|
Gitlab::Utils.check_path_traversal!(file_name)
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
directory = select_directory(file_name)
|
|
|
|
directory ? File.join(category_directory(directory), file_name) : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_files_for(dir)
|
2018-11-19 21:01:13 -05:00
|
|
|
dir = "#{dir}/" unless dir.end_with?('/')
|
2020-02-18 10:08:51 -05:00
|
|
|
|
|
|
|
Dir.glob(File.join(dir, "*#{@extension}")).select do |f|
|
|
|
|
next if excluded?(f)
|
|
|
|
|
|
|
|
f =~ self.class.filter_regex(@extension)
|
|
|
|
end
|
2016-06-24 15:43:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-02-18 10:08:51 -05:00
|
|
|
def excluded?(file_name)
|
2020-09-08 08:08:41 -04:00
|
|
|
@excluded_patterns.any? { |pattern| pattern.match?(file_name) }
|
2020-02-18 10:08:51 -05:00
|
|
|
end
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
def select_directory(file_name)
|
2021-01-19 16:10:45 -05:00
|
|
|
categories = @categories
|
|
|
|
categories.merge!(@include_categories_for_file[file_name]) if @include_categories_for_file[file_name].present?
|
|
|
|
categories.keys.find do |category|
|
2016-06-24 15:43:46 -04:00
|
|
|
File.exist?(File.join(category_directory(category), file_name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|