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, Markdown, Table, Tabs

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, #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



208
209
210
211
212
213
214
# File 'lib/brakeman/report/report_base.rb', line 208

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

#controller_warningsObject



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

def controller_warnings
  filter_warnings tracker.checks.controller_warnings
end

#convert_controller_warning(warning, original) ⇒ Object



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

def convert_controller_warning warning, original
  convert_warning warning, original
end

#convert_ignored_warning(warning, original) ⇒ Object



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

def convert_ignored_warning warning, original
  convert_warning warning, original
end

#convert_model_warning(warning, original) ⇒ Object



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

def convert_model_warning warning, original
  convert_warning warning, original
end

#convert_template_warning(warning, original) ⇒ Object



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

def convert_template_warning warning, original
  convert_warning warning, original
end

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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/brakeman/report/report_base.rb', line 140

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



159
160
161
162
163
# File 'lib/brakeman/report/report_base.rb', line 159

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

#filter_warnings(warnings) ⇒ Object



216
217
218
219
220
221
222
223
224
# File 'lib/brakeman/report/report_base.rb', line 216

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



110
111
112
113
114
115
116
# File 'lib/brakeman/report/report_base.rb', line 110

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



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

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



101
102
103
104
105
106
107
# File 'lib/brakeman/report/report_base.rb', line 101

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

#generate_template_warningsObject

Generate table of template warnings or return nil if no warnings



91
92
93
94
95
96
97
98
# File 'lib/brakeman/report/report_base.rb', line 91

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



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

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

#generic_warningsObject



226
227
228
# File 'lib/brakeman/report/report_base.rb', line 226

def generic_warnings
  filter_warnings tracker.checks.warnings
end

#ignored_warningsObject



242
243
244
245
246
247
248
# File 'lib/brakeman/report/report_base.rb', line 242

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

#model_warningsObject



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

def model_warnings
  filter_warnings tracker.checks.model_warnings
end

#number_of_templates(tracker) ⇒ Object



250
251
252
# File 'lib/brakeman/report/report_base.rb', line 250

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

#rails_versionObject



264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/brakeman/report/report_base.rb', line 264

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



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/brakeman/report/report_base.rb', line 126

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



181
182
183
184
185
186
187
188
# File 'lib/brakeman/report/report_base.rb', line 181

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



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

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



278
279
280
281
282
283
284
285
# File 'lib/brakeman/report/report_base.rb', line 278

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



254
255
256
257
258
259
260
261
262
# File 'lib/brakeman/report/report_base.rb', line 254

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



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/brakeman/report/report_base.rb', line 191

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