Class: TaintedLove::Reporter::StdoutReporter

Inherits:
Base
  • Object
show all
Defined in:
lib/tainted_love/reporter/stdout_reporter.rb

Overview

Reporter that outputs warnings in the console

Instance Attribute Summary collapse

Attributes inherited from Base

#warnings

Instance Method Summary collapse

Methods inherited from Base

#store_warning

Constructor Details

#initializeStdoutReporter

Returns a new instance of StdoutReporter.



9
10
11
12
13
14
# File 'lib/tainted_love/reporter/stdout_reporter.rb', line 9

def initialize
  super

  @stack_trace_size = 5
  @app_path = Dir.pwd
end

Instance Attribute Details

#app_pathObject

Returns the value of attribute app_path.



7
8
9
# File 'lib/tainted_love/reporter/stdout_reporter.rb', line 7

def app_path
  @app_path
end

#stack_trace_sizeObject

Returns the value of attribute stack_trace_size.



7
8
9
# File 'lib/tainted_love/reporter/stdout_reporter.rb', line 7

def stack_trace_size
  @stack_trace_size
end

Instance Method Details

#add_warning(warning) ⇒ Object



16
17
18
19
20
# File 'lib/tainted_love/reporter/stdout_reporter.rb', line 16

def add_warning(warning)
  puts
  format_warning(warning)
  puts
end

#format_line(line) ⇒ Object



46
47
48
# File 'lib/tainted_love/reporter/stdout_reporter.rb', line 46

def format_line(line)
  line[:file].sub(Dir.pwd, '.') + ':' + line[:line_number].to_s + ' in ' + line[:method]
end

#format_warning(warning) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tainted_love/reporter/stdout_reporter.rb', line 22

def format_warning(warning)
  puts '[!] TaintedLove'
  puts "#{warning.stack_trace.trace_hash[0...8]} #{warning.message} [#{warning.tags.join(', ')}]"

  tainted_input = if warning.tainted_input.size < 100
    warning.tainted_input.inspect
  else
    warning.tainted_input.inspect[0..100] + '...'
  end

  puts 'Tainted input: ' + tainted_input
  puts 'Taint tags: ' + warning.tainted_input.tainted_love_tags.uniq.inspect

  warning.stack_trace.lines.take(@stack_trace_size).each do |line|
    puts format_line(line)

    next unless line[:file].start_with?(@app_path)

    File.read(line[:file]).lines.each_with_index.drop([0, line[:line_number] - 2].max).take(3).each do |(code, n)|
      puts "| #{n + 1}\t#{code}"
    end
  end
end