Class: Brakeman::FileTypeDetector

Inherits:
BaseProcessor show all
Defined in:
lib/brakeman/processors/lib/file_type_detector.rb

Constant Summary collapse

MODEL_CLASSES =
[
  :'ActiveRecord::Base',
  :ApplicationRecord
]

Constants inherited from BaseProcessor

BaseProcessor::IGNORE

Constants included from Util

Util::ALL_COOKIES, Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::DIR_CONST, Util::LITERALS, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_COOKIES, Util::REQUEST_ENV, Util::REQUEST_PARAMETERS, Util::REQUEST_PARAMS, Util::REQUEST_REQUEST_PARAMETERS, Util::SAFE_LITERAL, Util::SESSION, Util::SESSION_SEXP, Util::SIMPLE_LITERALS

Constants inherited from SexpProcessor

SexpProcessor::VERSION

Instance Attribute Summary

Attributes inherited from SexpProcessor

#context, #env, #expected

Instance Method Summary collapse

Methods inherited from BaseProcessor

#find_render_type, #ignore, #make_inline_render, #make_render, #make_render_in_view, #process_arglist, #process_attrasgn, #process_block, #process_cdecl, #process_default, #process_dstr, #process_evstr, #process_file, #process_hash, #process_if, #process_ignore, #process_iter, #process_lasgn, #process_scope

Methods included from Util

#all_literals?, #array?, #block?, #call?, #camelize, #class_name, #constant?, #contains_class?, #cookies?, #dir_glob?, #false?, #hash?, #hash_access, #hash_insert, #hash_iterate, #hash_values, #integer?, #kwsplat?, #literal?, #make_call, #node_type?, #number?, #params?, #pluralize, #rails_version, #recurse_check?, #regexp?, #remove_kwsplat, #request_headers?, #request_value?, #result?, #safe_literal, #safe_literal?, #safe_literal_target?, #set_env_defaults, #sexp?, #simple_literal?, #string?, #string_interp?, #symbol?, #template_path_to_name, #true?, #underscore

Methods included from ProcessorHelper

#current_file, #process_all, #process_all!, #process_call_args, #process_call_defn?, #process_module

Methods inherited from SexpProcessor

#in_context, #process, processors, #scope

Constructor Details

#initializeFileTypeDetector

Returns a new instance of FileTypeDetector.



3
4
5
6
# File 'lib/brakeman/processors/lib/file_type_detector.rb', line 3

def initialize
  super(nil)
  reset
end

Instance Method Details

#detect_type(file) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/brakeman/processors/lib/file_type_detector.rb', line 8

def detect_type(file)
  reset
  process(file.ast)

  if @file_type.nil?
    @file_type = guess_from_path(file.path.relative)
  end

  @file_type || :libs
end

#guess_from_path(path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/brakeman/processors/lib/file_type_detector.rb', line 39

def guess_from_path path
  case
  when path.include?('app/models')
    :models
  when path.include?('app/controllers')
    :controllers
  when path.include?('config/initializers')
    :initializers
  when path.include?('lib/')
    :libs
  when path.match?(%r{config/environments/(?!production\.rb)$})
    :skip
  when path.match?(%r{environments/production\.rb$})
    :skip
  when path.match?(%r{application\.rb$})
    :skip
  end
end

#process_class(exp) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/brakeman/processors/lib/file_type_detector.rb', line 24

def process_class exp
  name = class_name(exp.class_name)
  parent = class_name(exp.parent_name)

  if name.match(/Controller$/)
    @file_type = :controllers
    return exp
  elsif MODEL_CLASSES.include? parent
    @file_type = :models
    return exp
  end

  super
end