From 43e267296dd146d561e6fdde4169ed391093eb95 Mon Sep 17 00:00:00 2001 From: Tom Brow Date: Fri, 20 Nov 2026 12:28:20 -0800 Subject: [PATCH] fix(mobile): replace kept error text that has a URL or a long line Where a URL ends in free text is ambiguous, so stripping its query string could miss part of it. A kept error now sends only its first line, and the fixed wording when that line contains a URL or is over 200 characters. 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 | 26 ++++++++-- .../diagnostic_reporting_test.dart | 52 ++++++++++++++++--- 4 files changed, 69 insertions(+), 13 deletions(-) diff --git a/docs/adr/0007-diagnostic-reporting-follows-the-destination.md b/docs/adr/0007-diagnostic-reporting-follows-the-destination.md index 60d602a037..3aaac09ae3 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, 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. +A report may carry the stack trace and error type, the first line of a Dart programmer error's message (a `StateError`, `TypeError`, `AssertionError` or `UnsupportedError`, whose text developers write) when it has no URL, 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 990f2a4207..2b7d90bf79 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 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. +- **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 first line is kept because developers write those messages and they make issue titles readable (a first line that contains a URL or is over 200 characters is replaced too), 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 62f80cae50..c213683aed 100644 --- a/mobile/lib/shared/diagnostics/diagnostic_filter.dart +++ b/mobile/lib/shared/diagnostics/diagnostic_filter.dart @@ -118,10 +118,11 @@ abstract final class DiagnosticFilter { /// 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. + /// for example "Bad state: Using "ref" when a widget is about to or has been + /// unmounted is unsafe." 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() || @@ -131,6 +132,21 @@ abstract final class DiagnosticFilter { _ => false, }; + /// The longest first line of a kept error's text that may leave the device. + static const _keptTextLimit = 200; + + /// The first line of a kept error's [text], or [removedText] when that line + /// contains a URL or is long. Where a URL ends in free text is ambiguous, so + /// none is kept. A long line, or the lines after the first, usually quote + /// other text, such as a response body or a wrapped error. + static String _keptText(String text) { + final newline = text.indexOf('\n'); + final line = newline < 0 ? text : text.substring(0, newline); + return line.length > _keptTextLimit || line.contains('://') + ? removedText + : line; + } + static final _urlTail = RegExp( r'''(\b[a-z][a-z0-9+.-]*://[^\s?#'"]*)[?#][^\s'"]*''', caseSensitive: false, @@ -191,7 +207,7 @@ abstract final class DiagnosticFilter { // its text only for the types in [_keepsText]. if (exception.value case final value?) { exception.value = _keepsText(exception.throwable) - ? stripUrlTails(value) + ? _keptText(value) : removedText; } exception.mechanism?.data = null; diff --git a/mobile/test/shared/diagnostics/diagnostic_reporting_test.dart b/mobile/test/shared/diagnostics/diagnostic_reporting_test.dart index 3a96fbd988..7f84162c97 100644 --- a/mobile/test/shared/diagnostics/diagnostic_reporting_test.dart +++ b/mobile/test/shared/diagnostics/diagnostic_reporting_test.dart @@ -218,22 +218,62 @@ 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 bad state', + StateError('ref used after dispose\nprivate-marker'), + 'StateError', + 'Bad state: ref used after dispose', + ), + ( + 'a failed assertion', + AssertionError('list is sorted'), + 'AssertionError', + 'list is sorted', + ), + ( + 'an unsupported call', + UnsupportedError('no resize'), + 'Unsupported', + 'no resize', + ), + ( + 'an unimplemented call', + UnimplementedError('no export'), + 'Unimpl', + 'no export', + ), ('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 { + test('keeps the first line of the text of $name', () 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(exception['value'], isNot(contains('\n'))); + expect(jsonEncode(sent), isNot(contains('private-marker'))); + }); + } + + final replaced = <(String, String)>[ + ('a URL', 'open https://e.org/f?token=private-marker'), + ('a URL with a quote', "open https://e.org/O'Brien?private-marker#x"), + ('a URL inside a word', 'open id_https://e.org/f?private-marker'), + ('a long line', 'state ${'x' * 200} private-marker'), + ]; + for (final (name, text) in replaced) { + test('replaces kept text that has $name', () async { + await hub.captureException( + StateError(text), + stackTrace: StackTrace.current, + ); + final sent = transport.events.single; + + final exception = (sent['exception']['values'] as List).last as Map; + expect(exception['value'], DiagnosticFilter.removedText); expect(jsonEncode(sent), isNot(contains('private-marker'))); }); }