From f578773f08da219ab4e9bae7bbaaa57fab62c107 Mon Sep 17 00:00:00 2001 From: Tom Brow Date: Fri, 20 Nov 2026 11:10:45 -0800 Subject: [PATCH] feat(mobile): keep the message of Dart programmer errors in reports The diagnostic filter replaced every error's own text with fixed wording, which left issue titles like "StateError: Error text removed before sending." that say nothing about the fault. Keep the text, minus URL query strings and fragments, for StateError, TypeError, AssertionError and UnsupportedError (which includes UnimplementedError). Developers write those messages ("Bad state: ref used after dispose"), so they make issues readable. Every other error type still has its text replaced. The accepted risk is a programmer- error message that interpolates a value. The SDK names a FlutterError "AssertionError" because it implements one, so the filter checks the error itself rather than the reported type name, and FlutterError text stays replaced. Co-authored-by: studio Signed-off-by: Tom Brow --- ...ostic-reporting-follows-the-destination.md | 2 +- docs/mobile-diagnostic-reporting.md | 2 +- .../shared/diagnostics/diagnostic_filter.dart | 31 ++++++++++++-- .../diagnostic_reporting_test.dart | 42 ++++++++++++++++++- 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/docs/adr/0007-diagnostic-reporting-follows-the-destination.md b/docs/adr/0007-diagnostic-reporting-follows-the-destination.md index 9ee3fd0638..60d602a037 100644 --- a/docs/adr/0007-diagnostic-reporting-follows-the-destination.md +++ b/docs/adr/0007-diagnostic-reporting-follows-the-destination.md @@ -4,7 +4,7 @@ A mobile build does [diagnostic reporting](../../CONTEXT.md#diagnostic-reporting We chose this so that the open-source app carries no organization's destination and no rule about which organizations may collect reports, the same reasoning that keeps organization hosts out of the code in [ADR 0001](0001-internal-and-public-mobile-builds.md). The first use is an organization's internal build, where everyone who has the app is a member of the organization that reads the reports. -A report may carry the stack trace and error type, app, operating system and device details, a trail of recent events written from a fixed list, the host names of the servers the app talks to, the signed-in member's public key, a random identifier the app creates for its installation, a random identifier the reporting SDK creates for the installation (which stands in as the user on reports sent while no one is signed in, and appears in the SDK's session records, which also carry when a session started, its length from start to end, how it ended and its error count), on iOS native crash and hang reports only, a hash the SDK computes from the identifier iOS gives the app's vendor, the hardware model and the app's bundle identifier (it stays the same across launches, and across a reinstall while another app from the same vendor stays installed) and, when the person is signed in to a Builderlab account, that Builderlab's origin and the account's subject identifier, never its email address. Networking measurements also carry the operation's name from a fixed list, its outcome, the kind of failure and how long it took. A report never carries message content, channel or community names, private keys or other secrets, or a URL's query string or fragment. +A report may carry the stack trace and error type, the message of a Dart programmer error (a `StateError`, `TypeError`, `AssertionError` or `UnsupportedError`, whose text developers write), app, operating system and device details, a trail of recent events written from a fixed list, the host names of the servers the app talks to, the signed-in member's public key, a random identifier the app creates for its installation, a random identifier the reporting SDK creates for the installation (which stands in as the user on reports sent while no one is signed in, and appears in the SDK's session records, which also carry when a session started, its length from start to end, how it ended and its error count), on iOS native crash and hang reports only, a hash the SDK computes from the identifier iOS gives the app's vendor, the hardware model and the app's bundle identifier (it stays the same across launches, and across a reinstall while another app from the same vendor stays installed) and, when the person is signed in to a Builderlab account, that Builderlab's origin and the account's subject identifier, never its email address. Networking measurements also carry the operation's name from a fixed list, its outcome, the kind of failure and how long it took. A report never carries message content, channel or community names, private keys or other secrets, or a URL's query string or fragment. ## Considered options diff --git a/docs/mobile-diagnostic-reporting.md b/docs/mobile-diagnostic-reporting.md index 48c2d46571..990f2a4207 100644 --- a/docs/mobile-diagnostic-reporting.md +++ b/docs/mobile-diagnostic-reporting.md @@ -48,7 +48,7 @@ A build that is given a [reporting destination](../CONTEXT.md#reporting-destinat - **No log lines in step one.** The counts carry the member and the time. The first implementation task confirms that the destination can filter counts by member; if it cannot, the fallback is one log line per outage, sent after it ends. - **Breadcrumbs come from a fixed list:** networking events, written by the app, and the app moving to the foreground or background, written by the SDK. The SDK's other automatic breadcrumbs, which could carry content or addresses, are turned off or filtered. - **Who a report is about.** The member's public key is set as the report's user when a session starts and cleared when it ends. When the session is a Builderlab account, the Builderlab's origin and the account's subject identifier are added, never its email address. Every report also carries the app's own random per-install identifier, created on first launch and kept in a file that device backups and transfers skip, so a phone restored from another gets its own, as a separate field, signed in or not, so reports from one device can be matched to the members who used it, and a member's reports to the devices they used. With no member set, for example before sign-in, the SDK fills the report's user with its own random per-install identifier, on Dart and native reports alike. On iOS, native crash and hang reports also carry the SDK's device app hash: a hash of the identifier iOS gives the app's vendor, the hardware model and the bundle identifier, stable across launches and across a reinstall while another app from the same vendor stays installed. The Dart filter drops it, so Dart reports don't carry it. The SDK also sends session records, so the destination can show a crash-free rate. A session begins each time the app starts or returns after 30 seconds or more in the background. The SDK sends a record when it begins, an update each time an error is counted, and a last record when it ends. Together they carry when the session started, how many errors it had, how it ended (normally, crashed or abnormally), the time from start to end (not time in active use), the release, the environment and the SDK's identifier. -- **One filter before anything leaves.** Errors, breadcrumbs and counts from Dart all pass through a single filter that removes URL query strings and fragments, drops any attribute not on the allowed list, replaces every error's own text with fixed wording, drops what the SDK adds about the failing widget or platform call, and keeps only the app, operating system, device and runtime details among the report's contexts, so the SDK's accessibility settings, locale and the device's own name stay on the device. An error keeps its type and stack trace. The SDK's options for sending personal data, screenshots, view hierarchies and session recordings stay off. +- **One filter before anything leaves.** Errors, breadcrumbs and counts from Dart all pass through a single filter that removes URL query strings and fragments, drops any attribute not on the allowed list, replaces every error's own text with fixed wording except for Dart's programmer-error types (`StateError`, `TypeError`, `AssertionError` and `UnsupportedError`, which includes `UnimplementedError`), whose text is kept without URL query strings because developers write those messages and they make issue titles readable, drops what the SDK adds about the failing widget or platform call, and keeps only the app, operating system, device and runtime details among the report's contexts, so the SDK's accessibility settings, locale and the device's own name stay on the device. An error keeps its type and stack trace. The accepted risk is a programmer-error message that interpolates a value. Flutter's `FlutterError` implements `AssertionError` but its text is still replaced. The SDK's options for sending personal data, screenshots, view hierarchies and session recordings stay off. - **Main-thread hangs** are captured by the SDK's own hang detection, at the SDK's default thresholds. The app's Dart code runs on the platform's main thread, so Dart work that blocks it shows up here. On iOS, the SDK reports the main thread being blocked, fully or mostly, for 2 seconds or more while the app is active, with the main thread's stack taken during the hang, including freezes the app recovers from. A hang that is still going when the app is killed, for example by the system watchdog, is sent on the next launch as a fatal hang. The SDK's separate watchdog-termination tracking also reports, on the next launch, a run that ended while the app was in the foreground with nothing else to explain it; it never reports on simulators or for kills in the background. App hang detection is off while a debugger is attached ([sentry-cocoa 9.35.0](https://github.com/getsentry/sentry-cocoa/blob/9.35.0/Sources/Swift/Integrations/AppHangTracking/SentryHangTrackingIntegration.swift)). On Android the SDK reports only freezes the system itself declared "app not responding". On Android 11 and later it reports only one that ended the process, read from the system's exit records on the next launch: the most recent unreported one, and only when the system kept a usable thread dump. Before Android 11 it reports when the main thread has been blocked for 5 seconds and the system has marked the app not responding, never with a debugger attached ([sentry-java 8.64.0](https://github.com/getsentry/sentry-java/blob/8.64.0/sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegrationFactory.java)). So with these defaults, a freeze the app recovers from is reported on iOS, and on Android only before Android 11 and only once the system has marked the app not responding. Dart frames in a hang's stack are readable only with the Dart code's debug symbols at the destination, the same as for crashes. - **Native reports skip that filter.** Native crashes and app hangs are captured and sent by the native SDKs, which never call the Dart filter. They carry stack traces and, for a native exception, its reason text. No app code puts content in a native exception today, and the SDK's native network breadcrumbs, which would carry request addresses, are off. - **Readable stack traces** need the build's debug symbols at the destination. Uploading them belongs to the release process of whoever distributes the build; this repository documents the requirement and adds nothing organization-specific. diff --git a/mobile/lib/shared/diagnostics/diagnostic_filter.dart b/mobile/lib/shared/diagnostics/diagnostic_filter.dart index 8006aa1806..62f80cae50 100644 --- a/mobile/lib/shared/diagnostics/diagnostic_filter.dart +++ b/mobile/lib/shared/diagnostics/diagnostic_filter.dart @@ -1,3 +1,4 @@ +import 'package:flutter/foundation.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; /// The one filter every Dart report, breadcrumb and count passes through @@ -5,7 +6,8 @@ import 'package:sentry_flutter/sentry_flutter.dart'; /// (native crashes, app hangs) do not pass through it. /// /// It removes URL query strings and fragments, keeps only attributes on the -/// allowed list, and drops breadcrumbs that are not from the app's fixed list. +/// allowed list, drops breadcrumbs that are not from the app's fixed list, and +/// replaces an error's text unless it is a Dart programmer error. /// See "One filter before anything leaves" in /// `docs/mobile-diagnostic-reporting.md`. abstract final class DiagnosticFilter { @@ -25,7 +27,8 @@ abstract final class DiagnosticFilter { /// version, the release, the member, and the operating system and device. static const _sdkAttributePrefixes = ['sentry.', 'user.', 'os.', 'device.']; - /// Fixed wording that replaces an error's own text. + /// Fixed wording that replaces an error's own text, except for Dart's + /// programmer-error types. static const removedText = 'Error text removed before sending.'; static const _breadcrumbCategories = {networkCategory, lifecycleCategory}; @@ -113,6 +116,21 @@ abstract final class DiagnosticFilter { 'trace', }; + /// Whether the text of the error [throwable] may leave the device. Dart's + /// programmer-error types qualify because developers write their messages, + /// for example "Bad state: ref used after dispose". The risk, accepted, is + /// a message that interpolates a value. The SDK names a [FlutterError] an + /// `AssertionError` because it implements one, so this checks the error + /// itself rather than the type name the report carries. + static bool _keepsText(Object? throwable) => switch (throwable) { + FlutterError() => false, + StateError() || + TypeError() || + AssertionError() || + UnsupportedError() => true, + _ => false, + }; + static final _urlTail = RegExp( r'''(\b[a-z][a-z0-9+.-]*://[^\s?#'"]*)[?#][^\s'"]*''', caseSensitive: false, @@ -169,8 +187,13 @@ abstract final class DiagnosticFilter { }; for (final exception in event.exceptions ?? const []) { // An error's text can quote its input, a server's response body or a - // URL, so only its type, mechanism type and stack trace are kept. - if (exception.value != null) exception.value = removedText; + // URL, so only its type, mechanism type and stack trace are kept, and + // its text only for the types in [_keepsText]. + if (exception.value case final value?) { + exception.value = _keepsText(exception.throwable) + ? stripUrlTails(value) + : removedText; + } exception.mechanism?.data = null; } event.breadcrumbs = [ diff --git a/mobile/test/shared/diagnostics/diagnostic_reporting_test.dart b/mobile/test/shared/diagnostics/diagnostic_reporting_test.dart index 5e54121a01..3a96fbd988 100644 --- a/mobile/test/shared/diagnostics/diagnostic_reporting_test.dart +++ b/mobile/test/shared/diagnostics/diagnostic_reporting_test.dart @@ -16,6 +16,8 @@ import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:nostr/nostr.dart' as nostr; // ignore: depend_on_referenced_packages, implementation_imports import 'package:sentry/src/deduplication/deduplication_event_processor.dart'; +// ignore: depend_on_referenced_packages, implementation_imports +import 'package:sentry/src/exception/dart_exception_type_identifier.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; // The SDK's own processors and integrations, so tests see what it really // sends rather than hand-built events. @@ -169,10 +171,12 @@ void main() { transport = _MemoryTransport(); options = SentryFlutterOptions(); configureDiagnostics(options, 'https://key@example.org/1'); - // The processors Sentry.init and SentryFlutter.init install. + // The processors and type identifier Sentry.init and SentryFlutter.init + // install. options ..transport = transport ..sendClientReports = false + ..prependExceptionTypeIdentifier(DartExceptionTypeIdentifier()) ..addEventProcessor(DeduplicationEventProcessor(options)) ..addEventProcessor(FlutterExceptionEventProcessor()) ..addEventProcessor(FlutterEnricherEventProcessor(options)) @@ -186,7 +190,12 @@ void main() { final rows = <(String, Object, String)>[ ('a parse failure', const FormatException('private-marker'), 'Format'), ('a bad argument', ArgumentError.value('private-marker'), 'Argument'), - ('a server response', StateError('body: private-marker'), 'StateError'), + ( + 'a Flutter framework error', + FlutterError('private-marker'), + 'AssertionError', + ), + ('an ordinary exception', Exception('private-marker'), 'Exception'), ( 'a platform failure', PlatformException( @@ -209,6 +218,26 @@ void main() { }); } + const link = 'open https://e.org/f?token=private-marker'; + final kept = <(String, Object, String, String)>[ + ('a bad state', StateError(link), 'StateError', 'Bad state: open'), + ('a failed assertion', AssertionError(link), 'AssertionError', 'open'), + ('an unsupported call', UnsupportedError(link), 'Unsupported', 'open'), + ('an unimplemented call', UnimplementedError(link), 'Unimpl', 'open'), + ('a failed cast', _failedCast(), 'TypeError', 'is not a subtype of'), + ]; + for (final (name, error, type, text) in kept) { + test('keeps the text of $name, without a URL query', () async { + await hub.captureException(error, stackTrace: StackTrace.current); + final sent = transport.events.single; + + final exception = (sent['exception']['values'] as List).last as Map; + expect(exception['type'], contains(type)); + expect(exception['value'], contains(text)); + expect(jsonEncode(sent), isNot(contains('private-marker'))); + }); + } + test('a report right after a member change names the new member', () async { SentryDiagnosticReporter(hub: hub) ..setMember('a' * 64) @@ -480,6 +509,15 @@ class _ReleaseChecker extends RuntimeChecker { bool isProfileMode() => false; } +Object _failedCast() { + try { + // ignore: unnecessary_cast + return [1] as List; + } on TypeError catch (error) { + return error; + } +} + class _MemoryTransport implements Transport { final events = >[]; final _first = Completer();