Sha256: 09f173888e29f6df0f0a33ac377f1c76de041531b3acdaebce12cf5657b5514a

Contents?: true

Size: 1.45 KB

Versions: 11

Compression:

Stored size: 1.45 KB

Contents

module NdrError
  # Mixin to help with fuzzing of exception messages/traces.
  module Fuzzing
    def fuzz(description, backtrace, parent_print = nil)
      Digest::MD5.hexdigest([
        fuzz_description(description),
        fuzz_backtrace(backtrace),
        parent_print
      ].compact.join)
    end

    private

    # Prepare a fuzzed description:
    def fuzz_description(description)
      # Apply the fuzzers sequentially:
      NdrError.description_fuzzers.inject(description) { |a, e| e.call(a) }
    end

    # Fuzz a backtrace:
    #   * independent of deployment paths
    #   * independent of line numbers
    def fuzz_backtrace(backtrace)
      backtrace.map { |line| fuzz_line(line) }.join("\n")
    end

    def fuzz_line(line)
      line = line.sub(Rails.root.to_s, 'Rails.root')

      # Remove gem version numbers from backtrace
      line.sub!(Regexp.new('/gems/([^/]*-)[0-9][0-9.]*/'), '/gems/\1/')

      # Remove GEM_PATH / LOAD_PATH differences:
      sub_paths!(line)

      # Remove Rails compiled callback & template identifiers:
      line.gsub!(Regexp.new('___?[0-9][0-9_]*[0-9]'), '__COMPILED_ID')

      # Remove line numbers:
      line.gsub!(/:(\d+):/, '')

      line
    end

    def sub_paths!(line)
      Gem.path.each do |path|
        line.sub!(Regexp.new('^' + Regexp.escape(path)), 'Gem.path')
      end

      $LOAD_PATH.each do |path|
        line.sub!(Regexp.new('^' + Regexp.escape(path)), '$LOAD_PATH')
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ndr_error-2.4.1 lib/ndr_error/fuzzing.rb
ndr_error-2.4.0 lib/ndr_error/fuzzing.rb
ndr_error-2.3.2 lib/ndr_error/fuzzing.rb
ndr_error-2.3.1 lib/ndr_error/fuzzing.rb
ndr_error-2.3.0 lib/ndr_error/fuzzing.rb
ndr_error-2.2.0 lib/ndr_error/fuzzing.rb
ndr_error-2.1.0 lib/ndr_error/fuzzing.rb
ndr_error-2.0.3 lib/ndr_error/fuzzing.rb
ndr_error-2.0.2 lib/ndr_error/fuzzing.rb
ndr_error-2.0.1 lib/ndr_error/fuzzing.rb
ndr_error-2.0.0 lib/ndr_error/fuzzing.rb