"""Sentry's scrubbing rules and its off switch (note Sentry).""" import os import unittest from unittest import mock try: import sentry_sdk except ImportError: # pragma: no cover - the host may not have the dependency sentry_sdk = None from app import observability @unittest.skipUnless(sentry_sdk, 'sentry-sdk is installed with the API image') class ScrubTest(unittest.TestCase): def test_request_credentials_tokens_and_bodies_are_removed(self): event = {'request': { 'url': 'https://dtf.example/api/quotes?page=2&token=abc&X-Amz-Signature=def', 'query_string': 'page=2&token=abc&X-Amz-Signature=def', 'cookies': {'dtf_session': 'secret'}, 'data': '{"customer":{"mail":"maria@example.com"}}', 'headers': {'Cookie': 'dtf_session=secret', 'Authorization': 'Bearer x', 'X-Auth-Token': 'x', 'X-Amz-Security-Token': 'x', 'X-Forwarded-For': '1.2.3.4'}, }} observability.scrub(event) request = event['request'] self.assertNotIn('cookies', request) self.assertNotIn('data', request) self.assertEqual(request['headers'], {'X-Forwarded-For': '1.2.3.4'}) self.assertEqual(request['query_string'], 'page=2') self.assertEqual(request['url'], 'https://dtf.example/api/quotes?page=2') def test_personal_data_in_messages_exceptions_and_breadcrumbs(self): event = { 'message': 'Falha para maria@example.com, CPF 123.456.789-01, CNPJ 11.222.333/0001-81', 'exception': {'values': [{'value': 'e-mail ana@example.com'}]}, 'breadcrumbs': [ {'message': 'GET https://x/y?token=abc', 'data': {'url': 'https://x/y?page=1&token=abc', 'note': 'CPF 12345678901'}}, {'message': 'CNPJ 11222333000181'}, ], } observability.scrub(event) self.assertNotIn('maria@example.com', event['message']) self.assertNotIn('123.456.789-01', event['message']) self.assertNotIn('11.222.333/0001-81', event['message']) self.assertEqual(event['message'].count(observability.REDACTED), 3) self.assertNotIn('ana@example.com', event['exception']['values'][0]['value']) self.assertNotIn('token=abc', event['breadcrumbs'][0]['message']) self.assertNotIn('token=abc', event['breadcrumbs'][0]['data']['url']) self.assertNotIn('12345678901', event['breadcrumbs'][0]['data']['note']) self.assertNotIn('11222333000181', event['breadcrumbs'][1]['message']) @unittest.skipUnless(sentry_sdk, 'sentry-sdk is installed with the API image') class InitTest(unittest.TestCase): def test_no_dsn_does_nothing(self): with mock.patch.dict(os.environ, {'SENTRY_DSN_API': ''}): with mock.patch.object(sentry_sdk, 'init') as init: self.assertFalse(observability.init('api')) init.assert_not_called() def test_a_dsn_initialises_without_pii_and_scrubs(self): env = {'SENTRY_DSN_API': 'https://key@o1.ingest.sentry.io/1', 'SENTRY_ENVIRONMENT': 'test', 'IMAGE_TAG': 'abc123', 'SENTRY_TRACES': '0.2'} with mock.patch.dict(os.environ, env): with mock.patch.object(sentry_sdk, 'init') as init, \ mock.patch.object(sentry_sdk, 'set_tag') as set_tag: self.assertTrue(observability.init('api')) options = init.call_args.kwargs self.assertEqual(options['dsn'], env['SENTRY_DSN_API']) self.assertEqual(options['environment'], 'test') self.assertEqual(options['release'], 'abc123') self.assertIs(options['send_default_pii'], False) self.assertEqual(options['traces_sample_rate'], 0.2) self.assertIs(options['before_send'], observability.scrub) set_tag.assert_called_once_with('component', 'api') if __name__ == '__main__': unittest.main()