Skip to content

Commit

Permalink
Merge pull request #894 from jcheatham/master
Browse files Browse the repository at this point in the history
Adds obsolete ignore filter identification functionality
  • Loading branch information
presidentbeef committed Sep 5, 2016
2 parents 832a97c + 25fd064 commit 7ff8b5e
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 4 deletions.
7 changes: 7 additions & 0 deletions lib/brakeman/report/ignore/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def initialize file, new_warnings
@new_warnings = new_warnings
@already_ignored = []
@ignored_fingerprints = Set.new
@used_fingerprints = Set.new
@notes = {}
@shown_warnings = @ignored_warnings = nil
@changed = false
Expand Down Expand Up @@ -43,6 +44,7 @@ def unignore warning

# Determine if warning should be ignored
def ignored? warning
@used_fingerprints << warning.fingerprint
@ignored_fingerprints.include? warning.fingerprint
end

Expand Down Expand Up @@ -75,6 +77,11 @@ def note_for warning
nil
end

# The set of unused ignore entries
def obsolete_fingerprints
(@ignored_fingerprints - @used_fingerprints).to_a
end

# Read configuration to file
def read_from_file file = @file
if File.exist? file
Expand Down
5 changes: 5 additions & 0 deletions lib/brakeman/report/report_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ def generate_errors
render_array('error_overview', ['Error', 'Location'], values, {:tracker => tracker})
end

def generate_obsolete
values = tracker.unused_fingerprints.collect{|fingerprint| [fingerprint] }
render_array('obsolete_ignore_entries', ['fingerprint'], values, {:tracker => tracker})
end

def generate_warnings
render_warnings generic_warnings,
:warning,
Expand Down
5 changes: 4 additions & 1 deletion lib/brakeman/report/report_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Brakeman::Report::JSON < Brakeman::Report::Base
def generate_report
errors = tracker.errors.map{|e| { :error => e[:error], :location => e[:backtrace][0] }}

obsolete = tracker.unused_fingerprints

warnings = convert_to_hashes all_warnings

ignored = convert_to_hashes ignored_warnings
Expand All @@ -26,7 +28,8 @@ def generate_report
:scan_info => scan_info,
:warnings => warnings,
:ignored_warnings => ignored,
:errors => errors
:errors => errors,
:obsolete => obsolete
}

JSON.pretty_generate report_info
Expand Down
1 change: 1 addition & 0 deletions lib/brakeman/report/report_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def generate_report
truncate_table(generate_templates.to_s) << "\n"
end

output_table("+Obsolete Ignore Entries+", generate_obsolete, out)
output_table("+Errors+", generate_errors, out)
output_table("+SECURITY WARNINGS+", generate_warnings, out)
output_table("Controller Warnings:", generate_controller_warnings, out)
Expand Down
5 changes: 5 additions & 0 deletions lib/brakeman/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ def filtered_warnings
end
end

def unused_fingerprints
return [] unless self.ignored_filter
self.ignored_filter.obsolete_fingerprints
end

def add_constant name, value, context = nil
@constants.add name, value, context unless @options[:disable_constant_tracking]
end
Expand Down
18 changes: 18 additions & 0 deletions test/apps/rails4/config/brakeman.ignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@
"user_input": null,
"confidence": "High",
"note": "skipping this for a test"
},
{
"warning_type": "Mass Assignment",
"warning_code": 60,
"fingerprint": "abcdef01234567890ba28050e7faf1d54f218dfa9435c3f65f47cb378c18cf98",
"message": "Potentially dangerous attribute available for mass assignment",
"file": "app/models/account.rb",
"line": null,
"link": "http://brakemanscanner.org/docs/warning_types/mass_assignment/",
"code": ":admin",
"render_path": null,
"location": {
"type": "model",
"model": "Account"
},
"user_input": null,
"confidence": "High",
"note": "this error should be detected as obsolete"
}
],
"updated": "2013-12-20 22:14:42 +0200",
Expand Down
8 changes: 6 additions & 2 deletions test/tests/json_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class JSONOutputTests < Minitest::Test
def setup
@@json ||= JSON.parse(Brakeman.run("#{TEST_PATH}/apps/rails3.2").report.to_json)
@@json ||= JSON.parse(Brakeman.run("#{TEST_PATH}/apps/rails4").report.to_json)
end

def test_for_render_path
Expand All @@ -13,7 +13,7 @@ def test_for_render_path
end

def test_for_expected_keys
assert (@@json.keys - ["warnings", "ignored_warnings", "scan_info", "errors"]).empty?
assert (@@json.keys - ["warnings", "ignored_warnings", "scan_info", "errors", "obsolete"]).empty?
end

def test_for_scan_info_keys
Expand All @@ -37,6 +37,10 @@ def test_for_errors
assert @@json["errors"].is_a? Array
end

def test_for_obsolete
assert_equal ["abcdef01234567890ba28050e7faf1d54f218dfa9435c3f65f47cb378c18cf98"], @@json["obsolete"]
end

def test_paths
assert @@json["warnings"].all? { |w| not w["file"].start_with? "/" }
end
Expand Down
9 changes: 8 additions & 1 deletion test/tests/report_generation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class TestReportGeneration < Minitest::Test
def setup
@@tracker||= Brakeman.run(:app_path => "#{TEST_PATH}/apps/rails3.2", :quiet => true, :report_routes => true)
@@tracker||= Brakeman.run(:app_path => "#{TEST_PATH}/apps/rails4", :quiet => true, :report_routes => true)
@@report ||= @@tracker.report
end

Expand Down Expand Up @@ -49,6 +49,13 @@ def test_csv_report_no_warnings
end
end

def test_obsolete_reporting
report = @@report.to_s

assert report.include? "+Obsolete Ignore Entries+"
assert report.include? "abcdef01234567890ba28050e7faf1d54f218dfa9435c3f65f47cb378c18cf98"
end

def test_tabs_sanity
report = @@report.to_tabs

Expand Down

0 comments on commit 7ff8b5e

Please sign in to comment.