Class: Brakeman::Report
Overview
Generates a report based on the Tracker and the results of Tracker#run_checks. Be sure to run_checks before generating a report.
Constant Summary
- TEXT_CONFIDENCE =
[ "High", "Medium", "Weak" ]
- 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_PARAMETERS, Util::COOKIES, Util::PARAMETERS, Util::PATH_PARAMETERS, Util::QUERY_PARAMETERS, Util::REQUEST_PARAMETERS, Util::SESSION
Instance Attribute Summary (collapse)
-
- (Object) checks
readonly
Returns the value of attribute checks.
-
- (Object) tracker
readonly
Returns the value of attribute tracker.
Instance Method Summary (collapse)
-
- (Object) context_for(warning)
Return array of lines surrounding the warning location from the original file.
-
- (Object) csv_header
Generate header for CSV output.
-
- (Object) file_by_name(name, type = nil)
Attempt to determine path to context file based on the reported name in the warning.
-
- (Object) file_for(warning)
Return file name related to given warning.
-
- (Object) generate_controller_warnings(html = false)
Generate table of controller warnings or nil if no warnings.
-
- (Object) generate_controllers
Generate table of controllers and routes found for those controllers.
-
- (Object) generate_errors(html = false)
Generate table of errors or return nil if no errors.
-
- (Object) generate_model_warnings(html = false)
Generate table of model warnings or return nil if no warnings.
-
- (Object) generate_overview(html = false)
Generate summary table of what was parsed.
-
- (Object) generate_template_warnings(html = false)
Generate table of template warnings or return nil if no warnings.
-
- (Object) generate_templates(html = false)
Generate listings of templates and their output.
-
- (Object) generate_warning_overview
Generate table of how many warnings of each warning type were reported.
-
- (Object) generate_warnings(html = false)
Generate table of general security warnings.
-
- (Object) html_header
Return header for HTML output.
-
- (Report) initialize(tracker)
constructor
A new instance of Report.
- - (Object) rails_version
-
- (Object) text_header
Generate header for text output.
-
- (Object) to_csv
Generate CSV output.
-
- (Object) to_html
Generate HTML output.
-
- (Object) to_pdf
Not yet implemented.
-
- (Object) to_s
Output text version of the report.
-
- (Object) to_tabs
Generated tab-separated output suitable for the Jenkins Brakeman Plugin: github.com/presidentbeef/brakeman-jenkins-plugin.
- - (Object) to_test
-
- (Object) warnings_summary
Return summary of warnings in hash and store in @warnings_summary.
-
- (Object) with_context(warning, message)
Generate HTML for warnings, including context show/hidden via Javascript.
Methods included from Util
#array?, #call?, #camelize, #cookies?, #false?, #hash?, #hash_insert, #hash_iterate, #integer?, #number?, #params?, #pluralize, #regexp?, #result?, #set_env_defaults, #sexp?, #string?, #symbol?, #true?, #underscore
Constructor Details
- (Report) initialize(tracker)
A new instance of Report
31 32 33 34 35 36 |
# File 'lib/brakeman/report.rb', line 31 def initialize tracker @tracker = tracker @checks = tracker.checks @element_id = 0 #Used for HTML ids @warnings_summary = nil end |
Instance Attribute Details
- (Object) checks (readonly)
Returns the value of attribute checks
24 25 26 |
# File 'lib/brakeman/report.rb', line 24 def checks @checks end |
- (Object) tracker (readonly)
Returns the value of attribute tracker
24 25 26 |
# File 'lib/brakeman/report.rb', line 24 def tracker @tracker end |
Instance Method Details
- (Object) context_for(warning)
Return array of lines surrounding the warning location from the original file.
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 |
# File 'lib/brakeman/report.rb', line 558 def context_for warning file = file_for warning context = [] return context unless warning.line and file and File.exist? file current_line = 0 start_line = warning.line - 5 end_line = warning.line + 5 start_line = 1 if start_line < 0 File.open file do |f| f.each_line do |line| current_line += 1 next if line.strip == "" if current_line > end_line break end if current_line >= start_line context << [current_line, line] end end end context end |
- (Object) csv_header
Generate header for CSV output
452 453 454 455 456 |
# File 'lib/brakeman/report.rb', line 452 def csv_header header = Ruport::Data::Table(["Application Path", "Report Generation Time", "Checks Performed", "Rails Version"]) header << [File.(tracker.[:app_path]), Time.now.to_s, checks.checks_run.sort.join(", "), rails_version] "BRAKEMAN REPORT\n\n" << header.to_csv end |
- (Object) file_by_name(name, type = nil)
Attempt to determine path to context file based on the reported name in the warning.
For example,
file_by_name FileController #=> "/rails/root/app/controllers/file_controller.rb
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'lib/brakeman/report.rb', line 512 def file_by_name name, type = nil return nil unless name string_name = name.to_s name = name.to_sym unless type if string_name =~ /Controller$/ type = :controller elsif camelize(string_name) == string_name type = :model else type = :template end end path = tracker.[:app_path] case type when :controller if tracker.controllers[name] and tracker.controllers[name][:file] path = tracker.controllers[name][:file] else path += "/app/controllers/#{underscore(string_name)}.rb" end when :model if tracker.models[name] and tracker.models[name][:file] path = tracker.models[name][:file] else path += "/app/controllers/#{underscore(string_name)}.rb" end when :template if tracker.templates[name] and tracker.templates[name][:file] path = tracker.templates[name][:file] elsif string_name.include? " " name = string_name.split[0].to_sym path = file_for name, :template else path = nil end end path end |
- (Object) file_for(warning)
Return file name related to given warning. Uses warning.file if it exists
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/brakeman/report.rb', line 487 def file_for warning if warning.file File. warning.file, tracker.[:app_path] else case warning.warning_set when :controller file_by_name warning.controller, :controller when :template file_by_name warning.template[:name], :template when :model file_by_name warning.model, :model when :warning file_by_name warning.class else nil end end end |
- (Object) generate_controller_warnings(html = false)
Generate table of controller warnings or nil if no warnings
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 219 220 221 |
# File 'lib/brakeman/report.rb', line 193 def generate_controller_warnings html = false unless checks.controller_warnings.empty? table = Ruport::Data::Table(["Confidence", "Controller", "Warning Type", "Message"]) checks.controller_warnings.each do |warning| next if warning.confidence > tracker.[:min_confidence] w = warning.to_row :controller if html w["Confidence"] = HTML_CONFIDENCE[w["Confidence"]] w["Message"] = with_context warning, w["Message"] else w["Confidence"] = TEXT_CONFIDENCE[w["Confidence"]] end table << w end if table.empty? nil else table.sort_rows_by! "Controller" table.sort_rows_by! "Warning Type" table.sort_rows_by! "Confidence" table.to_group "Controller Warnings" end else nil end end |
- (Object) generate_controllers
Generate table of controllers and routes found for those controllers
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/brakeman/report.rb', line 224 def generate_controllers table = Ruport::Data::Table(["Name", "Parent", "Includes", "Routes"]) tracker.controllers.keys.map{|k| k.to_s}.sort.each do |name| name = name.to_sym c = tracker.controllers[name] if tracker.routes[:allow_all_actions] or tracker.routes[name] == :allow_all_actions routes = c[: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[:public].keys) & tracker.routes[name.to_sym]). to_a. map {|e| e.to_s}. sort. join(", ") end if routes == "" routes = "[None]" end table << { "Name" => name.to_s, "Parent" => c[:parent].to_s, "Includes" => c[:includes].join(", "), "Routes" => routes } end table.sort_rows_by "Name" end |
- (Object) generate_errors(html = false)
Generate table of errors or return nil if no errors
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/brakeman/report.rb', line 80 def generate_errors html = false unless tracker.errors.empty? table = Ruport::Data::Table(["Error", "Location"]) tracker.errors.each do |w| p w if tracker.[:debug] if html w[:error] = CGI.escapeHTML w[:error] end table << { "Error" => w[:error], "Location" => w[:backtrace][0] } end table else nil end end |
- (Object) generate_model_warnings(html = false)
Generate table of model warnings or return nil if no warnings
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.rb', line 162 def generate_model_warnings html = false unless checks.model_warnings.empty? table = Ruport::Data::Table(["Confidence", "Model", "Warning Type", "Message"]) checks.model_warnings.each do |warning| next if warning.confidence > tracker.[:min_confidence] w = warning.to_row :model if html w["Confidence"] = HTML_CONFIDENCE[w["Confidence"]] w["Message"] = with_context warning, w["Message"] else w["Confidence"] = TEXT_CONFIDENCE[w["Confidence"]] end table << w end if table.empty? nil else table.sort_rows_by! "Model" table.sort_rows_by! "Warning Type" table.sort_rows_by! "Confidence" table.to_group "Model Warnings" end else nil end end |
- (Object) generate_overview(html = false)
Generate summary table of what was parsed
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 |
# File 'lib/brakeman/report.rb', line 39 def generate_overview html = false templates = Set.new(@tracker.templates.map {|k,v| v[:name].to_s[/[^.]+/]}).length warnings = checks.warnings.length + checks.controller_warnings.length + checks.model_warnings.length + checks.template_warnings.length #Add number of high confidence warnings in summary. #Skipping for CSV because it makes the cell text instead of #a number. unless tracker.[:output_format] == :to_csv summary = warnings_summary if html warnings = "#{warnings} <span class='high-confidence'>(#{summary[:high_confidence]})</span>" else warnings = "#{warnings} (#{summary[:high_confidence]})" end end table = Ruport::Data::Table(["Scanned/Reported", "Total"]) table << { "Scanned/Reported" => "Controllers", "Total" => tracker.controllers.length } #One less because of the 'fake' one used for unknown models table << { "Scanned/Reported" => "Models", "Total" => tracker.models.length - 1 } table << { "Scanned/Reported" => "Templates", "Total" => templates } table << { "Scanned/Reported" => "Errors", "Total" => tracker.errors.length } table << { "Scanned/Reported" => "Security Warnings", "Total" => warnings} end |
- (Object) generate_template_warnings(html = false)
Generate table of template warnings or return nil if no warnings
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 |
# File 'lib/brakeman/report.rb', line 131 def generate_template_warnings html = false unless checks.template_warnings.empty? table = Ruport::Data::Table(["Confidence", "Template", "Warning Type", "Message"]) checks.template_warnings.each do |warning| next if warning.confidence > tracker.[:min_confidence] w = warning.to_row :template if html w["Confidence"] = HTML_CONFIDENCE[w["Confidence"]] w["Message"] = with_context warning, w["Message"] else w["Confidence"] = TEXT_CONFIDENCE[w["Confidence"]] end table << w end if table.empty? nil else table.sort_rows_by! "Template" table.sort_rows_by! "Warning Type" table.sort_rows_by! "Confidence" table.to_group "View Warnings" end else nil end end |
- (Object) generate_templates(html = false)
Generate listings of templates and their output
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/brakeman/report.rb', line 260 def generate_templates html = false out_processor = Brakeman::OutputProcessor.new table = Ruport::Data::Table(["Name", "Output"]) tracker.templates.each do |name, template| unless template[:outputs].empty? template[:outputs].each do |out| out = out_processor.format out out = CGI.escapeHTML(out) if html table << { "Name" => name, "Output" => out.gsub("\n", ";").gsub(/\s+/, " ") } end end end Ruport::Data::Grouping(table, :by => "Name") end |
- (Object) generate_warning_overview
Generate table of how many warnings of each warning type were reported
69 70 71 72 73 74 75 76 77 |
# File 'lib/brakeman/report.rb', line 69 def generate_warning_overview table = Ruport::Data::Table(["Warning Type", "Total"]) types = warnings_summary.keys types.delete :high_confidence types.sort.each do |warning_type| table << { "Warning Type" => warning_type, "Total" => warnings_summary[warning_type] } end table end |
- (Object) generate_warnings(html = false)
Generate table of general security warnings
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/brakeman/report.rb', line 102 def generate_warnings html = false table = Ruport::Data::Table(["Confidence", "Class", "Method", "Warning Type", "Message"]) checks.warnings.each do |warning| next if warning.confidence > tracker.[:min_confidence] w = warning.to_row if html w["Confidence"] = HTML_CONFIDENCE[w["Confidence"]] w["Message"] = with_context warning, w["Message"] else w["Confidence"] = TEXT_CONFIDENCE[w["Confidence"]] end table << w end table.sort_rows_by! "Class" table.sort_rows_by! "Warning Type" table.sort_rows_by! "Confidence" if table.empty? table = Ruport::Data::Table("General Warnings") table << { "General Warnings" => "[NONE]" } end table end |
- (Object) html_header
Return header for HTML output. Uses CSS from tracker.options
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/brakeman/report.rb', line 399 def html_header if File.exist? tracker.[:html_style] css = File.read tracker.[:html_style] else raise "Cannot find CSS stylesheet for HTML: #{tracker.[:html_style]}" end <<-HTML <!DOCTYPE HTML SYSTEM> <html> <head> <title>Brakeman Report</title> <script type="text/javascript"> function toggle(context) { var elem = document.getElementById(context); if (elem.style.display != "block") elem.style.display = "block"; else elem.style.display = "none"; elem.parentNode.scrollIntoView(); } </script> <style type="text/css"> #{css} </style> </head> <body> <h1>Brakeman Report</h1> <table> <tr> <th>Application Path</th> <th>Rails Version</th> <th>Report Generation Time</th> <th>Checks Performed</th> </tr> <tr> <td>#{File. tracker.[:app_path]}</td> <td>#{rails_version}</td> <td>#{Time.now}</td> <td>#{checks.checks_run.sort.join(", ")}</td> </tr> </table> HTML end |
- (Object) rails_version
388 389 390 391 392 393 394 395 396 |
# File 'lib/brakeman/report.rb', line 388 def rails_version if version = tracker.config[:rails_version] return version elsif tracker.[:rails3] return "3.x" else return "Unknown" end end |
- (Object) text_header
Generate header for text output
447 448 449 |
# File 'lib/brakeman/report.rb', line 447 def text_header "\n+BRAKEMAN REPORT+\n\nApplication path: #{File. tracker.[:app_path]}\nRails version: #{rails_version}\nGenerated at #{Time.now}\nChecks run: #{checks.checks_run.sort.join(", ")}\n" end |
- (Object) to_csv
Generate CSV output
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/brakeman/report.rb', line 349 def to_csv out = csv_header << "\nSUMMARY\n" << generate_overview.to_csv << "\n" << generate_warning_overview.to_csv << "\n" if tracker.[:report_routes] or tracker.[:debug] out << "CONTROLLERS\n" << generate_controllers.to_csv << "\n" end if tracker.[:debug] out << "TEMPLATES\n\n" << generate_templates.to_csv << "\n" end res = generate_errors out << "ERRORS\n" << res.to_csv << "\n" if res res = generate_warnings out << "SECURITY WARNINGS\n" << res.to_csv << "\n" if res res = generate_controller_warnings out << res.to_csv << "\n" if res res = generate_model_warnings out << res.to_csv << "\n" if res res = generate_template_warnings out << res.to_csv << "\n" if res out end |
- (Object) to_html
Generate HTML output
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/brakeman/report.rb', line 277 def to_html out = html_header << "<h2 id='summary'>Summary</h2>" << generate_overview(true).to_html << "<br/>" << generate_warning_overview.to_html if tracker.[:report_routes] or tracker.[:debug] out << "<h2>Controllers</h2>" << generate_controllers.to_html end if tracker.[:debug] out << "<h2>Templates</h2>" << generate_templates(true).to_html end res = generate_errors(true) if res out << "<div onClick=\"toggle('errors_table');\"> <h2>Exceptions raised during the analysis (click to see them)</h2 ></div> <div id='errors_table' style='display:none'>" << res.to_html << '</div>' end res = generate_warnings(true) out << "<h2>Security Warnings</h2>" << res.to_html if res res = generate_controller_warnings(true) out << res.to_html if res res = generate_model_warnings(true) out << res.to_html if res res = generate_template_warnings(true) out << res.to_html if res out << "</body></html>" end |
- (Object) to_pdf
Not yet implemented
384 385 386 |
# File 'lib/brakeman/report.rb', line 384 def to_pdf raise "PDF output is not yet supported." end |
- (Object) to_s
Output text version of the report
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/brakeman/report.rb', line 314 def to_s out = text_header << "\n+SUMMARY+\n" << generate_overview.to_s << "\n" << generate_warning_overview.to_s << "\n" if tracker.[:report_routes] or tracker.[:debug] out << "+CONTROLLERS+\n" << generate_controllers.to_s << "\n" end if tracker.[:debug] out << "+TEMPLATES+\n\n" << generate_templates.to_s << "\n" end res = generate_errors out << "+ERRORS+\n" << res.to_s << "\n" if res res = generate_warnings out << "+SECURITY WARNINGS+\n" << res.to_s << "\n" if res res = generate_controller_warnings out << res.to_s << "\n" if res res = generate_model_warnings out << res.to_s << "\n" if res res = generate_template_warnings out << res.to_s << "\n" if res out end |
- (Object) to_tabs
Generated tab-separated output suitable for the Jenkins Brakeman Plugin: github.com/presidentbeef/brakeman-jenkins-plugin
669 670 671 672 673 674 675 676 677 678 679 680 681 |
# File 'lib/brakeman/report.rb', line 669 def to_tabs [[:warnings, "General"], [:controller_warnings, "Controller"], [:model_warnings, "Model"], [:template_warnings, "Template"]].map do |meth, category| checks.send(meth).map do |w| next if w.confidence > tracker.[:min_confidence] line = w.line || 0 w.warning_type.gsub!(/[^\w\s]/, ' ') "#{file_for w}\t#{line}\t#{w.warning_type}\t#{category}\t#{w.}\t#{TEXT_CONFIDENCE[w.confidence]}" end.join "\n" end.join "\n" end |
- (Object) to_test
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 |
# File 'lib/brakeman/report.rb', line 683 def to_test report = { :errors => tracker.errors, :controllers => tracker.controllers, :models => tracker.models, :templates => tracker.templates } [:warnings, :controller_warnings, :model_warnings, :template_warnings].each do |meth| report[meth] = @checks.send(meth) report[meth].each do |w| w. = w. if w.code w.code = w.format_code else w.code = "" end w.context = context_for(w).join("\n") w.file = file_for w end end report end |
- (Object) warnings_summary
Return summary of warnings in hash and store in @warnings_summary
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/brakeman/report.rb', line 459 def warnings_summary return @warnings_summary if @warnings_summary summary = Hash.new(0) high_confidence_warnings = 0 [checks.warnings, checks.controller_warnings, checks.model_warnings, checks.template_warnings].each do |warnings| warnings.each do |warning| unless warning.confidence > tracker.[:min_confidence] summary[warning.warning_type.to_s] += 1 if warning.confidence == 0 high_confidence_warnings += 1 end end end end summary[:high_confidence] = high_confidence_warnings @warnings_summary = summary end |
- (Object) with_context(warning, message)
Generate HTML for warnings, including context show/hidden via Javascript
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
# File 'lib/brakeman/report.rb', line 589 def with_context warning, context = context_for warning = nil if tracker.[:message_limit] and tracker.[:message_limit] > 0 and .length > tracker.[:message_limit] = = [0..tracker.[:message_limit]] << "..." end if context.empty? and not return CGI.escapeHTML() end @element_id += 1 code_id = "context#@element_id" = "message#@element_id" = "full_message#@element_id" alt = false output = "<div class='warning_message' onClick=\"toggle('#{code_id}');toggle('#{}');toggle('#{}')\" >" << if "<span id='#{}' style='display:block' >#{CGI.escapeHTML()}</span>" << "<span id='#{}' style='display:none'>#{CGI.escapeHTML()}</span>" else CGI.escapeHTML() end << "<table id='#{code_id}' class='context' style='display:none'>" 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 |