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 =
Brakeman::Warning::TEXT_CONFIDENCE

Constants included from Util

Util::ALL_COOKIES, Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_COOKIES, 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.



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

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.



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

def checks
  @checks
end

#trackerObject (readonly)

Returns the value of attribute tracker.



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

def tracker
  @tracker
end

Instance Method Details

#all_warningsObject



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

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

#controller_warningsObject



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

def controller_warnings
  filter_warnings tracker.checks.controller_warnings
end

#convert_controller_warning(warning, original) ⇒ Object



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

def convert_controller_warning warning, original
  convert_warning warning, original
end

#convert_ignored_warning(warning, original) ⇒ Object



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

def convert_ignored_warning warning, original
  convert_warning warning, original
end

#convert_model_warning(warning, original) ⇒ Object



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

def convert_model_warning warning, original
  convert_warning warning, original
end

#convert_template_warning(warning, original) ⇒ Object



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

def convert_template_warning warning, original
  convert_warning warning, original
end

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



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

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



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

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

#filter_warnings(warnings) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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

#generate_obsoleteObject



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

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



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

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



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

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



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

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

#generic_warningsObject



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

def generic_warnings
  filter_warnings tracker.checks.warnings
end

#ignored_warningsObject



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

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

#model_warningsObject



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

def model_warnings
  filter_warnings tracker.checks.model_warnings
end

#number_of_templates(tracker) ⇒ Object



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

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

#rails_versionObject



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

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



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

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



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

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



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

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



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

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



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

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



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

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