From 669e1458da2b080db5a18ad93fc172c603a70046 Mon Sep 17 00:00:00 2001 From: erwanlr Date: Wed, 24 Jul 2013 14:18:02 +0200 Subject: [PATCH] Fix #208 - Fixed vulnerable plugins still appear in the results --- lib/common/collections/wp_items/detectable.rb | 4 +++- lib/common/models/wp_item/vulnerable.rb | 4 ++++ spec/shared_examples/wp_item_vulnerable.rb | 17 +++++++++++++++++ spec/shared_examples/wp_items_detectable.rb | 16 ++++++++++++---- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/common/collections/wp_items/detectable.rb b/lib/common/collections/wp_items/detectable.rb index ccd82bd5..7f08fa7d 100755 --- a/lib/common/collections/wp_items/detectable.rb +++ b/lib/common/collections/wp_items/detectable.rb @@ -35,7 +35,9 @@ class WpItems < Array if target_item.exists?(exist_options, response) if !results.include?(target_item) - results << target_item + if !options[:only_vulnerable] || options[:only_vulnerable] && target_item.vulnerable? + results << target_item + end end end end diff --git a/lib/common/models/wp_item/vulnerable.rb b/lib/common/models/wp_item/vulnerable.rb index ffe2b1c6..b2f6bbf5 100755 --- a/lib/common/models/wp_item/vulnerable.rb +++ b/lib/common/models/wp_item/vulnerable.rb @@ -21,6 +21,10 @@ class WpItem vulnerabilities end + def vulnerable? + vulnerabilities.empty? ? false : true + end + # Checks if a item is vulnerable to a specific vulnerability # # @param [ Vulnerability ] vuln Vulnerability to check the item against diff --git a/spec/shared_examples/wp_item_vulnerable.rb b/spec/shared_examples/wp_item_vulnerable.rb index b42d196e..b08708b4 100644 --- a/spec/shared_examples/wp_item_vulnerable.rb +++ b/spec/shared_examples/wp_item_vulnerable.rb @@ -39,6 +39,23 @@ shared_examples 'WpItem::Vulnerable' do end end + describe '#vulnerable?' do + after do + subject.stub(:vulnerabilities).and_return(@stub) + subject.vulnerable?.should == @expected + end + + it 'returns false when no vulnerabilities' do + @stub = [] + @expected = false + end + + it 'returns true when vulnerabilities' do + @stub = ['not empty'] + @expected = true + end + end + describe '#vulnerable_to?' do let(:version_orig) { '1.5.6' } let(:version_newer) { '1.6' } diff --git a/spec/shared_examples/wp_items_detectable.rb b/spec/shared_examples/wp_items_detectable.rb index 66473ce2..979e00f7 100644 --- a/spec/shared_examples/wp_items_detectable.rb +++ b/spec/shared_examples/wp_items_detectable.rb @@ -178,12 +178,20 @@ shared_examples 'WpItems::Detectable' do let(:options) { { only_vulnerable: true } } let(:targets) { expected[:vulnerable_targets_items] } - it 'only checks vulnerable targets' do - target = targets.sample - @expected = subject.new << target + it 'only checks and return vulnerable targets' do + samples = targets.sample(2) + fixed_target = samples[0] + vulnerable_target = samples[1] stub_targets_dont_exist(targets) - target.stub(:exists?).and_return(true) + + vulnerable_target.stub(:exists?).and_return(true) + vulnerable_target.stub(:vulnerable?).and_return(true) + + fixed_target.stub(:exists?).and_return(true) + fixed_target.stub(:vulnerable?).and_return(false) + + @expected = subject.new << vulnerable_target subject.should_receive(:targets_items).and_return(targets) end