Sha256: 65a3165b9c846243fe6d64bd93870593027cd2ecba4f02453dd133b2e9b949fe

Contents?: true

Size: 1.55 KB

Versions: 5

Compression:

Stored size: 1.55 KB

Contents

require 'pronto'
require 'brakeman'

module Pronto
  class Brakeman < Runner
    def run
      files = ruby_patches.map do |patch|
        patch.new_file_full_path.relative_path_from(repo_path).to_s
      end

      return [] unless files.any?

      output = ::Brakeman.run(app_path: repo_path,
                              output_formats: [:to_s],
                              only_files: files)
      messages_for(ruby_patches, output).compact
    rescue ::Brakeman::NoApplication
      []
    end

    def messages_for(ruby_patches, output)
      output.filtered_warnings.map do |warning|
        patch = patch_for_warning(ruby_patches, warning)

        next unless patch
        line = patch.added_lines.find do |added_line|
          added_line.new_lineno == warning.line
        end

        new_message(line, warning) if line
      end
    end

    def new_message(line, warning)
      Message.new(line.patch.delta.new_file[:path], line,
                  severity_for_confidence(warning.confidence),
                  "Possible security vulnerability: #{warning.message}",
                  nil, self.class)
    end

    def severity_for_confidence(confidence_level)
      case confidence_level
      when 0 # Brakeman High confidence
        :fatal
      when 1 # Brakeman Medium confidence
        :warning
      else # Brakeman Low confidence (and other possibilities)
        :info
      end
    end

    def patch_for_warning(ruby_patches, warning)
      ruby_patches.find do |patch|
        patch.new_file_full_path.to_s == warning.file
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pronto-brakeman-0.10.0 lib/pronto/brakeman.rb
pronto-brakeman-0.9.1 lib/pronto/brakeman.rb
pronto-brakeman-0.9.0 lib/pronto/brakeman.rb
pronto-brakeman-0.8.0 lib/pronto/brakeman.rb
pronto-brakeman-0.7.0 lib/pronto/brakeman.rb