Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
when a reference to a nested dict goes out of scope
"""
data = {
'level1-key1': {
'level2-key1': FilterDict({
'level3-key1': {'level4-key1': 'level4-value1'},
'level3-key4': {'level4-key3': 'level4-value3'},
}),
'level2-key2': FilterDict({
'level3-key2': 'level3-value1',
'level3-key3': {'level4-key2': 'level4-value2'},
'level3-key5': {'level4-key4': 'level4-value4'},
}),
}
}
encoder = SanitizingJSONEncoder(keyword_filters=['password'])
sane_data = json.loads(encoder.encode(data))
self.assertEqual(sane_data, {
'level1-key1': {
'level2-key1': {
'level3-key1': {
'level4-key1': 'level4-value1'
},
'level3-key4': {
'level4-key3': 'level4-value3'
}
},
'level2-key2': {
'level3-key2': 'level3-value1',
'level3-key3': {
'level4-key2': 'level4-value2'
},
def test_encoding_recursive_repeated(self):
"""
Test that encoding the same object twice produces the same result
"""
data = {"Test": ["a", "b", "c"]}
data["Self"] = data
encoder = SanitizingJSONEncoder(keyword_filters=[])
sane_data = json.loads(encoder.encode(data))
self.assertEqual(sane_data,
{"Test": ["a", "b", "c"], "Self": "[RECURSIVE]"})
sane_data = json.loads(encoder.encode(data))
self.assertEqual(sane_data,
{"Test": ["a", "b", "c"], "Self": "[RECURSIVE]"})
def test_sanitize_bad_utf8_object(self):
data = {"bad_utf8": u("test \xe9")}
encoder = SanitizingJSONEncoder(keyword_filters=[])
sane_data = json.loads(encoder.encode(data))
self.assertEqual(sane_data, data)
def test_sanitize_valid_unicode_object(self):
data = {"item": u('\U0001f62c')}
encoder = SanitizingJSONEncoder(keyword_filters=[])
sane_data = json.loads(encoder.encode(data))
self.assertEqual(sane_data, data)
def test_unfiltered_encode(self):
data = {"metadata": {"another_password": "My password"}}
encoder = SanitizingJSONEncoder(keyword_filters=["password"])
sane_data = json.loads(encoder.encode(data))
self.assertEqual(sane_data, data)
'level3-key1': {'level4-key1': 'level4-value1'},
'token': 'mypassword',
},
'level2-key2': {
'level3-key3': {'level4-key2': 'level4-value2'},
'level3-key4': {'level4-key3': 'level4-value3'},
'level3-key5': {'password': 'super-secret'},
'level3-key6': {'level4-key4': 'level4-value4'},
'level3-key7': {'level4-key4': 'level4-value4'},
'level3-key8': {'level4-key4': 'level4-value4'},
'level3-key9': {'level4-key4': 'level4-value4'},
'level3-key0': {'level4-key4': 'level4-value4'},
},
}
})
encoder = SanitizingJSONEncoder(keyword_filters=['password', 'token'])
filtered_data = encoder.filter_string_values(data)
self.assertEqual(filtered_data, {
'level1-key1': {
'level2-key1': {
'level3-key1': {
'level4-key1': 'level4-value1'
},
'token': '[FILTERED]'
},
'level2-key2': {
'level3-key3': {
'level4-key2': 'level4-value2'
},
'level3-key4': {
'level4-key3': 'level4-value3'
},
def test_filter_string_values_list_handling(self):
"""
Test that filter_string_values can accept a list for the ignored
parameter for backwards compatibility
"""
data = {}
encoder = SanitizingJSONEncoder()
# no assert as we are just expecting this not to throw
encoder.filter_string_values(data, ['password'])
'version': notifier_version
},
'device': FilterDict({
'hostname': self.config.get('hostname'),
'runtimeVersions': self.config.get('runtime_versions')
}),
'app': {
'releaseStage': self.config.get('release_stage'),
'version': self.config.get('app_version')
},
'sessionCounts': sessions
}
try:
filters = self.config.params_filters
encoder = SanitizingJSONEncoder(separators=(',', ':'),
keyword_filters=filters)
encoded_payload = encoder.encode(payload)
self.config.delivery.deliver_sessions(self.config, encoded_payload)
except Exception as e:
bugsnag.logger.exception('Sending sessions failed %s', e)
def encode(self, obj):
safe_obj = self._sanitize(obj, False)
payload = super(SanitizingJSONEncoder, self).encode(safe_obj)
if len(payload) > MAX_PAYLOAD_LENGTH:
safe_obj = self._sanitize(safe_obj, True)
return super(SanitizingJSONEncoder, self).encode(safe_obj)
else:
return payload