Sha256: b44a90189bb35d51f8f08cab76bed8456cce51baddbc934063dc728ca556ddac

Contents?: true

Size: 1.64 KB

Versions: 15

Compression:

Stored size: 1.64 KB

Contents

# extracting the diff logic to it's own class for consistency. Currently handles
# an array of Brakeman::Warnings or plain hash representations.  
class Brakeman::Differ
  DEFAULT_HASH = {:new => [], :fixed => []}
  OLD_WARNING_KEYS = [:warning_type, :location, :code, :message, :file, :link, :confidence, :user_input]
  attr_reader :old_warnings, :new_warnings

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

  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

  # 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.
  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

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

Version data entries

15 entries across 12 versions & 4 rubygems

Version Path
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/brakeman-4.7.2/lib/brakeman/differ.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/brakeman-4.7.0/lib/brakeman/differ.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/brakeman-4.7.1/lib/brakeman/differ.rb
brakeman-4.7.2 lib/brakeman/differ.rb
brakeman-lib-4.7.2 lib/brakeman/differ.rb
brakeman-min-4.7.2 lib/brakeman/differ.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/brakeman-4.7.0/lib/brakeman/differ.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/brakeman-4.7.1/lib/brakeman/differ.rb
brakeman-4.7.1 lib/brakeman/differ.rb
brakeman-lib-4.7.1 lib/brakeman/differ.rb
brakeman-min-4.7.1 lib/brakeman/differ.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/brakeman-4.7.0/lib/brakeman/differ.rb
brakeman-4.7.0 lib/brakeman/differ.rb
brakeman-lib-4.7.0 lib/brakeman/differ.rb
brakeman-min-4.7.0 lib/brakeman/differ.rb