Class: LintFu::CLI::Prune

Inherits:
Command
  • Object
show all
Defined in:
lib/lint_fu/cli/prune.rb

Constant Summary collapse

RUBY_FILE_EXT =
/\.rb[a-z]?/

Instance Method Summary collapse

Methods inherited from Command

#initialize

Constructor Details

This class inherits a constructor from LintFu::CLI::Command

Instance Method Details

#runObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
# File 'lib/lint_fu/cli/prune.rb', line 5

def run
  #Build a model of the application we are scanning.
  timed("Build a model of the application") do
    builder = LintFu::Plugins::Rails.context_builder_for(self.app_root)
    builder.build
    @application = builder.eide.first
  end

  #Using the model we built, scan the controllers for security bugs.
  timed("Scan the application") do
    @scan = LintFu::Scan.new(self.app_root)
    #TODO generalize/abstract this, same as we did for context builders
    builder = LintFu::Plugins::Rails.issue_builder_for(self.app_root)
    builder.build(@application, @scan)
  end

  blessings       = []
  blessing_ranges = nil
  useless         = []

  timed("Find all annotations") do
    recurse(self.app_root, blessings)

    blessing_ranges = blessings.map do |triple|
      file, line, comment = triple[0], triple[1], triple[2]
      next LintFu::FileRange.new(file, line, line, comment)
    end
  end

  timed("Cross-check annotations against issues") do
    issue_ranges = @scan.issues.map do |issue|
      issue.sexp.preceding_comment_range
    end
    issue_ranges.compact!

    blessing_ranges.each do |b|
      useless << b unless issue_ranges.any? { |r| r.include?(b) }
    end
  end

  say "Found #{useless.size} extraneous annotations (out of #{blessings.size} total)."

  useless.each do |range|
    filename = File.relative_path(self.app_root, range.filename)
    say "#{filename}:#{range.line}"
  end

  say "WARNING: I did not actually prune these; you need to do it yourself!!"

  return 0
end