Class: Brakeman::Rescanner

Inherits:
Scanner
  • Object
show all
Defined in:
lib/brakeman/rescanner.rb

Overview

Class for rescanning changed files after an initial scan

Constant Summary

SCAN_ORDER =
[:config, :gemfile, :initializer, :lib, :routes, :template,
:model, :controller]

Constants inherited from Scanner

Scanner::RUBY_1_9

Instance Attribute Summary

Attributes inherited from Scanner

#options

Instance Method Summary (collapse)

Methods inherited from Scanner

#index_call_sites, #parse_ruby, #process, #process_config, #process_controller, #process_controllers, #process_gems, #process_initializer, #process_initializers, #process_lib, #process_libs, #process_model, #process_models, #process_routes, #process_template, #process_templates, #template_path_to_name, #tracker

Constructor Details

- (Rescanner) initialize(options, processor, changed_files)

Create new Rescanner to scan changed files



10
11
12
13
14
15
16
17
# File 'lib/brakeman/rescanner.rb', line 10

def initialize options, processor, changed_files
  super(options, processor)

  @paths = changed_files         #Files to rescan
  @old_results = tracker.checks  #Old warnings from previous scan
  @changes = nil                 #True if files had to be rescanned
  @reindex = Set.new
end

Instance Method Details

- (Object) file_type(path)

Guess at what kind of file the path contains



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/brakeman/rescanner.rb', line 178

def file_type path
  case path
  when /\/app\/controllers/
    :controller
  when /\/app\/views/
    :template
  when /\/app\/models/
    :model
  when /\/lib/
    :lib
  when /\/config\/initializers/
    :initializer
  when /config\/routes\.rb/
    :routes
  when /\/config/
    :config
  when /Gemfile/
    :gemfile
  else
    :unknown
  end
end

- (Object) recheck

Runs checks. Will rescan files if they have not already been scanned



21
22
23
24
25
26
27
# File 'lib/brakeman/rescanner.rb', line 21

def recheck
  rescan if @changes.nil?

  tracker.run_checks if @changes

  Brakeman::RescanReport.new @old_results, tracker.checks
end

- (Object) rescan

Rescans changed files



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/brakeman/rescanner.rb', line 30

def rescan
  tracker.template_cache.clear

  paths_by_type = {}

  SCAN_ORDER.each do |type|
    paths_by_type[type] = []
  end

  @paths.each do |path|
    paths_by_type[file_type path] << path
  end

  @changes = false

  SCAN_ORDER.each do |type|
    paths_by_type[type].each do |path|
      if rescan_file path, type
        @changes = true
      end
    end
  end

  if @changes and not @reindex.empty?
    tracker.reindex_call_sites @reindex
  end

  self
end

- (Object) rescan_controller(path)



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/brakeman/rescanner.rb', line 97

def rescan_controller path
  #Process source
  process_controller path

  #Process data flow and template rendering
  #from the controller
  tracker.controllers.each do |name, controller|
    if controller[:file] == path
      tracker.templates.keys.each do |template_name|
        if template_name.to_s.match /(.+)\.#{name}#/
          tracker.templates.delete template_name
        end
      end

      @processor.process_controller_alias controller[:src]
    end
  end
end

- (Object) rescan_file(path, type = nil)

Rescans a single file



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/brakeman/rescanner.rb', line 61

def rescan_file path, type = nil
  type ||= file_type path

  case type
  when :controller
    rescan_controller path
    @reindex << :controllers << :templates
  when :template
    rescan_template path
    @reindex << :templates
  when :model
    rescan_model path
  when :lib
    process_library path
  when :config
    process_config
  when :initializer
    process_initializer path
  when :routes
    # Routes affect which controller methods are treated as actions
    # which affects which templates are rendered, so routes, controllers,
    # and templates rendered from controllers must be rescanned
    tracker.reset_routes
    tracker.reset_templates :only_rendered => true
    process_routes
    process_controllers
    @reindex << :controllers << :templates
  when :gemfile
    process_gems
  else
    return false #Nothing to do, file hopefully does not need to be rescanned
  end

  true
end

- (Object) rescan_model(path)



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/brakeman/rescanner.rb', line 162

def rescan_model path
  num_models = tracker.models.length
  tracker.reset_model path
  process_model path if File.exists? path

  #Only need to rescan other things if a model is added or removed
  if num_models != tracker.models.length
    process_templates
    process_controllers
    @reindex << :templates << :controllers
  end

  @reindex << :models
end

- (Object) rescan_template(path)



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/brakeman/rescanner.rb', line 116

def rescan_template path
  template_name = template_path_to_name(path)

  tracker.reset_template template_name
  process_template path

  @processor.process_template_alias tracker.templates[template_name]

  rescan = Set.new

  rendered_from_controller = /^#{template_name}\.(.+Controller)#(.+)/
  rendered_from_view = /^#{template_name}\.Template:(.+)/

  #Search for processed template and process it.
  #Search for rendered versions of template and re-render (if necessary)
  tracker.templates.each do |name, template|
    if template[:file] == path or template[:file].nil?
     name = name.to_s

     if name.match(rendered_from_controller)
       #Rendered from controller, so reprocess controller

       rescan << [:controller, $1.to_sym, $2.to_sym]
     elsif name.match(rendered_from_view)
       #Rendered from another template, so reprocess that template

       rescan << [:template, $1.to_sym]
     end
    end
  end

  rescan.each do |r|
    if r[0] == :controller
      controller = tracker.controllers[r[1]]

      unless @paths.include? controller[:file]
        @processor.process_controller_alias controller[:src], r[2]
      end
    elsif r[0] == :template
      template = tracker.templates[r[1]]

      rescan_template template[:file]
    end
  end
end