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

Simplify pattern masking and handle dicts #355

Merged
merged 2 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Handle sensitive data that is a list
  • Loading branch information
Chris7 committed Jul 30, 2019
commit 075159a46798bf78b12c1ae6305dc0b74e3944bf
11 changes: 11 additions & 0 deletions project/tests/test_sensitive_data_in_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def test_password_in_body(self):
mock_request.get = mock_request.META.get
factory = RequestModelFactory(mock_request)
body, raw_body = factory.body()

self.assertIn('testunmasked', raw_body)
self.assertNotIn('test_username', raw_body)
self.assertNotIn('testpassword', raw_body)
Expand All @@ -43,6 +44,11 @@ def test_password_in_json(self):
self.assertNotIn('test_username', body)
self.assertNotIn('testpassword', body)

for datum in [json.loads(body), json.loads(raw_body)]:
self.assertEqual(datum['username'], RequestModelFactory.CLEANSED_SUBSTITUTE)
self.assertEqual(datum['password'], RequestModelFactory.CLEANSED_SUBSTITUTE)
self.assertEqual(datum['x'], 'testunmasked')

def test_password_in_batched_json(self):
mock_request = Mock()
mock_request.META = {DJANGO_META_CONTENT_TYPE: 'application/json; charset=UTF-8'}
Expand All @@ -62,3 +68,8 @@ def test_password_in_batched_json(self):
self.assertNotIn('test_username', body[1])
self.assertNotIn('testpassword', body[1])

for data in [json.loads(body), json.loads(raw_body)]:
for datum in data:
self.assertEqual(datum['username'], RequestModelFactory.CLEANSED_SUBSTITUTE)
self.assertEqual(datum['password'], RequestModelFactory.CLEANSED_SUBSTITUTE)
self.assertEqual(datum['x'], 'testunmasked')
31 changes: 17 additions & 14 deletions silk/model_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def _parse_content_type(content_type):

class RequestModelFactory(object):
"""Produce Request models from Django request objects"""
# String to replace on masking
CLEANSED_SUBSTITUTE = '********************'

def __init__(self, request):
super(RequestModelFactory, self).__init__()
Expand Down Expand Up @@ -91,34 +93,35 @@ def _mask_credentials(self, body):
"""
Mask credentials of potentially sensitive info before saving to db.
"""
CLEANSED_SUBSTITUTE = '********************'
sensitive_keys = {'username', 'api', 'token', 'key', 'secret', 'password', 'signature'}
key_string = '|'.join(sensitive_keys)

def replace_pattern_values(obj):
pattern = re.compile(r'username|api|token|key|secret|password|signature', re.I)
for key in obj:
if pattern.search(key):
obj[key] = CLEANSED_SUBSTITUTE

if isinstance(obj, dict):
for key in obj.keys() & sensitive_keys:
obj[key] = RequestModelFactory.CLEANSED_SUBSTITUTE
elif isinstance(obj, list):
for index, item in enumerate(obj):
obj[index] = replace_pattern_values(item)
else:
pattern = re.compile(r'{}'.format(key_string), re.I)
if pattern.search(str(obj)):
return RequestModelFactory.CLEANSED_SUBSTITUTE
return obj

try:
json_body = json.loads(body)
except Exception as e:
pattern = re.compile(r'(username|api|token|key|secret|password|signature)=(.*?)(&|$)', re.M)
pattern = re.compile(r'({})=(.*?)(&|$)'.format(key_string), re.M)
try:
results = re.findall(pattern, body)
except Exception:
Logger.debug('{}'.format(str(e)))
else:
for res in results:
body = re.sub(res[1], CLEANSED_SUBSTITUTE, body)
body = re.sub(res[1], RequestModelFactory.CLEANSED_SUBSTITUTE, body)
else:
if isinstance(json_body, list):
for obj in json_body:
obj = replace_pattern_values(obj)
else:
json_body = replace_pattern_values(json_body)
body = json.dumps(json_body)
body = json.dumps(replace_pattern_values(json_body))

return body

Expand Down