Class: Brakeman::Rescanner

Inherits:
Scanner show all
Includes:
Util
Defined in:
lib/brakeman/rescanner.rb

Overview

Class for rescanning changed files after an initial scan

Constant Summary collapse

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

Constants included from Util

Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_ENV, Util::REQUEST_PARAMETERS, Util::REQUEST_PARAMS, Util::SESSION, Util::SESSION_SEXP

Constants inherited from Scanner

Scanner::RUBY_1_9

Instance Attribute Summary

Attributes inherited from Scanner

#options

Instance Method Summary collapse

Methods included from Util

#array?, #block?, #call?, #camelize, #contains_class?, #context_for, #cookies?, #false?, #file_by_name, #file_for, #github_url, #hash?, #hash_access, #hash_insert, #hash_iterate, #integer?, #make_call, #node_type?, #number?, #params?, #pluralize, #regexp?, #relative_path, #request_env?, #request_value?, #result?, #set_env_defaults, #sexp?, #string?, #symbol?, #table_to_csv, #template_path_to_name, #true?, #truncate_table, #underscore

Methods inherited from Scanner

#guess_rails_version, #index_call_sites, #parse_files, #parse_ruby, #process, #process_config, #process_controller, #process_controller_data_flows, #process_controllers, #process_gems, #process_initializer, #process_initializers, #process_lib, #process_libs, #process_model, #process_models, #process_routes, #process_template, #process_template_data_flows, #process_templates, #report_progress, #track_progress, #tracker

Constructor Details

#initialize(options, processor, changed_files) ⇒ Rescanner

Create new Rescanner to scan changed files


14
15
16
17
18
19
20
21
# File 'lib/brakeman/rescanner.rb', line 14

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

  @paths = changed_files.map {|f| @app_tree.expand_path(f) }
  @old_results = tracker.filtered_warnings  #Old warnings from previous scan
  @changes = nil                 #True if files had to be rescanned
  @reindex = Set.new
end

Instance Method Details

#file_type(path) ⇒ Object

Guess at what kind of file the path contains


327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/brakeman/rescanner.rb', line 327

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\/.+\.rb/
    :config
  when /Gemfile/
    :gemfile
  else
    :unknown
  end
end

#parse_ruby_files(list) ⇒ Object


404
405
406
407
408
409
# File 'lib/brakeman/rescanner.rb', line 404

def parse_ruby_files list
  paths = list.select { |path| @app_tree.path_exists? path }
  file_parser = Brakeman::FileParser.new(tracker, @app_tree)
  file_parser.parse_files paths, :rescan
  file_parser.file_list[:rescan]
end

#recheckObject

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


25
26
27
28
29
30
31
# File 'lib/brakeman/rescanner.rb', line 25

def recheck
  rescan if @changes.nil?

  tracker.run_checks if @changes

  Brakeman::RescanReport.new @old_results, tracker
end

#remove_deleted_file(path) ⇒ Object

Check controllers, templates, models and libs for data from file and delete it.


304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/brakeman/rescanner.rb', line 304

def remove_deleted_file path
  deleted = false

  [:controllers, :models, :libs].each do |collection|
    tracker.send(collection).delete_if do |name, data|
      if data[:files].include?(path)
        deleted = true
        true
      end
    end
  end

  tracker.templates.delete_if do |name, data|
    if data[:file] == path
      deleted = true
      true
    end
  end

  deleted
end

#rescanObject

Rescans changed files


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
59
60
61
62
63
64
65
# File 'lib/brakeman/rescanner.rb', line 34

def rescan
  tracker.template_cache.clear

  paths_by_type = {}

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

  @paths.each do |path|
    type = file_type(path)
    paths_by_type[type] << path unless type == :unknown
  end

  @changes = false

  SCAN_ORDER.each do |type|
    paths_by_type[type].each do |path|
      Brakeman.debug "Rescanning #{path} as #{type}"

      if rescan_file path, type
        @changes = true
      end
    end
  end

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

  self
end

#rescan_controller(path) ⇒ Object


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/brakeman/rescanner.rb', line 103

def rescan_controller path
  controller = tracker.reset_controller path
  paths = controller.nil? ? [path] : controller[:files]
  parse_ruby_files(paths).each do |astfile|
    process_controller astfile
  end

  #Process data flow and template rendering
  #from the controller
  tracker.controllers.each do |name, controller|
    if controller[:files].include?(path)
      tracker.templates.each do |template_name, template|
        next unless template[:caller]
        unless template[:caller].grep(/^#{name}#/).empty?
          tracker.reset_template template_name
        end
      end

      controller[:src].each_value do |src|
        @processor.process_controller_alias controller[:name], src
      end
    end
  end

  @reindex << :templates << :controllers
end

#rescan_deleted_file(path, type) ⇒ Object

Handle rescanning when a file is deleted


239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/brakeman/rescanner.rb', line 239

def rescan_deleted_file path, type
  case type
  when :controller
    rescan_controller path
  when :template
    rescan_deleted_template path
  when :model
    rescan_model path
  when :lib
    rescan_deleted_lib path
  when :initializer
    rescan_deleted_initializer path
  else
    if remove_deleted_file path
      return true
    else
      Brakeman.notify "Ignoring deleted file: #{path}"
    end
  end

  true
end

#rescan_deleted_initializer(path) ⇒ Object


298
299
300
# File 'lib/brakeman/rescanner.rb', line 298

def rescan_deleted_initializer path
  tracker.initializers.delete Pathname.new(path).basename.to_s
end

#rescan_deleted_lib(path) ⇒ Object


285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/brakeman/rescanner.rb', line 285

def rescan_deleted_lib path
  deleted_lib = nil

  tracker.libs.delete_if do |name, lib|
    if lib[:files].include?(path)
      deleted_lib = lib
      true
    end
  end

  rescan_mixin deleted_lib if deleted_lib
end

#rescan_deleted_template(path) ⇒ Object


262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/brakeman/rescanner.rb', line 262

def rescan_deleted_template path
  return unless path.match KNOWN_TEMPLATE_EXTENSIONS

  template_name = template_path_to_name(path)

  #Remove template
  tracker.reset_template template_name

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

  #Remove any rendered versions, or partials rendered from it
  tracker.templates.delete_if do |name, template|
    if template[:file] == path
      true
    elsif template[:file].nil?
      name = name.to_s

      name.match(rendered_from_controller) or name.match(rendered_from_view)
    end
  end
end

#rescan_file(path, type = nil) ⇒ Object

Rescans a single file


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
96
97
98
99
100
101
# File 'lib/brakeman/rescanner.rb', line 68

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

  unless @app_tree.path_exists?(path)
    return rescan_deleted_file path, type
  end

  case type
  when :controller
    rescan_controller path
  when :template
    rescan_template path
  when :model
    rescan_model path
  when :lib
    rescan_lib path
  when :config
    process_config
  when :initializer
    rescan_initializer path
  when :routes
    rescan_routes
  when :gemfile
    if tracker.config[:gems][:rails_xss] and tracker.config[:escape_html]
      tracker.config[:escape_html] = false
    end

    process_gems
  else
    return false #Nothing to do, file hopefully does not need to be rescanned
  end

  true
end

#rescan_initializer(path) ⇒ Object


232
233
234
235
236
# File 'lib/brakeman/rescanner.rb', line 232

def rescan_initializer path
  parse_ruby_files([path]).each do |astfile|
    process_initializer astfile
  end
end

#rescan_lib(path) ⇒ Object


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/brakeman/rescanner.rb', line 202

def rescan_lib path
  lib = tracker.reset_lib path
  paths = lib.nil? ? [path] : lib[:files]
  parse_ruby_files(paths).each do |astfile|
    process_lib astfile
  end

  lib = nil

  tracker.libs.each do |name, library|
    if library[:files].include?(path)
      lib = library
      break
    end
  end

  rescan_mixin lib if lib
end

#rescan_mixin(lib) ⇒ Object


350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/brakeman/rescanner.rb', line 350

def rescan_mixin lib
  method_names = []

  [:public, :private, :protected].each do |access|
    lib[access].each do |name, meth|
      method_names << name
    end
  end

  method_matcher = /##{method_names.map {|n| Regexp.escape(n.to_s)}.join('|')}$/

  to_rescan = []

  #Rescan controllers that mixed in library
  tracker.controllers.each do |name, controller|
    if controller[:includes].include? lib[:name]
      controller[:files].each do |path|
        unless @paths.include? path
          to_rescan << path
        end
      end
    end
  end

  to_rescan.each do |controller|
    tracker.reset_controller controller
    rescan_file controller
  end

  to_rescan = []

  #Check if a method from this mixin was used to render a template.
  #This is not precise, because a different controller might have the
  #same method...
  tracker.templates.each do |name, template|
    next unless template[:caller]

    unless template[:caller].grep(method_matcher).empty?
      name.to_s.match /^([^.]+)/

      original = tracker.templates[$1.to_sym]

      if original
        to_rescan << [name, original[:file]]
      end
    end
  end

  to_rescan.each do |template|
    tracker.reset_template template[0]
    rescan_file template[1]
  end
end

#rescan_model(path) ⇒ Object


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/brakeman/rescanner.rb', line 184

def rescan_model path
  num_models = tracker.models.length
  model = tracker.reset_model path
  paths = model.nil? ? [path] : model[:files]
  parse_ruby_files(paths).each do |astfile|
    process_model astfile.path, astfile.ast
  end

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

  @reindex << :models
end

#rescan_routesObject


221
222
223
224
225
226
227
228
229
230
# File 'lib/brakeman/rescanner.rb', line 221

def rescan_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_controller_data_flows
  @reindex << :controllers << :templates
end

#rescan_template(path) ⇒ Object


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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/brakeman/rescanner.rb', line 130

def rescan_template path
  return unless path.match KNOWN_TEMPLATE_EXTENSIONS and @app_tree.path_exists?(path)

  template_name = template_path_to_name(path)

  tracker.reset_template template_name
  fp = Brakeman::FileParser.new(tracker, @app_tree)
  template_parser = Brakeman::TemplateParser.new(tracker, fp)
  template_parser.parse_template path, @app_tree.read_path(path)
  process_template fp.file_list[:templates].first

  @processor.process_template_alias tracker.templates[template_name]

  rescan = Set.new

  template_matcher = /^Template:(.+)/
  controller_matcher = /^(.+Controller)#(.+)/
  template_name_matcher = /^#{template_name}\./

  #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?
      next unless template[:caller] and name.to_s.match(template_name_matcher)

      template[:caller].each do |from|
        if from.match(template_matcher)
          rescan << [:template, $1.to_sym]
        elsif from.match(controller_matcher)
          rescan << [:controller, $1.to_sym, $2.to_sym]
        end
      end
    end
  end

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

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

      rescan_template template[:file]
    end
  end

  @reindex << :templates
end