Skip to content

Commit

Permalink
Whitelisting of matches
Browse files Browse the repository at this point in the history
  • Loading branch information
alxberardi committed Dec 8, 2016
1 parent 8bdac29 commit e66c9a8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ This changelog adheres to [Keep a CHANGELOG](http://keepachangelog.com/).

## [Unreleased]
### Added
- Whitelisting of matches
- Initial release
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ SensitiveDataFilter.config do |config|
config.enable_types :credit_card # Already defaults to :credit_card if not specified
config.on_occurrence do |occurrence|
# Report occurrence
end
end
config.whitelist pattern1, pattern2 # Allows specifying patterns to whitelist matches
end
```

Expand Down
12 changes: 12 additions & 0 deletions lib/sensitive_data_filter/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def self.handle_occurrence(occurrence)
handler.call(occurrence) if handler
end

def self.whitelisted?(value)
config.whitelist_patterns.any? { |pattern| value.match pattern }
end

class Config
DEFAULT_TYPES = %i(credit_card).freeze

Expand All @@ -32,5 +36,13 @@ def enabled_types
def on_occurrence(&block)
@occurrence_handler = block
end

def whitelist(*patterns)
@whitelist_patterns = patterns
end

def whitelist_patterns
@whitelist_patterns ||= []
end
end
end
8 changes: 7 additions & 1 deletion lib/sensitive_data_filter/scan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ def initialize(value)

def matches
@matches ||= SensitiveDataFilter.enabled_types.map.with_object({}) { |scanner, matches|
matches[scanner.name.split('::').last] = scanner.scan @value
matches[scanner.name.split('::').last] = whitelist scanner.scan(@value)
}
end

def matches?
matches.values.any?(&:present?)
end

private

def whitelist(matches)
matches.reject { |match| SensitiveDataFilter.whitelisted? match }
end
end
end
16 changes: 16 additions & 0 deletions spec/sensitive_data/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,20 @@
specify { expect(handler).to have_received(:handle).with occurrence }
end
end

describe '#whitelisted?' do
context 'when a whitelist is configured' do
before do
SensitiveDataFilter.config do |config|
config.whitelist 'is allowed', 'is acceptable'
end
end

let(:allowed_value) { 'this is allowed' }
let(:non_allowed_value) { 'this is not allowed' }

specify { expect(SensitiveDataFilter.whitelisted?(allowed_value)).to be true }
specify { expect(SensitiveDataFilter.whitelisted?(non_allowed_value)).to be false }
end
end
end
8 changes: 8 additions & 0 deletions spec/sensitive_data/scan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

describe SensitiveDataFilter::Scan do
let(:enabled_types) { [SensitiveDataFilter::Types::CreditCard] }
let(:whitelisted?) { false }

let(:credit_card_scanner) { double name: 'CreditCard', scan: matches }

before do
stub_const 'SensitiveDataFilter::Types::CreditCard', credit_card_scanner
allow(SensitiveDataFilter).to receive(:enabled_types).and_return enabled_types
allow(SensitiveDataFilter).to receive(:whitelisted?).and_return whitelisted?
end

let(:scan) { SensitiveDataFilter::Scan.new(value) }
Expand All @@ -27,6 +29,12 @@
specify { expect(scan.matches?).to be false }
specify { expect(scan.matches).to be_empty }
end

context 'when the matches are whitelisted' do
let(:whitelisted?) { true }
specify { expect(scan.matches?).to be false }
specify { expect(scan.matches).to eq 'CreditCard' => [] }
end
end

context 'when there are no matches' do
Expand Down

0 comments on commit e66c9a8

Please sign in to comment.