Skip to content
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
4 changes: 4 additions & 0 deletions lib/raven/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ class Configuration
# Provide a configurable callback to block or send events
attr_accessor :should_send

# additional fields to sanitize
attr_accessor :sanitize_fields

IGNORE_DEFAULT = ['ActiveRecord::RecordNotFound',
'ActionController::RoutingError',
'ActionController::InvalidAuthenticityToken',
Expand All @@ -105,6 +108,7 @@ def initialize
self.tags = {}
self.async = false
self.catch_debugged_exceptions = true
self.sanitize_fields = []
end

def server=(value)
Expand Down
3 changes: 3 additions & 0 deletions lib/raven/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

module Raven
class Processor
attr_accessor :sanitize_fields

def initialize(client)
@client = client
@sanitize_fields = client.configuration.sanitize_fields
end

def process(data)
Expand Down
8 changes: 5 additions & 3 deletions lib/raven/processor/sanitizedata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ module Raven
class Processor::SanitizeData < Processor
STRING_MASK = '********'
INT_MASK = 0
FIELDS_RE = /(authorization|password|passwd|secret|ssn|social(.*)?sec)/i
DEFAULT_FIELDS = %w(authorization password passwd secret ssn social(.*)?sec)
VALUES_RE = /^\d{16}$/

def process(value)
fields_re = /(#{(DEFAULT_FIELDS + @sanitize_fields).join("|")})/i

value.inject(value) do |value,(k,v)|
v = k if v.nil?
if v.is_a?(Hash) || v.is_a?(Array)
process(v)
elsif v.is_a?(String) && (json = parse_json_or_nil(v))
#if this string is actually a json obj, convert and sanitize
value = modify_in_place(value, [k,v], process(json).to_json)
elsif v.is_a?(Integer) && (VALUES_RE.match(v.to_s) || FIELDS_RE.match(k.to_s))
elsif v.is_a?(Integer) && (VALUES_RE.match(v.to_s) || fields_re.match(k.to_s))
value = modify_in_place(value, [k,v], INT_MASK)
elsif VALUES_RE.match(v.to_s) || FIELDS_RE.match(k.to_s)
elsif VALUES_RE.match(v.to_s) || fields_re.match(k.to_s)
value = modify_in_place(value, [k,v], STRING_MASK)
else
value
Expand Down
4 changes: 4 additions & 0 deletions spec/raven/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
it 'should catch_debugged_exceptions' do
expect(subject[:catch_debugged_exceptions]).to eq(true)
end

it 'should have no sanitize fields' do
expect(subject[:sanitize_fields]).to eq([])
end
end

context 'being initialized with a server string' do
Expand Down
1 change: 1 addition & 0 deletions spec/raven/removecirculareferences_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
describe Raven::Processor::RemoveCircularReferences do
before do
@client = double("client")
allow(@client).to receive_message_chain(:configuration, :sanitize_fields) { [] }
@processor = Raven::Processor::RemoveCircularReferences.new(@client)
end

Expand Down
9 changes: 7 additions & 2 deletions spec/raven/sanitizedata_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
describe Raven::Processor::SanitizeData do
before do
@client = double("client")
allow(@client).to receive_message_chain(:configuration, :sanitize_fields) { ['user_field'] }
@processor = Raven::Processor::SanitizeData.new(@client)
end

Expand All @@ -17,7 +18,8 @@
'mypasswd' => 'hello',
'test' => 1,
'ssn' => '123-45-6789',
'social_security_number' => 123456789
'social_security_number' => 123456789,
'user_field' => 'user'
}
}
}
Expand All @@ -33,6 +35,7 @@
expect(vars["test"]).to eq(1)
expect(vars["ssn"]).to eq(Raven::Processor::SanitizeData::STRING_MASK)
expect(vars["social_security_number"]).to eq(Raven::Processor::SanitizeData::INT_MASK)
expect(vars["user_field"]).to eq(Raven::Processor::SanitizeData::STRING_MASK)
end

it 'should filter json data' do
Expand All @@ -45,7 +48,8 @@
'mypasswd' => 'hello',
'test' => 1,
'ssn' => '123-45-6789',
'social_security_number' => 123456789
'social_security_number' => 123456789,
'user_field' => 'user'
}.to_json
}

Expand All @@ -60,6 +64,7 @@
expect(vars["test"]).to eq(1)
expect(vars["ssn"]).to eq(Raven::Processor::SanitizeData::STRING_MASK)
expect(vars["social_security_number"]).to eq(Raven::Processor::SanitizeData::INT_MASK)
expect(vars["user_field"]).to eq(Raven::Processor::SanitizeData::STRING_MASK)
end

it 'should filter json embedded in a ruby object' do
Expand Down
1 change: 1 addition & 0 deletions spec/raven/utf8conversion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
describe Raven::Processor::UTF8Conversion do
before do
@client = double("client")
allow(@client).to receive_message_chain(:configuration, :sanitize_fields) { [] }
@processor = Raven::Processor::UTF8Conversion.new(@client)
end

Expand Down