Skip to content

Commit 8aede7e

Browse files
committed
Merge pull request #228 from getsentry/fix-issue-227
Fix issue #227, where JSON arrays were blowing up SanitizeData
2 parents e7c0d73 + 6fc28c1 commit 8aede7e

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

lib/raven/processor/sanitizedata.rb

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,34 @@ class Processor::SanitizeData < Processor
66
VALUES_RE = /^\d{16}$/
77

88
def process(value)
9-
value.merge(value) do |k, v|
10-
if v.is_a?(Hash)
9+
value.inject(value) do |value,(k,v)|
10+
v = k if v.nil?
11+
if v.is_a?(Hash) || v.is_a?(Array)
1112
process(v)
12-
elsif v.is_a?(String) && (json_hash = parse_json_or_nil(v))
13+
elsif v.is_a?(String) && (json = parse_json_or_nil(v))
1314
#if this string is actually a json obj, convert and sanitize
14-
process(json_hash).to_json
15-
elsif v.is_a?(Integer) && (VALUES_RE.match(v.to_s) || FIELDS_RE.match(k))
16-
INT_MASK
17-
elsif VALUES_RE.match(v.to_s) || FIELDS_RE.match(k)
18-
STRING_MASK
15+
value = modify_in_place(value, [k,v], process(json).to_json)
16+
elsif v.is_a?(Integer) && (VALUES_RE.match(v.to_s) || FIELDS_RE.match(k.to_s))
17+
value = modify_in_place(value, [k,v], INT_MASK)
18+
elsif VALUES_RE.match(v.to_s) || FIELDS_RE.match(k.to_s)
19+
value = modify_in_place(value, [k,v], STRING_MASK)
1920
else
20-
v
21+
value
2122
end
2223
end
24+
value
25+
end
26+
27+
private
28+
29+
def modify_in_place(original_parent, original_child, new_child)
30+
if original_parent.is_a?(Array)
31+
index = original_parent.index(original_child[0])
32+
original_parent[index] = new_child
33+
elsif original_parent.is_a?(Hash)
34+
original_parent[original_child[0]] = new_child
35+
end
36+
original_parent
2337
end
2438
end
2539
end

spec/raven/sanitizedata_processor_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@
6262
expect(vars["social_security_number"]).to eq(Raven::Processor::SanitizeData::INT_MASK)
6363
end
6464

65+
it 'should filter json embedded in a ruby object' do
66+
data_with_embedded_json = {
67+
'data' => {
68+
'json' => ['foo','bar'].to_json,
69+
'json_hash' => {'foo' => 'bar'}.to_json,
70+
'sensitive' => {'password' => 'secret'}.to_json
71+
}
72+
}
73+
74+
result = @processor.process(data_with_embedded_json)
75+
76+
expect(JSON.parse(result["data"]["json"])).to eq(['foo','bar'])
77+
expect(JSON.parse(result["data"]["json_hash"])).to eq({'foo' => 'bar'})
78+
expect(JSON.parse(result["data"]["sensitive"])).to eq({'password' => Raven::Processor::SanitizeData::STRING_MASK})
79+
end
80+
6581
it 'should filter credit card values' do
6682
data = {
6783
'ccnumba' => '4242424242424242',

0 commit comments

Comments
 (0)