Class: Brakeman::Rescanner

Inherits:
Scanner
  • Object
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
IGNORE_PATTERN =
/\.(md|txt|js|ts|tsx|json|scss|css|xml|ru|png|jpg|pdf|gif|svg|webm|ttf|sql)$/

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

Instance Attribute Summary

Attributes inherited from Scanner

#options

Instance Method Summary collapse

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 inherited from Scanner

#detect_file_types, #file_cache, #guess_rails_version, #index_call_sites, #parse_files, #parse_ruby_file, #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_step, #process_step_file, #process_template, #process_template_data_flows, #process_templates, #report_progress, #support_rescanning?, #track_progress, #tracker

Constructor Details

#initialize(options, processor, changed_files) ⇒ Rescanner

Create new Rescanner to scan changed files



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

def initialize options, processor, changed_files
  super(options)

  @old_tracker = processor.tracked_events

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

Instance Method Details

#ignorable?(path) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/brakeman/rescanner.rb', line 74

def ignorable? path
  path.relative.match? IGNORE_PATTERN
end

#recheckObject

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



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

def recheck
  rescan if @changes.nil?

  if @changes
    tracker.run_checks
    Brakeman.filter_warnings(tracker, options) # Actually sets ignored_filter
    Brakeman::RescanReport.new @old_results, tracker
  else
    # No changes, fake no new results
    Brakeman::RescanReport.new @old_results, @old_tracker
  end
end

#rescanObject

Rescans changed files



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
66
67
68
69
70
# File 'lib/brakeman/rescanner.rb', line 38

def rescan
  raise "Cannot rescan: set `support_rescanning: true`" unless @old_tracker.options[:support_rescanning]

  tracker.file_cache = @old_tracker.pristine_file_cache

  template_paths = []
  ruby_paths = []

  # Remove changed files from the cache.
  # Collect files to re-parse.
  @paths.each do |path|
    file_cache.delete path

    if path.exists?
      if path.relative.match? KNOWN_TEMPLATE_EXTENSIONS
        template_paths << path
      elsif path.relative.end_with? '.rb'
        ruby_paths << path
      end
    end
  end

  # Try to skip rescanning files that do not impact
  # Brakeman results
  if @paths.all? { |path| ignorable? path }
    @changes = false
  else
    @changes = true
    process(ruby_paths:, template_paths:)
  end

  self
end