From e0c39747b56533cf87695ebfa22cfd33846eaf6e Mon Sep 17 00:00:00 2001 From: Tom Brow Date: Fri, 20 Nov 2026 10:19:14 -0800 Subject: [PATCH] feat(mobile): report push gateway replies the app cannot read The push gateway is called from native code, which already rejects a reply that does not match the gateway's protocol. Give that case its own error code, with no text from the reply, and report it from Dart as a push gateway response error. Routine failures (offline, refused, expired, server errors) keep their existing code and are not reported. Reports are attributed through the signing member, so an enrollment that finishes after a community switch names no one else. Co-authored-by: studio Signed-off-by: Tom Brow --- docs/mobile-diagnostic-reporting.md | 2 +- .../BuzzDevPushEnrollmentDriver.swift | 14 +++-- mobile/ios/Runner/AppDelegate.swift | 12 ++++ mobile/lib/shared/push/push_bootstrap.dart | 7 +++ mobile/lib/shared/push/push_bridge.dart | 29 +++++++-- mobile/test/shared/push/push_bridge_test.dart | 59 +++++++++++++++++++ 6 files changed, 113 insertions(+), 10 deletions(-) diff --git a/docs/mobile-diagnostic-reporting.md b/docs/mobile-diagnostic-reporting.md index 48c2d46571..48002d6699 100644 --- a/docs/mobile-diagnostic-reporting.md +++ b/docs/mobile-diagnostic-reporting.md @@ -1,6 +1,6 @@ # Mobile diagnostic reporting -Status: step one in progress. Implemented: the reporting module and explicit error reports (for push, the relay's capability descriptor; push gateway replies are not reported yet). Still to come: networking counts. +Status: step one in progress. Implemented: the reporting module and explicit error reports (for push, the relay's capability descriptor and the push gateway's replies). Still to come: networking counts. Terminology is defined in [CONTEXT.md](../CONTEXT.md#mobile-builds). The decisions behind this spec are in [ADR 0007](adr/0007-diagnostic-reporting-follows-the-destination.md) and [ADR 0008](adr/0008-sentry-for-mobile-diagnostic-reporting.md). diff --git a/mobile/ios/BuzzPushKit/Sources/BuzzPushKit/BuzzDevPushEnrollmentDriver.swift b/mobile/ios/BuzzPushKit/Sources/BuzzPushKit/BuzzDevPushEnrollmentDriver.swift index fd6cdb466d..78c8d23235 100644 --- a/mobile/ios/BuzzPushKit/Sources/BuzzPushKit/BuzzDevPushEnrollmentDriver.swift +++ b/mobile/ios/BuzzPushKit/Sources/BuzzPushKit/BuzzDevPushEnrollmentDriver.swift @@ -95,6 +95,10 @@ public enum BuzzDevPushEnrollmentError: Error, LocalizedError, Equatable { case invalidRelayURL case invalidRelayDescriptor case invalidResponse(route: String) + /// A gateway reply whose body does not decode, or whose fields fail + /// validation. Unlike `invalidResponse`, which also covers unreadable local + /// enrollment state, this one means the gateway's contract drifted. + case invalidGatewayResponse(route: String) case unexpectedStatus(route: String, expected: Int, actual: Int, body: String) case randomGenerationFailed(Int32) case appAttestUnsupported @@ -109,7 +113,7 @@ public enum BuzzDevPushEnrollmentError: Error, LocalizedError, Equatable { return "The relay URL must be a ws or wss origin." case .invalidRelayDescriptor: return "NIP-11 must contain exactly one valid current push key." - case .invalidResponse(let route): + case .invalidResponse(let route), .invalidGatewayResponse(let route): return "The response from \(route) did not match the closed push protocol." case .unexpectedStatus(let route, let expected, let actual, let body): return "The response from \(route) was HTTP \(actual), expected \(expected): \(body)" @@ -866,7 +870,7 @@ public final class BuzzDevPushEnrollmentDriver { Self.isBase64URLChallenge(response.challenge), response.expiresAt > Int64(now().timeIntervalSince1970) else { - throw BuzzDevPushEnrollmentError.invalidResponse(route: "v1/installations/challenges") + throw BuzzDevPushEnrollmentError.invalidGatewayResponse(route: "v1/installations/challenges") } return Challenge(id: id, value: response.challenge) } @@ -896,7 +900,7 @@ public final class BuzzDevPushEnrollmentDriver { response.endpointEpoch == Self.endpointEpoch, response.expiresAt == expiresAt else { - throw BuzzDevPushEnrollmentError.invalidResponse(route: "v1/installations") + throw BuzzDevPushEnrollmentError.invalidGatewayResponse(route: "v1/installations") } return installation } @@ -927,7 +931,7 @@ public final class BuzzDevPushEnrollmentDriver { ) ) guard !response.endpointGrant.isEmpty, response.endpointGrant.utf8.count <= 4_096 else { - throw BuzzDevPushEnrollmentError.invalidResponse(route: "v1/delegations") + throw BuzzDevPushEnrollmentError.invalidGatewayResponse(route: "v1/delegations") } return response.endpointGrant } @@ -976,7 +980,7 @@ public final class BuzzDevPushEnrollmentDriver { do { return try JSONDecoder().decode(Response.self, from: data) } catch { - throw BuzzDevPushEnrollmentError.invalidResponse(route: route) + throw BuzzDevPushEnrollmentError.invalidGatewayResponse(route: route) } } diff --git a/mobile/ios/Runner/AppDelegate.swift b/mobile/ios/Runner/AppDelegate.swift index 093220d205..24ae81e2e3 100644 --- a/mobile/ios/Runner/AppDelegate.swift +++ b/mobile/ios/Runner/AppDelegate.swift @@ -861,6 +861,18 @@ import os.log relayURL: relayURL ) await MainActor.run { result(record.flutterArguments) } + } catch BuzzDevPushEnrollmentError.invalidGatewayResponse { + // Its own code, with no text from the reply, so Dart can report + // contract drift without parsing messages. + await MainActor.run { + result( + FlutterError( + code: "gateway_response_invalid", + message: "The push gateway sent a response the app cannot read.", + details: nil + ) + ) + } } catch { await MainActor.run { result( diff --git a/mobile/lib/shared/push/push_bootstrap.dart b/mobile/lib/shared/push/push_bootstrap.dart index a461a34b56..b6e87f5cfe 100644 --- a/mobile/lib/shared/push/push_bootstrap.dart +++ b/mobile/lib/shared/push/push_bootstrap.dart @@ -7,6 +7,7 @@ import 'package:hooks_riverpod/hooks_riverpod.dart'; import '../community/community.dart'; import '../community/community_provider.dart'; +import '../diagnostics/diagnostic_reporter.dart'; import '../relay/relay_provider.dart'; import '../relay/relay_session.dart'; import '../relay/signed_event_relay.dart'; @@ -559,6 +560,12 @@ class _BuzzPushLifecycle extends HookConsumerWidget { Env.pushGatewayUrl, communitiesForSnapshotRefresh: ref.read(communityListProvider).value ?? [community], + // Enrollment can finish after a community switch, when reports already + // name the next member. + diagnostics: SignerDiagnosticReporter( + ref.read(diagnosticReporterProvider), + signer: memberPubkey, + ), ); // Relay lease replacement and gateway delegation are independent state // machines. Subscription changes advance only the kind-30350 generation; diff --git a/mobile/lib/shared/push/push_bridge.dart b/mobile/lib/shared/push/push_bridge.dart index 078aeee37b..4951550226 100644 --- a/mobile/lib/shared/push/push_bridge.dart +++ b/mobile/lib/shared/push/push_bridge.dart @@ -6,11 +6,16 @@ import 'package:flutter/services.dart'; import '../community/community.dart'; import '../deeplink/deep_link.dart'; +import '../diagnostics/diagnostic_reporter.dart'; import '../relay/relay_provider.dart'; import 'push_snapshot.dart'; const _channel = MethodChannel('buzz/push'); +/// The code native enrollment fails with when the push gateway's reply does +/// not match its protocol. +const _gatewayResponseInvalid = 'gateway_response_invalid'; + /// Latest APNs registration state, including callbacks replayed by iOS after /// the Flutter method channel attaches. final apnsDeviceToken = ValueNotifier(null); @@ -307,11 +312,27 @@ Future enrollBuzzPush( String relayUrl, String gatewayUrl, { List? communitiesForSnapshotRefresh, + DiagnosticReporter diagnostics = const NoopDiagnosticReporter(), }) async { - final raw = await _channel.invokeMapMethod('enrollPush', { - 'relayUrl': relayUrl, - 'gatewayUrl': gatewayUrl, - }); + final Map? raw; + try { + raw = await _channel.invokeMapMethod('enrollPush', { + 'relayUrl': relayUrl, + 'gatewayUrl': gatewayUrl, + }); + } on PlatformException catch (error, stackTrace) { + // The gateway is called from native code. It has its own code for a reply + // the app cannot read; every other failure (offline, refused, expired, + // server errors) is routine and not reported. + if (error.code == _gatewayResponseInvalid) { + diagnostics.reportError( + error, + stackTrace, + operation: DiagnosticOperation.pushGatewayResponse, + ); + } + rethrow; + } if (raw == null) { throw StateError('Native push enrollment returned no grant.'); } diff --git a/mobile/test/shared/push/push_bridge_test.dart b/mobile/test/shared/push/push_bridge_test.dart index d59e8cbcac..efde78fdeb 100644 --- a/mobile/test/shared/push/push_bridge_test.dart +++ b/mobile/test/shared/push/push_bridge_test.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:buzz/shared/community/community.dart'; import 'package:buzz/shared/deeplink/deep_link.dart'; +import 'package:buzz/shared/diagnostics/diagnostic_reporter.dart'; import 'package:buzz/shared/hosted/hosted_environment.dart'; import 'package:buzz/shared/hosted/hosted_relay_auth.dart'; import 'package:buzz/shared/push/push_bridge.dart'; @@ -179,6 +180,53 @@ void main() { }, ); + test('a gateway reply the app cannot read is reported and still fails ' + 'enrollment', () async { + debugDefaultTargetPlatformOverride = TargetPlatform.iOS; + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger + .setMockMethodCallHandler(_channel, (call) async { + throw PlatformException(code: 'gateway_response_invalid'); + }); + final reporter = _RecordingDiagnostics(); + + await expectLater( + enrollBuzzPush( + 'wss://relay.example/', + 'https://gateway.example/', + diagnostics: reporter, + ), + throwsA(isA()), + ); + + expect(reporter.operations, [DiagnosticOperation.pushGatewayResponse]); + }); + + test('routine enrollment failures are not reported', () async { + debugDefaultTargetPlatformOverride = TargetPlatform.iOS; + final reporter = _RecordingDiagnostics(); + + for (final code in [ + 'dev_enrollment_failed', + 'missing_apns_token', + 'enrollment_in_progress', + ]) { + TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger + .setMockMethodCallHandler(_channel, (call) async { + throw PlatformException(code: code); + }); + await expectLater( + enrollBuzzPush( + 'wss://relay.example/', + 'https://gateway.example/', + diagnostics: reporter, + ), + throwsA(isA()), + ); + } + + expect(reporter.operations, isEmpty); + }); + test('shares and clears the hosted session with the extension', () async { debugDefaultTargetPlatformOverride = TargetPlatform.iOS; final shared = []; @@ -423,3 +471,14 @@ Map _grantMap(String endpointGrant) => { 'generation': 1, 'expiresAt': 1752624000, }; + +class _RecordingDiagnostics extends NoopDiagnosticReporter { + final operations = []; + + @override + void reportError( + Object error, + StackTrace? stackTrace, { + required DiagnosticOperation operation, + }) => operations.add(operation); +}