diff --git a/src/tribler/core/sentry_reporter/sentry_reporter.py b/src/tribler/core/sentry_reporter/sentry_reporter.py index dd471f9df36..d17230de90e 100644 --- a/src/tribler/core/sentry_reporter/sentry_reporter.py +++ b/src/tribler/core/sentry_reporter/sentry_reporter.py @@ -14,7 +14,6 @@ from sentry_sdk.integrations.logging import LoggingIntegration, ignore_logger from sentry_sdk.integrations.threading import ThreadingIntegration - from tribler.core import version from tribler.core.sentry_reporter.sentry_tools import ( get_first_item, diff --git a/src/tribler/core/sentry_reporter/sentry_scrubber.py b/src/tribler/core/sentry_reporter/sentry_scrubber.py index 3a9e6a38702..4b846f798a4 100644 --- a/src/tribler/core/sentry_reporter/sentry_scrubber.py +++ b/src/tribler/core/sentry_reporter/sentry_scrubber.py @@ -174,17 +174,22 @@ def scrub_entity_recursively(self, entity: Union[str, Dict, List, Any], depth=10 if isinstance(entity, dict): result = {} for key, value in entity.items(): - if key in self.dict_keys_for_scrub: + if key in self.dict_keys_for_scrub and isinstance(value, str): value = value.strip() fake_value = obfuscate_string(value) placeholder = self.create_placeholder(fake_value) self.add_sensitive_pair(value, placeholder) - result[key] = self.scrub_entity_recursively(value, depth) + result[key] = placeholder + else: + result[key] = self.scrub_entity_recursively(value, depth) return result return entity def add_sensitive_pair(self, text, placeholder): + if not (text and text.strip()): # We should not replace empty substrings in the middle of other strings + return + if text in self.sensitive_occurrences: return diff --git a/src/tribler/core/sentry_reporter/sentry_tools.py b/src/tribler/core/sentry_reporter/sentry_tools.py index 8e31dc14540..f0f2281600c 100644 --- a/src/tribler/core/sentry_reporter/sentry_tools.py +++ b/src/tribler/core/sentry_reporter/sentry_tools.py @@ -118,9 +118,6 @@ def obfuscate_string(s: str, part_of_speech: str = 'noun') -> str: The same random words will be generated for the same given strings. """ - if not s: - return s - faker = Faker(locale='en_US') faker.seed_instance(s) return faker.word(part_of_speech=part_of_speech) diff --git a/src/tribler/core/sentry_reporter/tests/test_sentry_scrubber.py b/src/tribler/core/sentry_reporter/tests/test_sentry_scrubber.py index fa7ccf56488..ac79d4af41b 100644 --- a/src/tribler/core/sentry_reporter/tests/test_sentry_scrubber.py +++ b/src/tribler/core/sentry_reporter/tests/test_sentry_scrubber.py @@ -218,16 +218,16 @@ def test_scrub_event(scrubber): } assert scrubber.scrub_event(event) == { 'the very first item': '', - 'server_name': '', + 'server_name': '', CONTEXTS: { REPORTER: { 'any': { - 'USERNAME': '', + 'USERNAME': '', 'USERDOMAIN_ROAMINGPROFILE': '', 'PATH': '/users//apps', 'TMP_WIN': 'C:\\Users\\\\AppData\\Local\\Temp', - 'USERDOMAIN': '', - 'COMPUTERNAME': '', + 'USERDOMAIN': '', + 'COMPUTERNAME': '', }, STACKTRACE: [ 'Traceback (most recent call last):', @@ -306,15 +306,20 @@ def test_scrub_dict(scrubber): assert scrubber.scrub_entity_recursively(None) is None assert scrubber.scrub_entity_recursively({}) == {} - given = {'PATH': '/home/username/some/', 'USERDOMAIN': 'UD', 'USERNAME': 'U', 'REPEATED': 'user username UD U'} + assert scrubber.scrub_entity_recursively({'key': [1]}) == {'key': [1]} # non-string values should not lead to error + + given = {'PATH': '/home/username/some/', 'USERDOMAIN': 'UD', 'USERNAME': 'U', 'REPEATED': 'user username UD U', + 'key': ''} assert scrubber.scrub_entity_recursively(given) == {'PATH': '/home//some/', 'REPEATED': 'user ', 'USERDOMAIN': '', - 'USERNAME': ''} + 'USERNAME': '', + 'key': ''} - assert 'username' in scrubber.sensitive_occurrences.keys() - assert 'UD' in scrubber.sensitive_occurrences.keys() - assert 'U' in scrubber.sensitive_occurrences.keys() + assert 'username' in scrubber.sensitive_occurrences + assert 'UD' in scrubber.sensitive_occurrences + assert 'U' in scrubber.sensitive_occurrences + assert '' not in scrubber.sensitive_occurrences def test_scrub_list(scrubber):