AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): stop the Sentry scrubber silently dropping error events (#14880)
Bentlye507a4d62167
diff --git a/autogpt_platform/backend/backend/util/metrics.py b/autogpt_platform/backend/backend/util/metrics.pyindex b16a54d1c3..c36304a7ac 100644--- a/autogpt_platform/backend/backend/util/metrics.py+++ b/autogpt_platform/backend/backend/util/metrics.py@@ -10,7 +10,7 @@ from sentry_sdk.api import capture_exception as _sentry_capture_exceptionfrom sentry_sdk.api import flush as _sentry_flushfrom sentry_sdk.integrations.asyncio import AsyncioIntegrationfrom sentry_sdk.integrations.logging import LoggingIntegration-from sentry_sdk.scrubber import DEFAULT_DENYLIST, EventScrubber+from sentry_sdk.scrubber import DEFAULT_DENYLIST, DEFAULT_PII_DENYLISTtry:from sentry_sdk.integrations.anthropic import AnthropicIntegration@@ -87,9 +87,10 @@ _SENTRY_SENSITIVE_FIELDS = sorted("verification_url",})-_SENTRY_EVENT_SCRUBBER = EventScrubber(- denylist=_SENTRY_SENSITIVE_FIELDS,- recursive=True,+# Matched case-insensitively against every key in the event, as sentry's+# EventScrubber does (with its PII list, since send_default_pii is off).+_SENTRY_SENSITIVE_KEYS = frozenset(+ field.lower() for field in (*_SENTRY_SENSITIVE_FIELDS, *DEFAULT_PII_DENYLIST))_EMBEDDED_SECRET_ASSIGNMENT = re.compile(r"(?i)(?:access[_-]?token|refresh[_-]?token|id[_-]?token|api[_-]?key|"@@ -106,14 +107,17 @@ _TOKEN_SHAPED_VALUE = re.compile(_FILTERED_VALUE = "[Filtered]"-def _scrub_embedded_secret_values(value: object) -> object:+def _scrub_secrets(value: object) -> object:if isinstance(value, dict):for key, nested in value.items():- value[key] = _scrub_embedded_secret_values(nested)+ if isinstance(key, str) and key.lower() in _SENTRY_SENSITIVE_KEYS:+ value[key] = _FILTERED_VALUE+ else:+ value[key] = _scrub_secrets(nested)return valueif isinstance(value, list):for index, nested in enumerate(value):- value[index] = _scrub_embedded_secret_values(nested)+ value[index] = _scrub_secrets(nested)return valueif isinstance(value, str) and (_EMBEDDED_SECRET_ASSIGNMENT.search(value) or _TOKEN_SHAPED_VALUE.search(value)@@ -123,8 +127,17 @@ def _scrub_embedded_secret_values(value: object) -> object:def _scrub_sentry_event(event: dict) -> None:- _SENTRY_EVENT_SCRUBBER.scrub_dict(event)- _scrub_embedded_secret_values(event)+ """Scrub sensitive keys and secret-shaped values from an outgoing event.++ ``before_send`` sees the event after the SDK has serialized it, and the+ transport then ``json.dumps`` it as is. So every replacement must be plain+ JSON: sentry's EventScrubber writes ``AnnotatedValue`` objects, and one of+ those here made the SDK drop the whole event as an internal error. ``_meta``+ is the SDK's own annotation tree and is left alone.+ """+ for key, value in event.items():+ if key != "_meta":+ event[key] = _scrub_secrets(value)# FalkorDB (Graphiti CoPilot memory) connection-teardown noise. graphiti-core'sdiff --git a/autogpt_platform/backend/backend/util/metrics_test.py b/autogpt_platform/backend/backend/util/metrics_test.pyindex b2001f5a81..eeb13071b5 100644--- a/autogpt_platform/backend/backend/util/metrics_test.py+++ b/autogpt_platform/backend/backend/util/metrics_test.py@@ -14,7 +14,8 @@ import sysimport sentry_sdkfrom sentry_sdk.consts import DEFAULT_OPTIONS-from sentry_sdk.utils import event_from_exception+from sentry_sdk.serializer import serialize+from sentry_sdk.utils import event_from_exception, json_dumps# Imported at module scope on purpose: AppProcess calls sentry_init() in its# class body, so the guard has to hold at collection time, not just in a test.@@ -206,6 +207,44 @@ def test_before_send_scrubs_secrets_from_actual_exception_event() -> None:assert "safe-context-value" in serialized+def test_before_send_output_survives_the_sdk_transport() -> None:+ """The SDK runs ``before_send`` on the already-serialized event and then+ ``json_dumps`` it for the envelope. A scrubbed event that is not plain JSON+ is dropped there as an internal SDK error, so it never reaches Sentry.++ A frame local named ``session`` (as in ``download_with_fresh_session``) and+ an ``authorization`` request header are enough to trigger that."""++ def download_with_fresh_session():+ session = "aiohttp-client-session"+ if session:+ raise RuntimeError("Response payload is not completed")++ try:+ download_with_fresh_session()+ except RuntimeError:+ event, hint = event_from_exception(+ sys.exc_info(), client_options=DEFAULT_OPTIONS+ )+ event["request"] = {+ "method": "GET",+ "url": "http://backend/api/workspace/files/f/download",+ "headers": {"authorization": "Bearer FAKE-TOKEN-1", "accept": "*/*"},+ }++ scrubbed = _before_send(serialize(event), hint)++ assert scrubbed is not None+ body = json.loads(json_dumps(scrubbed))+ raising_frame = body["exception"]["values"][0]["stacktrace"]["frames"][-1]+ assert raising_frame["vars"]["session"] == "[Filtered]"+ assert body["request"]["headers"]["authorization"] == "[Filtered]"+ assert body["request"]["headers"]["accept"] == "*/*"+ assert body["exception"]["values"][0]["value"] == (+ "Response payload is not completed"+ )++def test_before_send_keeps_untyped_balance_message() -> None:try:raise RuntimeError("Third-party API reported insufficient balance")