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

Commit

Permalink
Mask Complete dictionary instead of just the leaves (#1198)
Browse files Browse the repository at this point in the history
* Mask even if value is a dictionary

* Update varmap to mask entire dict based on key

* Add test case for complete dict masking
  • Loading branch information
dhruv-aggarwal authored and ashwoods committed Feb 13, 2018
1 parent 6d05d48 commit f2aec52
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
8 changes: 4 additions & 4 deletions raven/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ def varmap(func, var, context=None, name=None):
if objid in context:
return func(name, '<...>')
context[objid] = 1
if isinstance(var, dict):
ret = dict((k, varmap(func, v, context, k))
for k, v in iteritems(var))
elif isinstance(var, (list, tuple)):
if isinstance(var, (list, tuple)):
ret = [varmap(func, f, context, name) for f in var]
else:
ret = func(name, var)
if isinstance(ret, dict):
ret = dict((k, varmap(func, v, context, k))
for k, v in iteritems(var))
del context[objid]
return ret

Expand Down
27 changes: 21 additions & 6 deletions tests/processors/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
'access_token': 'oauth2 access token',
'custom_key1': 'you should not see this',
'custom_key2': 'you should not see this',
'custom_key3': {'mask': 'This entire dict'}
}


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

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

def setUp(self):
client = Mock(sanitize_keys=['custom_key1', 'custom_key2'])
client = Mock(
sanitize_keys=['custom_key1', 'custom_key2', 'custom_key3']
)
self.proc = SanitizeKeysProcessor(client)

def _check_vars_sanitized(self, vars, MASK):
Expand All @@ -95,6 +99,8 @@ def _check_vars_sanitized(self, vars, MASK):
self.assertEquals(vars['custom_key1'], MASK)
self.assertTrue('custom_key2' in vars)
self.assertEquals(vars['custom_key2'], MASK)
self.assertTrue('custom_key3' in vars)
self.assertEquals(vars['custom_key3'], MASK)

def test_stacktrace(self, *args, **kwargs):
data = get_stack_trace_data_real()
Expand Down Expand Up @@ -131,14 +137,15 @@ def test_extra(self):

def test_querystring_as_string(self):
data = get_http_data()
data['request']['query_string'] = 'foo=bar&custom_key1=nope&custom_key2=nope'
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'
result = self.proc.process(data)

self.assertTrue('request' in result)
http = result['request']
self.assertEquals(
http['query_string'],
'foo=bar&custom_key1=%(m)s&custom_key2=%(m)s' % {'m': self.proc.MASK})
"foo=bar&custom_key1=%(m)s&custom_key2=%(m)s&custom_key3=%(m)s" % {'m': self.proc.MASK}
)

def test_querystring_as_string_with_partials(self):
data = get_http_data()
Expand All @@ -147,11 +154,16 @@ def test_querystring_as_string_with_partials(self):

self.assertTrue('request' in result)
http = result['request']
self.assertEquals(http['query_string'], 'foo=bar&custom_key1&baz=bar' % {'m': self.proc.MASK})
self.assertEquals(
http['query_string'],
'foo=bar&custom_key1&baz=bar' % {'m': self.proc.MASK}
)

def test_cookie_as_string(self):
data = get_http_data()
data['request']['cookies'] = 'foo=bar;custom_key1=nope;custom_key2=nope;'

data['request']['cookies'] = \
'foo=bar;custom_key1=nope;custom_key2=nope;'
result = self.proc.process(data)

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

self.assertTrue('request' in result)
http = result['request']
self.assertEquals(http['cookies'], 'foo=bar;custom_key1;baz=bar' % dict(m=self.proc.MASK))
self.assertEquals(
http['cookies'],
'foo=bar;custom_key1;baz=bar' % dict(m=self.proc.MASK)
)

def test_cookie_header(self):
data = get_http_data()
Expand Down

0 comments on commit f2aec52

Please sign in to comment.