Skip to content

Commit fc0919e

Browse files
committed
Add criticality filtering to cli
Why? When utilizing bundler-audit on CI, it can be helpful to filter criticality to find higher criticality gems. What? * Adds filter (--filter or -f) command line argument that accepts array of values * Filters within Audit::Scanner to remove filtered out advisories
1 parent 31dc79b commit fc0919e

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ Ignore specific advisories:
116116

117117
$ bundle-audit check --ignore OSVDB-108664
118118

119+
Filter results by criticality level (low, medium, high):
120+
121+
$ bundle-audit check --filter medium high
122+
119123
Rake task:
120124

121125
```ruby

lib/bundler/audit/cli.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ class CLI < ::Thor
3434
method_option :verbose, :type => :boolean, :aliases => '-v'
3535
method_option :ignore, :type => :array, :aliases => '-i'
3636
method_option :update, :type => :boolean, :aliases => '-u'
37+
method_option :filter, :type => :array, :aliases => '-f'
3738

3839
def check
3940
update if options[:update]
4041

4142
scanner = Scanner.new
4243
vulnerable = false
4344

44-
scanner.scan(:ignore => options.ignore) do |result|
45+
scanner.scan(ignore: options[:ignore], filter: options[:filter]) do |result|
4546
vulnerable = true
4647

4748
case result

lib/bundler/audit/scanner.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ def initialize(root=Dir.pwd,gemfile_lock='Gemfile.lock')
5656
# @option options [Array<String>] :ignore
5757
# The advisories to ignore.
5858
#
59+
# @option options [Array<String>] :filter
60+
# The criticalities to filter.
61+
#
5962
# @yield [result]
6063
# The given block will be passed the results of the scan.
6164
#
@@ -68,13 +71,10 @@ def initialize(root=Dir.pwd,gemfile_lock='Gemfile.lock')
6871
def scan(options={},&block)
6972
return enum_for(__method__,options) unless block
7073

71-
ignore = Set[]
72-
ignore += options[:ignore] if options[:ignore]
73-
7474
scan_sources(options,&block)
7575
scan_specs(options,&block)
7676

77-
return self
77+
self
7878
end
7979

8080
#
@@ -127,6 +127,9 @@ def scan_sources(options={})
127127
# @option options [Array<String>] :ignore
128128
# The advisories to ignore.
129129
#
130+
# @option options [Array<String>] :filter
131+
# The criticalities to filter.
132+
#
130133
# @yield [result]
131134
# The given block will be passed the results of the scan.
132135
#
@@ -146,11 +149,16 @@ def scan_specs(options={})
146149
ignore = Set[]
147150
ignore += options[:ignore] if options[:ignore]
148151

152+
filter = Set[]
153+
filter += options[:filter].map! { |current_filter| current_filter.downcase.to_sym } if options[:filter]
154+
149155
@lockfile.specs.each do |gem|
150156
@database.check_gem(gem) do |advisory|
151157
is_ignored = ignore.intersect?(advisory.identifiers.to_set)
152158
next if is_ignored
153159

160+
next unless filter.empty? || filter.include?(advisory.criticality)
161+
154162
yield UnpatchedGem.new(gem,advisory)
155163
end
156164
end

spec/integration_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@
5050
end
5151
end
5252

53+
context "when auditing a bundle with filtered criticality" do
54+
let(:bundle) { 'unpatched_gems' }
55+
let(:directory) { File.join('spec','bundle',bundle) }
56+
57+
let(:command) do
58+
File.expand_path(File.join(File.dirname(__FILE__),'..','bin','bundler-audit -f medium'))
59+
end
60+
61+
subject do
62+
Dir.chdir(directory) { sh(command, :fail => true) }
63+
end
64+
65+
it "should print advisories for filtered criticality" do
66+
expect(subject).not_to include("Criticality: High")
67+
expect(subject).to include("Criticality: Medium")
68+
end
69+
end
70+
5371
context "when auditing a bundle with insecure sources" do
5472
let(:bundle) { 'insecure_sources' }
5573
let(:directory) { File.join('spec','bundle',bundle) }

spec/scanner_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646
expect(ids).not_to include('OSVDB-89025')
4747
end
4848
end
49+
50+
context "when the :filter option is given" do
51+
subject { scanner.scan(filter: ['high']) }
52+
53+
it "should return only filtered criticalities" do
54+
criticalities = subject.map { |result| result.advisory.criticality }
55+
expect(criticalities).not_to include(:medium, :low, nil)
56+
expect(criticalities).to include(:high)
57+
end
58+
end
4959
end
5060

5161
context "when auditing a bundle with insecure sources" do

0 commit comments

Comments
 (0)