Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filters: filter context payload #55

Merged
merged 1 commit into from
Mar 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Airbrake Ruby Changelog

### master

* Started filtering the context payload
([#55](https://github.com/airbrake/airbrake-ruby/pull/55))
* Fixed bug when similar keys would be filtered out using non-regexp values for
`Airbrake.blacklist/whitelist_keys`
([#54](https://github.com/airbrake/airbrake-ruby/pull/54))
Expand Down
10 changes: 9 additions & 1 deletion lib/airbrake-ruby/filters/keys_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ module Filters
# @see KeysWhitelist
# @see KeysBlacklist
module KeysFilter
##
# @return [String] The label to replace real values of filtered payload
FILTERED = '[Filtered]'.freeze

##
# Creates a new KeysBlacklist or KeysWhitelist filter that uses the given
# +patterns+ for filtering a notice's payload.
Expand All @@ -28,6 +32,10 @@ def initialize(*patterns)
def call(notice)
FILTERABLE_KEYS.each { |key| filter_hash(notice[key]) }

if notice[:context][:user] && should_filter?(:user)
notice[:context][:user] = FILTERED
end

return unless notice[:context][:url]
url = URI(notice[:context][:url])
return if url.nil? || url.query.nil?
Expand All @@ -46,7 +54,7 @@ def should_filter?(_key)
def filter_hash(hash)
hash.each_key do |key|
if should_filter?(key)
hash[key] = '[Filtered]'.freeze
hash[key] = FILTERED
elsif hash[key].is_a?(Hash)
filter_hash(hash[key])
end
Expand Down
11 changes: 11 additions & 0 deletions spec/notifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,17 @@ def to_json(*)
with(body: expected_body)
).to have_been_made.once
end

it "filters out user" do
@airbrake.blacklist_keys('user')

notice = @airbrake.build_notice(ex)
notice[:context][:user] = { id: 1337, name: 'Bingo Bango' }

@airbrake.notify_sync(notice)

expect_a_request_with_body(/"user":"\[Filtered\]"/)
end
end

describe "#whitelist_keys" do
Expand Down