Class: Brakeman::Warning

Inherits:
Object
  • Object
show all
Defined in:
lib/brakeman/warning.rb

Overview

The Warning class stores information about warnings

Constant Summary collapse

TEXT_CONFIDENCE =
[ "High", "Medium", "Weak" ]
OPTIONS =
{:called_from => :@called_from,
  :check => :@check,
  :class => :@class,
  :code => :@code,
  :confidence => :@confidence,
  :controller => :@controller,
  :file => :@file,
  :gem_info => :@gem_info,
  :line => :@line,
  :link_path => :@link_path,
  :message => :@message,
  :method => :@method,
  :model => :@model,
  :relative_path => :@relative_path,
  :template => :@template,
  :user_input => :@user_input,
  :warning_set => :@warning_set,
  :warning_type => :@warning_type
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Warning

options[:result] can be a result from Tracker#find_call. Otherwise, it can be nil.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/brakeman/warning.rb', line 36

def initialize options = {}
  @view_name = nil

  OPTIONS.each do |key, var|
    self.instance_variable_set(var, options[key])
  end

  result = options[:result]
  if result
    @code ||= result[:call]
    @file ||= result[:location][:file]

    if result[:location][:type] == :template #template result
      @template ||= result[:location][:template]
    else
      @class ||= result[:location][:class]
      @method ||= result[:location][:method]
    end
  end

  if @method.to_s =~ /^fake_filter\d+/
    @method = :before_filter
  end

  if @user_input.is_a? Brakeman::BaseCheck::Match
    @user_input_type = @user_input.type
    @user_input = @user_input.match
  elsif @user_input == false
    @user_input = nil
  end

  if not @line
    if @user_input and @user_input.respond_to? :line
      @line = @user_input.line
    elsif @code and @code.respond_to? :line
      @line = @code.line
    end
  end

  if @gem_info
    if @gem_info.is_a? Hash
      @line ||= @gem_info[:line]
      @file ||= @gem_info[:file]
    else
      # Fallback behavior returns just a string for the file name
      @file ||= @gem_info
    end
  end

  unless @warning_set
    if self.model
      @warning_set = :model
    elsif self.template
      @warning_set = :template
      @called_from = self.template.render_path
    elsif self.controller
      @warning_set = :controller
    else
      @warning_set = :warning
    end
  end

  if options[:warning_code]
    @warning_code = Brakeman::WarningCodes.code options[:warning_code]
  end

  Brakeman.debug("Warning created without warning code: #{options[:warning_code]}") unless @warning_code

  @format_message = nil
  @row = nil
end

Instance Attribute Details

#called_fromObject (readonly)

Returns the value of attribute called_from.



7
8
9
# File 'lib/brakeman/warning.rb', line 7

def called_from
  @called_from
end

#checkObject (readonly)

Returns the value of attribute check.



7
8
9
# File 'lib/brakeman/warning.rb', line 7

def check
  @check
end

#classObject (readonly)

Returns the value of attribute class.



7
8
9
# File 'lib/brakeman/warning.rb', line 7

def class
  @class
end

#codeObject

Returns the value of attribute code.



11
12
13
# File 'lib/brakeman/warning.rb', line 11

def code
  @code
end

#confidenceObject (readonly)

Returns the value of attribute confidence.



7
8
9
# File 'lib/brakeman/warning.rb', line 7

def confidence
  @confidence
end

#contextObject

Returns the value of attribute context.



11
12
13
# File 'lib/brakeman/warning.rb', line 11

def context
  @context
end

#controllerObject (readonly)

Returns the value of attribute controller.



7
8
9
# File 'lib/brakeman/warning.rb', line 7

def controller
  @controller
end

#fileObject

Returns the value of attribute file.



11
12
13
# File 'lib/brakeman/warning.rb', line 11

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line.



7
8
9
# File 'lib/brakeman/warning.rb', line 7

def line
  @line
end

#messageObject

Returns the value of attribute message.



11
12
13
# File 'lib/brakeman/warning.rb', line 11

def message
  @message
end

#methodObject (readonly)

Returns the value of attribute method.



7
8
9
# File 'lib/brakeman/warning.rb', line 7

def method
  @method
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/brakeman/warning.rb', line 7

def model
  @model
end

#relative_pathObject

Returns the value of attribute relative_path.



11
12
13
# File 'lib/brakeman/warning.rb', line 11

def relative_path
  @relative_path
end

#templateObject (readonly)

Returns the value of attribute template.



7
8
9
# File 'lib/brakeman/warning.rb', line 7

def template
  @template
end

#user_inputObject (readonly)

Returns the value of attribute user_input.



7
8
9
# File 'lib/brakeman/warning.rb', line 7

def user_input
  @user_input
end

#user_input_typeObject (readonly)

Returns the value of attribute user_input_type.



7
8
9
# File 'lib/brakeman/warning.rb', line 7

def user_input_type
  @user_input_type
end

#warning_codeObject (readonly)

Returns the value of attribute warning_code.



7
8
9
# File 'lib/brakeman/warning.rb', line 7

def warning_code
  @warning_code
end

#warning_setObject (readonly)

Returns the value of attribute warning_set.



7
8
9
# File 'lib/brakeman/warning.rb', line 7

def warning_set
  @warning_set
end

#warning_typeObject (readonly)

Returns the value of attribute warning_type.



7
8
9
# File 'lib/brakeman/warning.rb', line 7

def warning_type
  @warning_type
end

Instance Method Details

#eql?(other_warning) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/brakeman/warning.rb', line 112

def eql? other_warning
  self.hash == other_warning.hash
end

#fingerprintObject



201
202
203
204
205
206
207
208
# File 'lib/brakeman/warning.rb', line 201

def fingerprint
  loc = self.location
  location_string = loc && loc.sort_by { |k, v| k.to_s }.inspect
  warning_code_string = sprintf("%03d", @warning_code)
  code_string = @code.inspect

  Digest::SHA2.new(256).update("#{warning_code_string}#{code_string}#{location_string}#{@relative_path}#{self.confidence}").to_s
end

#format_code(strip = true) ⇒ Object

Return String of the code output from the OutputProcessor and stripped of newlines and tabs.



127
128
129
# File 'lib/brakeman/warning.rb', line 127

def format_code strip = true
  format_ruby self.code, strip
end

#format_messageObject

Return formatted warning message



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/brakeman/warning.rb', line 138

def format_message
  return @format_message if @format_message

  @format_message = self.message.dup

  if self.line
    @format_message << " near line #{self.line}"
  end

  if self.code
    @format_message << ": #{format_code}"
  end

  @format_message
end

#format_user_input(strip = true) ⇒ Object

Return String of the user input formatted and stripped of newlines and tabs.



133
134
135
# File 'lib/brakeman/warning.rb', line 133

def format_user_input strip = true
  format_ruby self.user_input, strip
end

#hashObject



108
109
110
# File 'lib/brakeman/warning.rb', line 108

def hash
  self.to_s.hash
end


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/brakeman/warning.rb', line 154

def link
  return @link if @link

  if @link_path
    if @link_path.start_with? "http"
      @link = @link_path
    else
      @link = "http://brakemanscanner.org/docs/warning_types/#{@link_path}"
    end
  else
    warning_path = self.warning_type.to_s.downcase.gsub(/\s+/, '_') + "/"
    @link = "http://brakemanscanner.org/docs/warning_types/#{warning_path}"
  end

  @link
end

#location(include_renderer = true) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/brakeman/warning.rb', line 210

def location include_renderer = true
  case @warning_set
  when :template
    location = { :type => :template, :template => self.view_name(include_renderer) }
  when :model
    location = { :type => :model, :model => self.model }
  when :controller
    location = { :type => :controller, :controller => self.controller }
  when :warning
    if self.class
      location = { :type => :method, :class => self.class, :method => self.method }
    else
      location = nil
    end
  end
end

#to_hashObject



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/brakeman/warning.rb', line 227

def to_hash
  { :warning_type => self.warning_type,
    :warning_code => @warning_code,
    :fingerprint => self.fingerprint,
    :message => self.message,
    :file => self.file,
    :line => self.line,
    :link => self.link,
    :code => (@code && self.format_code(false)),
    :render_path => self.called_from,
    :location => self.location(false),
    :user_input => (@user_input && self.format_user_input(false)),
    :confidence => TEXT_CONFIDENCE[self.confidence]
  }
end

#to_jsonObject



243
244
245
# File 'lib/brakeman/warning.rb', line 243

def to_json
  MultiJson.dump self.to_hash
end

#to_row(type = :warning) ⇒ Object

Generates a hash suitable for inserting into a table



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/brakeman/warning.rb', line 172

def to_row type = :warning
  @row = { "Confidence" => self.confidence,
    "Warning Type" => self.warning_type.to_s,
    "Message" => self.format_message }

  case type
  when :template
    @row["Template"] = self.view_name.to_s
  when :model
    @row["Model"] = self.model.to_s
  when :controller
    @row["Controller"] = self.controller.to_s
  when :warning
    @row["Class"] = self.class.to_s
    @row["Method"] = self.method.to_s
  end

  @row
end

#to_sObject



192
193
194
195
196
197
198
199
# File 'lib/brakeman/warning.rb', line 192

def to_s
 output =  "(#{TEXT_CONFIDENCE[self.confidence]}) #{self.warning_type} - #{self.message}"
 output << " near line #{self.line}" if self.line
 output << " in #{self.file}" if self.file
 output << ": #{self.format_code}" if self.code

 output
end

#view_name(include_renderer = true) ⇒ Object

Returns name of a view, including where it was rendered from



117
118
119
120
121
122
123
# File 'lib/brakeman/warning.rb', line 117

def view_name(include_renderer = true)
  if called_from and include_renderer
    @view_name = "#{template.name} (#{called_from.last})"
  else
    @view_name = template.name
  end
end