From 62dfadecdface687841930de70f12d151961f47e Mon Sep 17 00:00:00 2001 From: Nino van Hooff Date: Mon, 24 Feb 2020 15:27:17 +0100 Subject: [PATCH 1/4] Prune all sensitive data before sending it to Sentry CURA-7245 --- cura/CrashHandler.py | 16 ++++++++++++++++ cura_app.py | 4 +++- plugins/SentryLogger/SentryLogger.py | 13 ++++--------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/cura/CrashHandler.py b/cura/CrashHandler.py index e72180887c..3cfbab2551 100644 --- a/cura/CrashHandler.py +++ b/cura/CrashHandler.py @@ -32,6 +32,8 @@ from UM.Resources import Resources from cura import ApplicationMetadata catalog = i18nCatalog("cura") +home_dir = os.path.expanduser("~") + MYPY = False if MYPY: @@ -83,6 +85,20 @@ class CrashHandler: self.dialog = QDialog() self._createDialog() + @staticmethod + def pruneSensitiveData(obj): + if type(obj) is list: + return [CrashHandler.pruneSensitiveData(item) for item in obj] + if type(obj) is dict: + return {k: CrashHandler.pruneSensitiveData(v) for k, v in obj.items()} + if type(obj) is str: + return obj.replace(home_dir, "") + return obj + + @staticmethod + def sentry_before_send(event, hint): + return CrashHandler.pruneSensitiveData(event) + def _createEarlyCrashDialog(self): dialog = QDialog() dialog.setMinimumWidth(500) diff --git a/cura_app.py b/cura_app.py index a8fe708c5f..fba136516c 100755 --- a/cura_app.py +++ b/cura_app.py @@ -11,6 +11,7 @@ import sys from UM.Platform import Platform from cura import ApplicationMetadata from cura.ApplicationMetadata import CuraAppName +from cura.CrashHandler import CrashHandler try: import sentry_sdk @@ -42,8 +43,9 @@ if with_sentry_sdk: sentry_env = "nightly" except IndexError: pass - + sentry_sdk.init("https://5034bf0054fb4b889f82896326e79b13@sentry.io/1821564", + before_send = CrashHandler.sentry_before_send, environment = sentry_env, release = "cura%s" % ApplicationMetadata.CuraVersion, default_integrations = False, diff --git a/plugins/SentryLogger/SentryLogger.py b/plugins/SentryLogger/SentryLogger.py index 31ab38b6e2..51e77ad589 100644 --- a/plugins/SentryLogger/SentryLogger.py +++ b/plugins/SentryLogger/SentryLogger.py @@ -3,6 +3,9 @@ from UM.Logger import LogOutput from typing import Set + +from cura.CrashHandler import CrashHandler + try: from sentry_sdk import add_breadcrumb except ImportError: @@ -10,8 +13,6 @@ except ImportError: from typing import Optional import os -home_dir = os.path.expanduser("~") - class SentryLogger(LogOutput): # Sentry (https://sentry.io) is the service that Cura uses for logging crashes. This logger ensures that the @@ -37,7 +38,7 @@ class SentryLogger(LogOutput): # \param message String containing message to be logged def log(self, log_type: str, message: str) -> None: level = self._translateLogType(log_type) - message = self._pruneSensitiveData(message) + message = CrashHandler.pruneSensitiveData(message) if level is None: if message not in self._show_once: level = self._translateLogType(log_type[0]) @@ -47,12 +48,6 @@ class SentryLogger(LogOutput): else: add_breadcrumb(level = level, message = message) - @staticmethod - def _pruneSensitiveData(message): - if home_dir in message: - message = message.replace(home_dir, "") - return message - @staticmethod def _translateLogType(log_type: str) -> Optional[str]: return SentryLogger._levels.get(log_type) From 2cd6149ef0d821fa24fa676b755b9ceaf2198995 Mon Sep 17 00:00:00 2001 From: Nino van Hooff Date: Mon, 24 Feb 2020 16:14:12 +0100 Subject: [PATCH 2/4] Update cura/CrashHandler.py Add typing to pruneSensitiveData Co-Authored-By: Jaime van Kessel --- cura/CrashHandler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cura/CrashHandler.py b/cura/CrashHandler.py index 3cfbab2551..82c083ef2c 100644 --- a/cura/CrashHandler.py +++ b/cura/CrashHandler.py @@ -86,7 +86,7 @@ class CrashHandler: self._createDialog() @staticmethod - def pruneSensitiveData(obj): + def pruneSensitiveData(obj: Any) -> Any: if type(obj) is list: return [CrashHandler.pruneSensitiveData(item) for item in obj] if type(obj) is dict: From 94e9753b6cc8d44826ae8a1cf9d023fa9d84602b Mon Sep 17 00:00:00 2001 From: Nino van Hooff Date: Mon, 24 Feb 2020 16:26:32 +0100 Subject: [PATCH 3/4] Update cura/CrashHandler.py Import Any and re-order if-statements for efficiency CURA-7245 --- cura/CrashHandler.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cura/CrashHandler.py b/cura/CrashHandler.py index 82c083ef2c..4ac2e190ff 100644 --- a/cura/CrashHandler.py +++ b/cura/CrashHandler.py @@ -10,7 +10,7 @@ import os.path import uuid import json import locale -from typing import cast +from typing import cast, Any try: from sentry_sdk.hub import Hub @@ -87,12 +87,13 @@ class CrashHandler: @staticmethod def pruneSensitiveData(obj: Any) -> Any: - if type(obj) is list: - return [CrashHandler.pruneSensitiveData(item) for item in obj] - if type(obj) is dict: - return {k: CrashHandler.pruneSensitiveData(v) for k, v in obj.items()} - if type(obj) is str: + if isinstance(obj, str): return obj.replace(home_dir, "") + if isinstance(obj, list): + return [CrashHandler.pruneSensitiveData(item) for item in obj] + if isinstance(obj, dict): + return {k: CrashHandler.pruneSensitiveData(v) for k, v in obj.items()} + return obj @staticmethod From 9c0e6f9338b05beb6556b6b2ad41bab449775fbf Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Mon, 24 Feb 2020 16:57:37 +0100 Subject: [PATCH 4/4] Apply suggestions from code review Codestyle! --- cura/CrashHandler.py | 2 +- cura_app.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cura/CrashHandler.py b/cura/CrashHandler.py index 4ac2e190ff..6acd15bbf8 100644 --- a/cura/CrashHandler.py +++ b/cura/CrashHandler.py @@ -97,7 +97,7 @@ class CrashHandler: return obj @staticmethod - def sentry_before_send(event, hint): + def sentryBeforeSend(event, hint): return CrashHandler.pruneSensitiveData(event) def _createEarlyCrashDialog(self): diff --git a/cura_app.py b/cura_app.py index fba136516c..422313131b 100755 --- a/cura_app.py +++ b/cura_app.py @@ -45,7 +45,7 @@ if with_sentry_sdk: pass sentry_sdk.init("https://5034bf0054fb4b889f82896326e79b13@sentry.io/1821564", - before_send = CrashHandler.sentry_before_send, + before_send = CrashHandler.sentryBeforeSend, environment = sentry_env, release = "cura%s" % ApplicationMetadata.CuraVersion, default_integrations = False,