Class: Brakeman::Report::HTML

Inherits:
Base 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 inherited from Base

Base::TEXT_CONFIDENCE

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

Attributes inherited from Base

#checks, #tracker

Instance Method Summary collapse

Methods inherited from Base

#all_warnings, #controller_warnings, #convert_controller_warning, #convert_model_warning, #convert_to_rows, #filter_warnings, #generate_controller_warnings, #generate_controllers, #generate_errors, #generate_ignored_warnings, #generate_model_warnings, #generate_template_warnings, #generate_warning_overview, #generate_warnings, #generic_warnings, #ignored_warnings, #model_warnings, #number_of_templates, #rails_version, #render_warnings, #sort, #template_warnings, #text_message, #warning_file, #warnings_summary

Methods included from Util

#array?, #block?, #call?, #camelize, #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?, #symbol?, #table_to_csv, #template_path_to_name, #true?, #truncate_table, #underscore

Constructor Details

#initialize(*args) ⇒ HTML

Returns a new instance of HTML.



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

def initialize *args
  super

  @element_id = 0 #Used for HTML ids
end

Instance Method Details

#convert_ignored_warning(warning, original) ⇒ Object



90
91
92
93
94
95
# File 'lib/brakeman/report/report_html.rb', line 90

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

#convert_template_warning(warning, original) ⇒ Object



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

def convert_template_warning warning, original
  warning["Confidence"] = HTML_CONFIDENCE[warning["Confidence"]]
  warning["Message"] = with_context original, warning["Message"]
  warning["Warning Type"] = with_link original, warning["Warning Type"]
  warning["Called From"] = original.called_from
  warning["Template Name"] = original.template[:name]
  warning
end

#convert_warning(warning, original) ⇒ Object



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

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

#generate_overviewObject



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

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



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

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



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

def generate_templates
  out_processor = Brakeman::OutputProcessor.new
  template_rows = {}
  tracker.templates.each do |name, template|
    unless template[:outputs].empty?
      template[:outputs].each do |out|
        out = CGI.escapeHTML(out_processor.format(out))
        template_rows[name] ||= []
        template_rows[name] << out.gsub("\n", ";").gsub(/\s+/, " ")
      end
    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



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

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



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

def html_message warning, message
  message = CGI.escapeHTML(message)

  if warning.file
    github_url = github_url warning.file, warning.line
    message.gsub!(/(near line \d+)/, "<a href='#{URI.escape github_url, /'/}' target='_blank'>\\1</a>") if github_url
  end

  if @highlight_user_input and warning.user_input
    user_input = CGI.escapeHTML(warning.format_user_input)
    message.gsub!(user_input, "<span class=\"user_input\">#{user_input}</span>")
  end

  message
end

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



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

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



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
185
186
187
188
189
190
# File 'lib/brakeman/report/report_html.rb', line 117

def with_context warning, message
  context = context_for(@app_tree, warning)
  full_message = nil

  if tracker.options[:message_limit] and tracker.options[:message_limit] > 0 and message.length > tracker.options[:message_limit]
    full_message = html_message(warning, message)
    message = message[0..tracker.options[:message_limit]] << "..."
  end

  message = html_message(warning, message)
  return message if context.empty? and not full_message

  @element_id += 1
  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}')\" >" <<
  if full_message
    "<span id='#{message_id}' style='display:block' >#{message}</span>" <<
    "<span id='#{full_message_id}' style='display:none'>#{full_message}</span>"
  else
    message
  end <<
  "<table id='#{code_id}' class='context' style='display:none'>" <<
  "<caption>#{CGI.escapeHTML warning_file(warning) || ''}</caption>"

  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 << "</table></div>"
end


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

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