Class: Brakeman::Report::Base

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/brakeman/report/report_base.rb

Overview

Base class for report formats

Direct Known Subclasses

CodeClimate, HTML, Hash, JSON, Table, Tabs, Text

Constant Summary collapse

TEXT_CONFIDENCE =
[ "High", "Medium", "Weak" ]

Constants included from Util

Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_ENV, Util::REQUEST_PARAMETERS, Util::REQUEST_PARAMS, Util::SESSION, Util::SESSION_SEXP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#array?, #block?, #call?, #camelize, #class_name, #constant?, #contains_class?, #context_for, #cookies?, #false?, #file_by_name, #file_for, #github_url, #hash?, #hash_access, #hash_insert, #hash_iterate, #integer?, #make_call, #node_type?, #number?, #params?, #pluralize, #regexp?, #relative_path, #request_env?, #request_value?, #result?, #set_env_defaults, #sexp?, #string?, #string_interp?, #symbol?, #table_to_csv, #template_path_to_name, #true?, #truncate_table, #underscore

Constructor Details

#initialize(app_tree, tracker) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
20
21
22
# File 'lib/brakeman/report/report_base.rb', line 15

def initialize app_tree, tracker
  @app_tree = app_tree
  @tracker = tracker
  @checks = tracker.checks
  @ignore_filter = tracker.ignored_filter
  @highlight_user_input = tracker.options[:highlight_user_input]
  @warnings_summary = nil
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



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

def checks
  @checks
end

#trackerObject (readonly)

Returns the value of attribute tracker.



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

def tracker
  @tracker
end

Instance Method Details

#all_warningsObject



213
214
215
216
217
218
219
# File 'lib/brakeman/report/report_base.rb', line 213

def all_warnings
  if @ignore_filter
    @all_warnings ||= @ignore_filter.shown_warnings
  else
    @all_warnings ||= tracker.checks.all_warnings
  end
end

#controller_warningsObject



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

def controller_warnings
  filter_warnings tracker.checks.controller_warnings
end

#convert_controller_warning(warning, original) ⇒ Object



178
179
180
# File 'lib/brakeman/report/report_base.rb', line 178

def convert_controller_warning warning, original
  convert_warning warning, original
end

#convert_ignored_warning(warning, original) ⇒ Object



182
183
184
# File 'lib/brakeman/report/report_base.rb', line 182

def convert_ignored_warning warning, original
  convert_warning warning, original
end

#convert_model_warning(warning, original) ⇒ Object



174
175
176
# File 'lib/brakeman/report/report_base.rb', line 174

def convert_model_warning warning, original
  convert_warning warning, original
end

#convert_template_warning(warning, original) ⇒ Object



170
171
172
# File 'lib/brakeman/report/report_base.rb', line 170

def convert_template_warning warning, original
  convert_warning warning, original
end

#convert_to_rows(warnings, type = :warning) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/brakeman/report/report_base.rb', line 145

def convert_to_rows warnings, type = :warning
  warnings.map do |warning|
    w = warning.to_row type

    case type
    when :warning
      convert_warning w, warning
    when :template
      convert_template_warning w, warning
    when :model
      convert_model_warning w, warning
    when :controller
      convert_controller_warning w, warning
    when :ignored
      convert_ignored_warning w, warning
    end
  end
end

#convert_warning(warning, original) ⇒ Object



164
165
166
167
168
# File 'lib/brakeman/report/report_base.rb', line 164

def convert_warning warning, original
  warning["Confidence"] = TEXT_CONFIDENCE[warning["Confidence"]]
  warning["Message"] = text_message original, warning["Message"]
  warning
end

#filter_warnings(warnings) ⇒ Object



221
222
223
224
225
226
227
228
229
# File 'lib/brakeman/report/report_base.rb', line 221

def filter_warnings warnings
  if @ignore_filter
    warnings.reject do |w|
      @ignore_filter.ignored? w
    end
  else
    warnings
  end
end

#generate_controller_warningsObject

Generate table of controller warnings or nil if no warnings



115
116
117
118
119
120
121
# File 'lib/brakeman/report/report_base.rb', line 115

def generate_controller_warnings
  render_warnings controller_warnings,
                  :controller,
                  'controller_warnings',
                  ['Confidence', 'Controller', 'Warning Type', 'Message'],
                  'Controller'
end

#generate_controllersObject

Generate table of controllers and routes found for those controllers



35
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
# File 'lib/brakeman/report/report_base.rb', line 35

def generate_controllers
  controller_rows = []

  tracker.controllers.keys.map{|k| k.to_s}.sort.each do |name|
    name = name.to_sym
    c = tracker.controllers[name]

    if tracker.routes.include? :allow_all_actions or (tracker.routes[name] and tracker.routes[name].include? :allow_all_actions)
      routes = c.methods_public.keys.map{|e| e.to_s}.sort.join(", ")
    elsif tracker.routes[name].nil?
      #No routes defined for this controller.
      #This can happen when it is only a parent class
      #for other controllers, for example.
      routes = "[None]"

    else
      routes = (Set.new(c.methods_public.keys) & tracker.routes[name.to_sym]).
        to_a.
        map {|e| e.to_s}.
        sort.
        join(", ")
    end

    if routes == ""
      routes = "[None]"
    end

    controller_rows << { "Name" => name.to_s,
      "Parent" => c.parent.to_s,
      "Includes" => c.includes.join(", "),
      "Routes" => routes
    }
  end

  cols = ['Name', 'Parent', 'Includes', 'Routes']

  locals = {:controller_rows => controller_rows}
  values = controller_rows.collect{|row| row.values_at(*cols) }
  render_array('controller_overview', cols, values, locals)
end

#generate_errorsObject

Generate table of errors or return nil if no errors



77
78
79
80
# File 'lib/brakeman/report/report_base.rb', line 77

def generate_errors
  values = tracker.errors.collect{|error| [error[:error], error[:backtrace][0]]}
  render_array('error_overview', ['Error', 'Location'], values, {:tracker => tracker})
end

#generate_ignored_warningsObject



123
124
125
126
127
128
129
# File 'lib/brakeman/report/report_base.rb', line 123

def generate_ignored_warnings
  render_warnings ignored_warnings,
                  :ignored,
                  'ignored_warnings',
                  ['Confidence', 'Warning Type', 'File', 'Message'],
                  'Warning Type'
end

#generate_model_warningsObject

Generate table of model warnings or return nil if no warnings



106
107
108
109
110
111
112
# File 'lib/brakeman/report/report_base.rb', line 106

def generate_model_warnings
  render_warnings model_warnings,
                  :model,
                  'model_warnings',
                  ['Confidence', 'Model', 'Warning Type', 'Message'],
                  'Model'
end

#generate_obsoleteObject



82
83
84
85
# File 'lib/brakeman/report/report_base.rb', line 82

def generate_obsolete
  values = tracker.unused_fingerprints.collect{|fingerprint| [fingerprint] }
  render_array('obsolete_ignore_entries', ['fingerprint'], values, {:tracker => tracker})
end

#generate_template_warningsObject

Generate table of template warnings or return nil if no warnings



96
97
98
99
100
101
102
103
# File 'lib/brakeman/report/report_base.rb', line 96

def generate_template_warnings
  render_warnings template_warnings,
                  :template,
                  'view_warnings',
                  ['Confidence', 'Template', 'Warning Type', 'Message'],
                  'Template'

end

#generate_warning_overviewObject

Generate table of how many warnings of each warning type were reported



25
26
27
28
29
30
31
32
# File 'lib/brakeman/report/report_base.rb', line 25

def generate_warning_overview
  types = warnings_summary.keys
  types.delete :high_confidence
  values = types.sort.collect{|warning_type| [warning_type, warnings_summary[warning_type]] }
  locals = {:types => types, :warnings_summary => warnings_summary}

  render_array('warning_overview', ['Warning Type', 'Total'], values, locals)
end

#generate_warningsObject



87
88
89
90
91
92
93
# File 'lib/brakeman/report/report_base.rb', line 87

def generate_warnings
  render_warnings generic_warnings,
                  :warning,
                  'security_warnings',
                  ["Confidence", "Class", "Method", "Warning Type", "Message"],
                  'Class'
end

#generic_warningsObject



231
232
233
# File 'lib/brakeman/report/report_base.rb', line 231

def generic_warnings
  filter_warnings tracker.checks.warnings
end

#ignored_warningsObject



247
248
249
250
251
252
253
# File 'lib/brakeman/report/report_base.rb', line 247

def ignored_warnings
  if @ignore_filter
    @ignore_filter.ignored_warnings
  else
    []
  end
end

#model_warningsObject



239
240
241
# File 'lib/brakeman/report/report_base.rb', line 239

def model_warnings
  filter_warnings tracker.checks.model_warnings
end

#number_of_templates(tracker) ⇒ Object



255
256
257
# File 'lib/brakeman/report/report_base.rb', line 255

def number_of_templates tracker
  Set.new(tracker.templates.map {|k,v| v.name.to_s[/[^.]+/]}).length
end

#rails_versionObject



269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/brakeman/report/report_base.rb', line 269

def rails_version
  case
  when tracker.config.rails_version
    tracker.config.rails_version
  when tracker.options[:rails4]
    "4.x"
  when tracker.options[:rails3]
    "3.x"
  else
    "Unknown"
  end
end

#render_warnings(warnings, type, template, cols, sort_col) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/brakeman/report/report_base.rb', line 131

def render_warnings warnings, type, template, cols, sort_col
  unless warnings.empty?
    rows = sort(convert_to_rows(warnings, type), sort_col)

    values = rows.collect { |row| row.values_at(*cols) }

    locals = { :warnings => rows }

    render_array(template, cols, values, locals)
  else
    nil
  end
end

#sort(rows, sort_col) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/brakeman/report/report_base.rb', line 186

def sort rows, sort_col
  stabilizer = 0
  rows.sort_by do |row|
    stabilizer += 1

    row.values_at("Confidence", "Warning Type", sort_col) << stabilizer
  end
end

#template_warningsObject



235
236
237
# File 'lib/brakeman/report/report_base.rb', line 235

def template_warnings
  filter_warnings tracker.checks.template_warnings
end

#text_message(warning, message) ⇒ Object

Escape warning message and highlight user input in text output



283
284
285
286
287
288
289
290
# File 'lib/brakeman/report/report_base.rb', line 283

def text_message warning, message
  if @highlight_user_input and warning.user_input
    user_input = warning.format_user_input
    message.gsub(user_input, "+#{user_input}+")
  else
    message
  end
end

#warning_file(warning, absolute = ) ⇒ Object



259
260
261
262
263
264
265
266
267
# File 'lib/brakeman/report/report_base.rb', line 259

def warning_file warning, absolute = @tracker.options[:absolute_paths]
  return nil if warning.file.nil?

  if absolute
    warning.file
  else
    relative_path warning.file
  end
end

#warnings_summaryObject

Return summary of warnings in hash and store in @warnings_summary



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/brakeman/report/report_base.rb', line 196

def warnings_summary
  return @warnings_summary if @warnings_summary

  summary = Hash.new(0)
  high_confidence_warnings = 0

  [all_warnings].each do |warnings|
    warnings.each do |warning|
      summary[warning.warning_type.to_s] += 1
      high_confidence_warnings += 1 if warning.confidence == 0
    end
  end

  summary[:high_confidence] = high_confidence_warnings
  @warnings_summary = summary
end