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.

Constant Summary collapse

DEFAULT_HASH =
{:new => [], :fixed => []}
OLD_WARNING_KEYS =
[:warning_type, :location, :code, :message, :file, :link, :confidence, :user_input]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_warnings, old_warnings) ⇒ Differ

Returns a new instance of Differ.



8
9
10
11
# File 'lib/brakeman/differ.rb', line 8

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.



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

def new_warnings
  @new_warnings
end

#old_warningsObject (readonly)

Returns the value of attribute old_warnings.



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

def old_warnings
  @old_warnings
end

Instance Method Details

#diffObject



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

def diff
  # get the type of elements
  return DEFAULT_HASH if @new_warnings.empty?

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

  second_pass(warnings)
end

#fingerprint(warning) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/brakeman/differ.rb', line 47

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.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/brakeman/differ.rb', line 27

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