class Brakeman::AppTree
Constants
- EXCLUDED_PATHS
- VIEW_EXTENSIONS
Attributes
root[R]
Public Class Methods
from_options(options)
click to toggle source
# File lib/brakeman/app_tree.rb, line 10 def self.from_options(options) root = File.expand_path options[:app_path] # Convert files into Regexp for matching init_options = {} if options[:skip_files] init_options[:skip_files] = regex_for_paths(options[:skip_files]) end if options[:only_files] init_options[:only_files] = regex_for_paths(options[:only_files]) end init_options[:additional_libs_path] = options[:additional_libs_path] init_options[:engine_paths] = options[:engine_paths] init_options[:skip_vendor] = options[:skip_vendor] new(root, init_options) end
new(root, init_options = {})
click to toggle source
# File lib/brakeman/app_tree.rb, line 57 def initialize(root, init_options = {}) @root = root @project_root_path = Pathname.new(@root) @skip_files = init_options[:skip_files] @only_files = init_options[:only_files] @additional_libs_path = init_options[:additional_libs_path] || [] @engine_paths = init_options[:engine_paths] || [] @absolute_engine_paths = @engine_paths.select { |path| path.start_with?(File::SEPARATOR) } @relative_engine_paths = @engine_paths - @absolute_engine_paths @skip_vendor = init_options[:skip_vendor] @gemspec = nil @root_search_pattern = nil end
Private Class Methods
regex_for_paths(paths)
click to toggle source
Accepts an array of filenames and paths with the following format and returns a Regexp to match them:
* "path1/file1.rb" - Matches a specific filename in the project directory. * "path1/" - Matches any path that contains "path1" in the project directory. * "/path1/ - Matches any path that is rooted at "path1" in the project directory.
# File lib/brakeman/app_tree.rb, line 34 def self.regex_for_paths(paths) path_regexes = paths.map do |f| # If path ends in a file separator then we assume it is a path rather # than a filename. if f.end_with?(File::SEPARATOR) # If path starts with a file separator then we assume that they # want the project relative path to start with this path prefix. if f.start_with?(File::SEPARATOR) "\\A#{Regexp.escape f}" # If it ends in a file separator, but does not begin with a file # separator then we assume the path can match any path component in # the project. else Regexp.escape f end else "#{Regexp.escape f}\\z" end end Regexp.new("(?:" << path_regexes.join("|") << ")") end
Public Instance Methods
controller_paths()
click to toggle source
# File lib/brakeman/app_tree.rb, line 109 def controller_paths @controller_paths ||= prioritize_concerns(find_paths("app/**/controllers")) end
exists?(path)
click to toggle source
# File lib/brakeman/app_tree.rb, line 93 def exists?(path) if path.is_a? Brakeman::FilePath path.exists? else File.exist?(File.join(@root, path)) end end
expand_path(path)
click to toggle source
Should only be used by Brakeman::FilePath
. Use AppTree#file_path(path)
.absolute instead.
# File lib/brakeman/app_tree.rb, line 78 def expand_path(path) File.expand_path(path, @root) end
file_path(path)
click to toggle source
Create a new Brakeman::FilePath
# File lib/brakeman/app_tree.rb, line 72 def file_path(path) Brakeman::FilePath.from_app_tree(self, path) end
gemspec()
click to toggle source
# File lib/brakeman/app_tree.rb, line 133 def gemspec return @gemspec unless @gemspec.nil? gemspecs = Dir.glob(File.join(@root, "*.gemspec")) if gemspecs.length > 1 or gemspecs.empty? @gemspec = false else @gemspec = file_path(File.basename(gemspecs.first)) end end
initializer_paths()
click to toggle source
# File lib/brakeman/app_tree.rb, line 105 def initializer_paths @initializer_paths ||= prioritize_concerns(find_paths("config/initializers")) end
layout_exists?(name)
click to toggle source
# File lib/brakeman/app_tree.rb, line 122 def layout_exists?(name) !Dir.glob("#{root_search_pattern}app/views/layouts/#{name}.html.{erb,haml,slim}").empty? end
lib_paths()
click to toggle source
# File lib/brakeman/app_tree.rb, line 126 def lib_paths @lib_files ||= find_paths("lib").reject { |path| path.relative.include? "/generators/" or path.relative.include? "lib/tasks/" or path.relative.include? "lib/templates/" } + find_additional_lib_paths + find_helper_paths + find_job_paths end
model_paths()
click to toggle source
# File lib/brakeman/app_tree.rb, line 113 def model_paths @model_paths ||= prioritize_concerns(find_paths("app/**/models")) end
relative_path(path)
click to toggle source
Should only be used by Brakeman::FilePath
Use AppTree#file_path(path)
.relative instead.
# File lib/brakeman/app_tree.rb, line 84 def relative_path(path) pname = Pathname.new path if path and not path.empty? and pname.absolute? pname.relative_path_from(Pathname.new(self.root)).to_s else path end end
ruby_file_paths()
click to toggle source
# File lib/brakeman/app_tree.rb, line 101 def ruby_file_paths find_paths(".").uniq end
template_paths()
click to toggle source
# File lib/brakeman/app_tree.rb, line 117 def template_paths @template_paths ||= find_paths(".", "*.{#{VIEW_EXTENSIONS}}") + find_paths("**", "*.{erb,haml,slim}").reject { |path| File.basename(path).count(".") > 1 } end
Private Instance Methods
convert_to_file_paths(paths)
click to toggle source
# File lib/brakeman/app_tree.rb, line 246 def convert_to_file_paths paths paths.map { |path| file_path(path) } end
find_additional_lib_paths()
click to toggle source
# File lib/brakeman/app_tree.rb, line 155 def find_additional_lib_paths @additional_libs_path.collect{ |path| find_paths path }.flatten end
find_helper_paths()
click to toggle source
# File lib/brakeman/app_tree.rb, line 147 def find_helper_paths find_paths "app/helpers" end
find_job_paths()
click to toggle source
# File lib/brakeman/app_tree.rb, line 151 def find_job_paths find_paths "app/jobs" end
find_paths(directory, extensions = ".rb")
click to toggle source
# File lib/brakeman/app_tree.rb, line 159 def find_paths(directory, extensions = ".rb") select_files(glob_files(directory, "*", extensions).sort) end
glob_files(directory, name, extensions = ".rb")
click to toggle source
# File lib/brakeman/app_tree.rb, line 163 def glob_files(directory, name, extensions = ".rb") pattern = "#{root_search_pattern}#{directory}/**/#{name}#{extensions}" Dir.glob(pattern) end
match_path(files, path)
click to toggle source
# File lib/brakeman/app_tree.rb, line 218 def match_path files, path absolute_path = Pathname.new(path) # relative root never has a leading separator. But, we use a leading # separator in a @skip_files entry to imply that a directory is # "absolute" with respect to the project directory. project_relative_path = File.join( File::SEPARATOR, absolute_path.relative_path_from(@project_root_path).to_s ) files.match(project_relative_path) end
prioritize_concerns(paths)
click to toggle source
# File lib/brakeman/app_tree.rb, line 242 def prioritize_concerns paths paths.partition { |path| path.relative.include? "concerns" }.flatten end
reject_global_excludes(paths)
click to toggle source
# File lib/brakeman/app_tree.rb, line 204 def reject_global_excludes(paths) paths.reject do |path| relative_path = path.relative if @skip_vendor and relative_path.include? 'vendor/' true else EXCLUDED_PATHS.any? do |excluded| relative_path.include? excluded end end end end
reject_skipped_files(paths)
click to toggle source
# File lib/brakeman/app_tree.rb, line 184 def reject_skipped_files(paths) return paths unless @skip_files paths.reject do |path| match_path @skip_files, path end end
root_search_pattern()
click to toggle source
# File lib/brakeman/app_tree.rb, line 231 def root_search_pattern return @root_search_pattern if @root_search_pattern abs = @absolute_engine_paths.to_a.map { |path| path.gsub(/#{File::SEPARATOR}+$/, '') } rel = @relative_engine_paths.to_a.map { |path| path.gsub(/#{File::SEPARATOR}+$/, '') } roots = ([@root] + abs).join(",") rel_engines = (rel + [""]).join("/,") @root_search_pattern = "{#{roots}}/{#{rel_engines}}" end
select_files(paths)
click to toggle source
# File lib/brakeman/app_tree.rb, line 169 def select_files(paths) paths = select_only_files(paths) paths = reject_skipped_files(paths) paths = convert_to_file_paths(paths) reject_global_excludes(paths) end
select_only_files(paths)
click to toggle source
# File lib/brakeman/app_tree.rb, line 176 def select_only_files(paths) return paths unless @only_files paths.select do |path| match_path @only_files, path end end