Class: Brakeman::Report::HTML

Inherits:
Table show all
Defined in:
lib/brakeman/report/report_html.rb

Constant Summary collapse

HTML_CONFIDENCE =
[ "<span class='high-confidence'>High</span>",
"<span class='med-confidence'>Medium</span>",
"<span class='weak-confidence'>Weak</span>" ]

Constants included from Util

Util::ALL_COOKIES, Util::ALL_PARAMETERS, Util::COOKIES, Util::COOKIES_SEXP, Util::DIR_CONST, Util::LITERALS, Util::PARAMETERS, Util::PARAMS_SEXP, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_COOKIES, Util::REQUEST_ENV, Util::REQUEST_PARAMETERS, Util::REQUEST_PARAMS, Util::REQUEST_REQUEST_PARAMETERS, Util::SAFE_LITERAL, Util::SESSION, Util::SESSION_SEXP, Util::SIMPLE_LITERALS

Instance Attribute Summary

Attributes inherited from Base

#checks, #tracker

Instance Method Summary collapse

Methods inherited from Table

#convert_to_rows, #generate_controller_warnings, #generate_controllers, #generate_errors, #generate_ignored_warnings, #generate_model_warnings, #generate_obsolete, #generate_template_warnings, #generate_warning_overview, #generate_warnings, #output_table, #render_warnings, #sort, #text_header, #text_message, #truncate_table

Methods inherited from Base

#absolute_paths?, #all_warnings, #context_for, #controller_information, #controller_warnings, #filter_warnings, #generic_warnings, #github_url, #ignored_warnings, #model_warnings, #number_of_templates, #rails_version, #template_warnings, #warning_file, #warnings_summary

Methods included from Util

#all_literals?, #array?, #block?, #call?, #camelize, #class_name, #constant?, #contains_class?, #cookies?, #dir_glob?, #false?, #hash?, #hash_access, #hash_insert, #hash_iterate, #hash_values, #integer?, #kwsplat?, #literal?, #make_call, #node_type?, #number?, #params?, #pluralize, #rails_version, #recurse_check?, #regexp?, #remove_kwsplat, #request_headers?, #request_value?, #result?, #safe_literal, #safe_literal?, #safe_literal_target?, #set_env_defaults, #sexp?, #simple_literal?, #string?, #string_interp?, #symbol?, #template_path_to_name, #true?, #underscore

Constructor Details

#initialize(*args) ⇒ HTML

Returns a new instance of HTML.



9
10
11
12
13
# File 'lib/brakeman/report/report_html.rb', line 9

def initialize *args
  super

  @element_id = 0 #Used for HTML ids
end

Instance Method Details

#convert_ignored_warning(warning, original) ⇒ Object



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

def convert_ignored_warning warning, original
  warning = convert_warning(warning, original)
  warning['File'] = original.file.relative
  warning['Note'] = CGI.escapeHTML(@ignore_filter.note_for(original) || "")
  warning
end

#convert_template_warning(warning, original) ⇒ Object



80
81
82
83
84
85
# File 'lib/brakeman/report/report_html.rb', line 80

def convert_template_warning warning, original
  warning = convert_warning warning, original
  warning["Called From"] = original.called_from
  warning["Template Name"] = original.template.name
  warning
end

#convert_warning(warning, original) ⇒ Object



69
70
71
72
73
74
# File 'lib/brakeman/report/report_html.rb', line 69

def convert_warning warning, original
  warning["Confidence"] = HTML_CONFIDENCE[original.confidence]
  warning["Message"] = with_context original, warning["Message"]
  warning["Warning Type"] = with_link original, warning["Warning Type"]
  warning
end

#generate_overviewObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/brakeman/report/report_html.rb', line 34

def generate_overview
    locals = {
      :tracker => tracker,
      :warnings => all_warnings.length,
      :warnings_summary => warnings_summary,
      :number_of_templates => number_of_templates(@tracker),
      :ignored_warnings => ignored_warnings.length
      }

    Brakeman::Report::Renderer.new('overview', :locals => locals).render
end

#generate_reportObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/brakeman/report/report_html.rb', line 15

def generate_report
  out = html_header <<
  generate_overview <<
  generate_warning_overview.to_s

  # Return early if only summarizing
  return out if tracker.options[:summary_only]

  out << generate_controllers.to_s if tracker.options[:report_routes] or tracker.options[:debug]
  out << generate_templates.to_s if tracker.options[:debug]
  out << generate_errors.to_s
  out << generate_warnings.to_s
  out << generate_controller_warnings.to_s
  out << generate_model_warnings.to_s
  out << generate_template_warnings.to_s
  out << generate_ignored_warnings.to_s
  out << "</body></html>"
end

#generate_templatesObject

Generate listings of templates and their output



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/brakeman/report/report_html.rb', line 47

def generate_templates
  out_processor = Brakeman::OutputProcessor.new
  template_rows = {}
  tracker.templates.each do |name, template|
    template.each_output do |out|
      out = CGI.escapeHTML(out_processor.format(out))
      template_rows[name] ||= []
      template_rows[name] << out.gsub("\n", ";").gsub(/\s+/, " ")
    end
  end

  template_rows = template_rows.sort_by{|name, value| name.to_s}

    Brakeman::Report::Renderer.new('template_overview', :locals => {:template_rows => template_rows}).render
end

#html_headerObject

Return header for HTML output. Uses CSS from tracker.options



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/brakeman/report/report_html.rb', line 95

def html_header
  if File.exist? tracker.options[:html_style]
    css = File.read tracker.options[:html_style]
  else
    raise "Cannot find CSS stylesheet for HTML: #{tracker.options[:html_style]}"
  end

  locals = {
    :css => css,
    :tracker => tracker,
    :checks => checks,
    :rails_version => rails_version,
    :brakeman_version => Brakeman::Version
    }

  Brakeman::Report::Renderer.new('header', :locals => locals).render
end

#html_message(warning, message) ⇒ Object

Escape warning message and highlight user input in HTML output



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/brakeman/report/report_html.rb', line 187

def html_message warning, message
  message = message.to_html

  if warning.file
    if github_url = github_url(warning.file, warning.line)
      message << " <a href=\"#{github_url}\" target='_blank'>near line #{warning.line}</a>"
    elsif warning.line
      message << " near line #{warning.line}"
    end
  end

  if warning.code
    code = warning.format_with_user_input do |_, user_input|
      "[BMP_UI]#{user_input}[/BMP_UI]"
    end

    code = "<span class=\"code\">#{CGI.escapeHTML(code).gsub("[BMP_UI]", "<span class=\"user_input\">").gsub("[/BMP_UI]", "</span>")}</span>"
    full_message = "#{message}: #{code}"

    if warning.code.mass > 20
      message_id = "message#@element_id"
      full_message_id = "full_message#@element_id"

      "<span id='#{message_id}' style='display:block'>#{message}: ...</span>" <<
      "<span id='#{full_message_id}' style='display:none'>#{full_message}</span>"
    else
      full_message
    end
  else
    message
  end
end

#render_array(template, headings, value_array, locals) ⇒ Object



63
64
65
66
67
# File 'lib/brakeman/report/report_html.rb', line 63

def render_array template, headings, value_array, locals
  return if value_array.empty?

  Brakeman::Report::Renderer.new(template, :locals => locals).render
end

#with_context(warning, message) ⇒ Object

Generate HTML for warnings, including context show/hidden via Javascript



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/brakeman/report/report_html.rb', line 114

def with_context warning, message
  @element_id += 1
  context = context_for(warning)
  message = html_message(warning, message)

  code_id = "context#@element_id"
  message_id = "message#@element_id"
  full_message_id = "full_message#@element_id"
  alt = false
  output = "<div class='warning_message' onClick=\"toggle('#{code_id}');toggle('#{message_id}');toggle('#{full_message_id}')\" >" <<
  message <<
  "<table id='#{code_id}' class='context' style='display:none'>" <<
  "<caption>#{CGI.escapeHTML warning_file(warning) || ''}</caption>"

  output << <<-HTML
    <thead style='display:none'>
      <tr>
        <th>line number</th>
        <th>line content</th>
      </tr>
    </thead>
    <tbody>
  HTML

  unless context.empty?
    if warning.line - 1 == 1 or warning.line + 1 == 1
      error = " near_error"
    elsif 1 == warning.line
      error = " error"
    else
      error = ""
    end

    output << <<-HTML
      <tr class='context first#{error}'>
        <td class='context_line'>
          <pre class='context'>#{context.first[0]}</pre>
        </td>
        <td class='context'>
          <pre class='context'>#{CGI.escapeHTML context.first[1].chomp}</pre>
        </td>
      </tr>
    HTML

    if context.length > 1
      output << context[1..-1].map do |code|
        alt = !alt
        if code[0] == warning.line - 1 or code[0] == warning.line + 1
          error = " near_error"
        elsif code[0] == warning.line
          error = " error"
        else
          error = ""
        end

        <<-HTML
        <tr class='context#{alt ? ' alt' : ''}#{error}'>
          <td class='context_line'>
            <pre class='context'>#{code[0]}</pre>
          </td>
          <td class='context'>
            <pre class='context'>#{CGI.escapeHTML code[1].chomp}</pre>
          </td>
        </tr>
        HTML
      end.join
    end
  end

  output << "</tbody></table></div>"
end


76
77
78
# File 'lib/brakeman/report/report_html.rb', line 76

def with_link warning, message
  "<a rel=\"noreferrer\" href=\"#{warning.link}\">#{message}</a>"
end