Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit f2aec52

Browse files
dhruv-aggarwalashwoods
authored andcommitted
Mask Complete dictionary instead of just the leaves (#1198)
* Mask even if value is a dictionary * Update varmap to mask entire dict based on key * Add test case for complete dict masking
1 parent 6d05d48 commit f2aec52

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

raven/utils/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ def varmap(func, var, context=None, name=None):
4343
if objid in context:
4444
return func(name, '<...>')
4545
context[objid] = 1
46-
if isinstance(var, dict):
47-
ret = dict((k, varmap(func, v, context, k))
48-
for k, v in iteritems(var))
49-
elif isinstance(var, (list, tuple)):
46+
if isinstance(var, (list, tuple)):
5047
ret = [varmap(func, f, context, name) for f in var]
5148
else:
5249
ret = func(name, var)
50+
if isinstance(ret, dict):
51+
ret = dict((k, varmap(func, v, context, k))
52+
for k, v in iteritems(var))
5353
del context[objid]
5454
return ret
5555

tests/processors/tests.py

+21-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
'access_token': 'oauth2 access token',
2424
'custom_key1': 'you should not see this',
2525
'custom_key2': 'you should not see this',
26+
'custom_key3': {'mask': 'This entire dict'}
2627
}
2728

2829

@@ -37,6 +38,7 @@ def _will_throw_type_error(foo, **kwargs):
3738
access_token = "secret stuff!" # NOQA F841
3839
custom_key1 = "you shouldn't see this" # NOQA F841
3940
custom_key2 = "you shouldn't see this" # NOQA F841
41+
custom_key3 = "you shouldn't see this" # NOQA F841
4042

4143
# TypeError: unsupported operand type(s) for /: 'str' and 'str'
4244
raise exception_class()
@@ -84,7 +86,9 @@ def get_extra_data():
8486
class SanitizeKeysProcessorTest(TestCase):
8587

8688
def setUp(self):
87-
client = Mock(sanitize_keys=['custom_key1', 'custom_key2'])
89+
client = Mock(
90+
sanitize_keys=['custom_key1', 'custom_key2', 'custom_key3']
91+
)
8892
self.proc = SanitizeKeysProcessor(client)
8993

9094
def _check_vars_sanitized(self, vars, MASK):
@@ -95,6 +99,8 @@ def _check_vars_sanitized(self, vars, MASK):
9599
self.assertEquals(vars['custom_key1'], MASK)
96100
self.assertTrue('custom_key2' in vars)
97101
self.assertEquals(vars['custom_key2'], MASK)
102+
self.assertTrue('custom_key3' in vars)
103+
self.assertEquals(vars['custom_key3'], MASK)
98104

99105
def test_stacktrace(self, *args, **kwargs):
100106
data = get_stack_trace_data_real()
@@ -131,14 +137,15 @@ def test_extra(self):
131137

132138
def test_querystring_as_string(self):
133139
data = get_http_data()
134-
data['request']['query_string'] = 'foo=bar&custom_key1=nope&custom_key2=nope'
140+
data['request']['query_string'] = 'foo=bar&custom_key1=nope&custom_key2=nope&custom_key3=%7B%27key2%27%3A+%27nope%27%2C+%27key1%27%3A+%27nope%27%7D'
135141
result = self.proc.process(data)
136142

137143
self.assertTrue('request' in result)
138144
http = result['request']
139145
self.assertEquals(
140146
http['query_string'],
141-
'foo=bar&custom_key1=%(m)s&custom_key2=%(m)s' % {'m': self.proc.MASK})
147+
"foo=bar&custom_key1=%(m)s&custom_key2=%(m)s&custom_key3=%(m)s" % {'m': self.proc.MASK}
148+
)
142149

143150
def test_querystring_as_string_with_partials(self):
144151
data = get_http_data()
@@ -147,11 +154,16 @@ def test_querystring_as_string_with_partials(self):
147154

148155
self.assertTrue('request' in result)
149156
http = result['request']
150-
self.assertEquals(http['query_string'], 'foo=bar&custom_key1&baz=bar' % {'m': self.proc.MASK})
157+
self.assertEquals(
158+
http['query_string'],
159+
'foo=bar&custom_key1&baz=bar' % {'m': self.proc.MASK}
160+
)
151161

152162
def test_cookie_as_string(self):
153163
data = get_http_data()
154-
data['request']['cookies'] = 'foo=bar;custom_key1=nope;custom_key2=nope;'
164+
165+
data['request']['cookies'] = \
166+
'foo=bar;custom_key1=nope;custom_key2=nope;'
155167
result = self.proc.process(data)
156168

157169
self.assertTrue('request' in result)
@@ -167,7 +179,10 @@ def test_cookie_as_string_with_partials(self):
167179

168180
self.assertTrue('request' in result)
169181
http = result['request']
170-
self.assertEquals(http['cookies'], 'foo=bar;custom_key1;baz=bar' % dict(m=self.proc.MASK))
182+
self.assertEquals(
183+
http['cookies'],
184+
'foo=bar;custom_key1;baz=bar' % dict(m=self.proc.MASK)
185+
)
171186

172187
def test_cookie_header(self):
173188
data = get_http_data()

0 commit comments

Comments
 (0)