Class: Brakeman::Differ

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

Overview

extracting the diff logic to it's own class for consistency. Currently handles an array of Brakeman::Warnings or plain hash representations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_warnings, old_warnings) ⇒ Differ

Returns a new instance of Differ.


6
7
8
9
# File 'lib/brakeman/differ.rb', line 6

def initialize new_warnings, old_warnings
  @new_warnings = new_warnings
  @old_warnings = old_warnings
end

Instance Attribute Details

#new_warningsObject (readonly)

Returns the value of attribute new_warnings


4
5
6
# File 'lib/brakeman/differ.rb', line 4

def new_warnings
  @new_warnings
end

#old_warningsObject (readonly)

Returns the value of attribute old_warnings


4
5
6
# File 'lib/brakeman/differ.rb', line 4

def old_warnings
  @old_warnings
end

Instance Method Details

#diffObject


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

def diff
  warnings = {}
  warnings[:new] = @new_warnings - @old_warnings
  warnings[:fixed] = @old_warnings - @new_warnings

  second_pass(warnings)
end

#fingerprint(warning) ⇒ Object


42
43
44
45
46
47
48
# File 'lib/brakeman/differ.rb', line 42

def fingerprint(warning)
  if warning.is_a?(Brakeman::Warning)
    warning.fingerprint
  else
    warning[:fingerprint]
  end
end

#second_pass(warnings) ⇒ Object

second pass to cleanup any vulns which have changed in line number only. Given a list of new warnings, delete pairs of new/fixed vulns that differ only by line number.


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/brakeman/differ.rb', line 22

def second_pass(warnings)
  new_fingerprints = Set.new(warnings[:new].map(&method(:fingerprint)))
  fixed_fingerprints = Set.new(warnings[:fixed].map(&method(:fingerprint)))

  # Remove warnings which fingerprints are both in :new and :fixed
  shared_fingerprints = new_fingerprints.intersection(fixed_fingerprints)

  unless shared_fingerprints.empty?
    warnings[:new].delete_if do |warning|
      shared_fingerprints.include?(fingerprint(warning))
    end

    warnings[:fixed].delete_if do |warning|
      shared_fingerprints.include?(fingerprint(warning))
    end
  end

  warnings
end