Skip to content

Commit

Permalink
Rename original and filtered to dirty and clean
Browse files Browse the repository at this point in the history
This allows a developer to better grasp the purpose of objects within the library. It provides clear indication that the original values are dangerous to use.
  • Loading branch information
blaknite committed Dec 8, 2016
1 parent 25b121e commit 36291d4
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 67 deletions.
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,25 @@ config.middleware.use SensitiveDataFilter::Middleware::Filter
```ruby
SensitiveDataFilter.config do |config|
config.enable_types :credit_card # Already defaults to :credit_card if not specified
config.on_occurrence do |occurrence|
config.on_occurrence do |occurrence|
# Report occurrence
end
end
end
```

An occurrence object has the following properties:

* origin_ip: the IP address that originated the request
* request_method: the HTTP method for the request (GET, POST, etc.)
* url: the URL of the request
* original_params: the parameters sent with the request
* filtered_params: the parameters sent with the request, with sensitive data filtered
* session: the session properties for the request
* matches: the matched sensitive data
* matches_count: the number of matches per data type, e.g. { 'CreditCard' => 1 }
* origin_ip: the IP address that originated the request
* request_method: the HTTP method for the request (GET, POST, etc.)
* url: the URL of the request
* dirty_params: the parameters sent with the request
* clean_params: the parameters sent with the request, with sensitive data clean
* session: the session properties for the request
* matches: the matched sensitive data
* matches_count: the number of matches per data type, e.g. { 'CreditCard' => 1 }

It also exposes `to_h` and `to_s` methods for hash and string representation respectively.
Please note that these representations omit sensitive data, i.e. `original_params` and `matches` are not included.
Please note that these representations omit sensitive data, i.e. `dirty_params` and `matches` are not included.

#### Important Note

Expand All @@ -68,7 +68,7 @@ In Rails you can do something like:
```ruby
filters = Rails.application.config.filter_parameters
filter = ActionDispatch::Http::ParameterFilter.new filters
filter.filter @occurrence.filtered_params
filter.filter @occurrence.clean_params
```

## Development
Expand All @@ -85,4 +85,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/sealin
## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

14 changes: 7 additions & 7 deletions lib/sensitive_data_filter/middleware/env_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ class EnvFilter

def initialize(env)
@env = env
@original_env_parser = EnvParser.new(env)
@scanner = ParameterScanner.new(@original_env_parser)
@filtered_env_parser = @original_env_parser.copy
@filtered_env_parser.mask! if @scanner.sensitive_data?
@dirty_env_parser = EnvParser.new(env)
@scanner = ParameterScanner.new(@dirty_env_parser)
@clean_env_parser = @dirty_env_parser.copy
@clean_env_parser.mask! if @scanner.sensitive_data?
@occurrence = build_occurrence
end

def filtered_env
@filtered_env_parser.env
def clean_env
@clean_env_parser.env
end

def occurrence?
Expand All @@ -27,7 +27,7 @@ def occurrence?

def build_occurrence
if @scanner.sensitive_data?
Occurrence.new(@original_env_parser, @filtered_env_parser, @scanner.matches)
Occurrence.new(@dirty_env_parser, @clean_env_parser, @scanner.matches)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sensitive_data_filter/middleware/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(app)
def call(env)
env_filter = EnvFilter.new env
handle_occurrence env_filter
@app.call env_filter.filtered_env
@app.call env_filter.clean_env
end

private
Expand Down
32 changes: 16 additions & 16 deletions lib/sensitive_data_filter/middleware/occurrence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,38 @@ class Occurrence

attr_reader :matches

def initialize(original_env_parser, filtered_env_parser, matches)
@original_env_parser = original_env_parser
@filtered_env_parser = filtered_env_parser
@matches = matches
def initialize(dirty_env_parser, clean_env_parser, matches)
@dirty_env_parser = dirty_env_parser
@clean_env_parser = clean_env_parser
@matches = matches
end

def origin_ip
@original_env_parser.ip
@dirty_env_parser.ip
end

def original_params
@original_env_parser.params
def dirty_params
@dirty_env_parser.params
end

def filtered_params
@filtered_env_parser.params
def clean_params
@clean_env_parser.params
end

def_delegators :@original_env_parser, :request_method, :url, :session
def_delegators :@dirty_env_parser, :request_method, :url, :session

def matches_count
@matches.map { |type, matches| [type, matches.count] }.to_h
end

def to_h
{
origin_ip: origin_ip,
request_method: request_method,
url: url,
filtered_params: filtered_params,
session: session,
matches_count: matches_count
origin_ip: origin_ip,
request_method: request_method,
url: url,
clean_params: clean_params,
session: session,
matches_count: matches_count
}
end

Expand Down
6 changes: 3 additions & 3 deletions spec/sensitive_data/mask_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@

describe '#mask_hash' do
let(:hash) { { a: nil, b: 42, c: 'unmasked' } }
let(:original_hash) { hash.dup }
let(:dirty_hash) { hash.dup }
let(:expected_result) { { a: nil, b: 42, c: masked_value } }
let(:result) { SensitiveDataFilter::Mask.mask_hash hash }

before do
original_hash
dirty_hash
result
end

specify { expect(result).to eq expected_result }
specify { expect(hash).to eq original_hash }
specify { expect(hash).to eq dirty_hash }
end
end
10 changes: 5 additions & 5 deletions spec/sensitive_data/middleware/env_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

describe SensitiveDataFilter::Middleware::EnvFilter do
let(:env_parser_class) { double }
let(:env_parser) { double 'original_env_parser' }
let(:env_parser_copy) { double 'filtered_env_parser', env: filtered_env }
let(:filtered_env) { double 'filtered_env' }
let(:env_parser) { double 'dirty_env_parser' }
let(:env_parser_copy) { double 'clean_env_parser', env: clean_env }
let(:clean_env) { double 'clean_env' }

let(:parameter_scanner_class) { double }
let(:parameter_scanner) { double sensitive_data?: sensitive_data?, matches: scan_matches }
Expand Down Expand Up @@ -45,14 +45,14 @@
specify { expect(env_parser_copy).to have_received :mask! }
specify { expect(env_filter.occurrence?).to be true }
specify { expect(env_filter.occurrence).to eq occurrence }
specify { expect(env_filter.filtered_env).to eq filtered_env }
specify { expect(env_filter.clean_env).to eq clean_env }
end

context 'when sensitive data is not detected' do
let(:sensitive_data?) { false }
specify { expect(env_parser_copy).to_not have_received :mask! }
specify { expect(env_filter.occurrence?).to be false }
specify { expect(env_filter.occurrence).to be_nil }
specify { expect(env_filter.filtered_env).to eq filtered_env }
specify { expect(env_filter.clean_env).to eq clean_env }
end
end
8 changes: 4 additions & 4 deletions spec/sensitive_data/middleware/env_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@
let(:method) { 'POST' }
let(:input) { 'credit_card=4111 1111 1111 1111' }

let(:filtered_query_params) { { 'id' => '42', 'credit_card' => '[FILTERED]' } }
let(:filtered_body_params) { { 'credit_card' => '[FILTERED]' } }
let(:clean_query_params) { { 'id' => '42', 'credit_card' => '[FILTERED]' } }
let(:clean_body_params) { { 'credit_card' => '[FILTERED]' } }

before do
env_parser.mask!
end

specify { expect(env_parser.query_params).to eq filtered_query_params }
specify { expect(env_parser.body_params).to eq filtered_body_params }
specify { expect(env_parser.query_params).to eq clean_query_params }
specify { expect(env_parser.body_params).to eq clean_body_params }
end
end
12 changes: 6 additions & 6 deletions spec/sensitive_data/middleware/filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
describe SensitiveDataFilter::Middleware::Filter do
let(:env_filter_class) { double }
let(:env_filter) {
double occurrence?: occurrence?, occurrence: occurrence, filtered_env: filtered_env
double occurrence?: occurrence?, occurrence: occurrence, clean_env: clean_env
}
let(:occurrence) { double }
let(:filtered_env) { double 'filtered_env' }
let(:occurrence) { double }
let(:clean_env) { double 'clean_env' }

let(:app) { double }
let(:middleware) { SensitiveDataFilter::Middleware::Filter }
Expand All @@ -21,19 +21,19 @@
stub_const 'SensitiveDataFilter::Middleware::EnvFilter', env_filter_class
allow(env_filter_class).to receive(:new).with(env).and_return env_filter
allow(SensitiveDataFilter).to receive(:handle_occurrence).with occurrence
allow(app).to receive(:call).with filtered_env
allow(app).to receive(:call).with clean_env
stack.call(env)
end

context 'when an occurrence is detected' do
let(:occurrence?) { true }
specify { expect(SensitiveDataFilter).to have_received(:handle_occurrence).with occurrence }
specify { expect(app).to have_received(:call).with filtered_env }
specify { expect(app).to have_received(:call).with clean_env }
end

context 'when sensitive data is detected' do
let(:occurrence?) { false }
specify { expect(SensitiveDataFilter).not_to have_received(:handle_occurrence) }
specify { expect(app).to have_received(:call).with filtered_env }
specify { expect(app).to have_received(:call).with clean_env }
end
end
24 changes: 12 additions & 12 deletions spec/sensitive_data/middleware/occurrence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
let(:ip) { '127.0.0.1' }
let(:request_method) { 'POST' }
let(:url) { 'https://test.example.com.au/test' }
let(:original_params) { { credit_cards: '4111 1111 1111 1111 and 5123 4567 8901 2346' } }
let(:filtered_params) { { credit_cards: '[FILTERED] and [FILTERED]' } }
let(:dirty_params) { { credit_cards: '4111 1111 1111 1111 and 5123 4567 8901 2346' } }
let(:clean_params) { { credit_cards: '[FILTERED] and [FILTERED]' } }
let(:session) { { 'session_id' => '01ab02cd' } }
let(:original_env_parser) {
let(:dirty_env_parser) {
double(
ip: ip,
request_method: request_method,
url: url,
params: original_params,
params: dirty_params,
session: session
)
}
let(:filtered_env_parser) {
let(:clean_env_parser) {
double(
ip: ip,
request_method: request_method,
url: url,
params: filtered_params,
params: clean_params,
session: session
)
}
Expand All @@ -37,8 +37,8 @@
let(:matches_count) { { 'CreditCard' => 2 } }
subject(:occurrence) {
SensitiveDataFilter::Middleware::Occurrence.new(
original_env_parser,
filtered_env_parser,
dirty_env_parser,
clean_env_parser,
matches
)
}
Expand All @@ -47,8 +47,8 @@
specify { expect(occurrence.origin_ip).to eq ip }
specify { expect(occurrence.request_method).to eq request_method }
specify { expect(occurrence.url).to eq url }
specify { expect(occurrence.original_params).to eq original_params }
specify { expect(occurrence.filtered_params).to eq filtered_params }
specify { expect(occurrence.dirty_params).to eq dirty_params }
specify { expect(occurrence.clean_params).to eq clean_params }
specify { expect(occurrence.session).to eq session }
specify { expect(occurrence.matches_count).to eq matches_count }

Expand All @@ -57,7 +57,7 @@
origin_ip: ip,
request_method: request_method,
url: url,
filtered_params: filtered_params,
clean_params: clean_params,
session: session,
matches_count: matches_count
}
Expand All @@ -68,7 +68,7 @@
"Origin Ip: 127.0.0.1\n"\
"Request Method: POST\n"\
"Url: https://test.example.com.au/test\n"\
"Filtered Params: {:credit_cards=>\"[FILTERED] and [FILTERED]\"}\n"\
"Clean Params: {:credit_cards=>\"[FILTERED] and [FILTERED]\"}\n"\
"Session: {\"session_id\"=>\"01ab02cd\"}\n"\
"Matches Count: {\"CreditCard\"=>2}"
}
Expand Down

0 comments on commit 36291d4

Please sign in to comment.