mirror of
https://github.com/block/buzz.git
synced 2026-11-25 02:31:36 +00:00
## Summary Ticket 7 of the [Android push plan](https://github.com/block/buzz/blob/main/PLANS/ANDROID_PUSH.md). Android phones can now enroll with the push gateway and publish a relay lease, through the same shared push layer iOS uses. The wake handling side is ticket 6, [#9804 Android push wake handling](https://github.com/block/buzz/pull/9804). - The phone gets an FCM token, then generates a sign-only P-256 Keystore key whose attestation challenge is the SHA-256 of the enrollment transcript. The chain goes to the gateway at enrollment. Every later installation call (delegate, rotate endpoint, revoke delegation, revoke installation) is signed with that key over the transcript bytes the gateway builds. - A rotated FCM token rotates the endpoint. Signing out revokes the delegation and the installation. The first enrollment waits a short random time. - Android 13+ asks for the notification permission at the same point iOS does. Declining keeps the app working. - A phone that cannot attest (no hardware attestation, unlocked bootloader, unlisted boot key) keeps working, and Settings says push is unavailable on this phone. - The relay lease keeps the fixed `transport` value iOS sends. - Firebase config is the same build-time setting [#9804 Android push wake handling](https://github.com/block/buzz/pull/9804) adds: four `BUZZ_FIREBASE_*` environment variables become the string resources Firebase reads. Nothing is committed, and builds without them still work (push reports unavailable). The Gradle hunks (permission, config block, `firebase-messaging` dependency) are copied from that PR verbatim so the two merge cleanly, including its `lifecycle-process` line, which this PR does not use. - The token and permission calls use their own `buzz/push_registration` channel, because Android's notification-open handler from that PR already owns `buzz/push` and a channel has one native handler. - The gateway gets an `enroll_android` transcript vector so Dart and Rust agree on the exact bytes. Enrollment state is journaled before each network attempt, so a lost response resumes with byte-identical requests instead of leaving an orphaned installation. ### Related issue None found. Builds on [#9560 enroll Android installations](https://github.com/block/buzz/pull/9560) and [#9562 verify Key Attestation](https://github.com/block/buzz/pull/9562). ### Validation Journey tests cover first enrollment, a second relay, reuse and renewal, lost responses, token rotation, an installation the gateway dropped, a lost Keystore key, a phone that cannot attest, and sign-out revocation, against a fake gateway that keeps the real gateway's semantics. Transcripts replay the shared vectors byte for byte. A settings journey checks the Notifications row and the unavailable row on Android, and a resume table checks the FCM token refresh: it runs while the relay reconnects, but not when push is off or the relay doesn't offer push. On a physical Pixel 9a (debug build, a local test gateway on a local Postgres, reached over `adb reverse`), the real Keystore key and hardware attestation chain enrolled and delegated, and the gateway accepted the attestation. The same session then ran: a signed delivery authorized against the delegation, endpoint rotation and re-delegation at the next epoch, rotating back, delegation revocation, installation revocation, and re-enrolling the same FCM token afterwards. The notification prompt appears when registration starts, before the token is fetched: <img src="https://github.com/user-attachments/assets/2aed92fe-20c1-4658-b990-51c3f3c96120" width="270" alt="Android notification permission prompt"> Not validated on the device: the FCM wake itself (the local gateway has no credentials to call FCM, so the delivery was authorized and then failed at the FCM hop), lease publication to a relay, and the in-app unavailable row, which only the widget tests exercise. A relay-backed run needs a signed-in test community on the phone. ### Settings while turning off Turning notifications off sends a relay tombstone. Until the relay accepts it, the row now says so, on both platforms. An Android build without a gateway also keeps a saved opt-in visible so it can still be turned off; once off, the row goes away. These are rendered from the Settings journey test. | | Before | After | |---|---|---| | Android build without a gateway, saved opt-in | <img src="https://raw.githubusercontent.com/block/buzz/47220acf78ad21639d2a3a8b79be3f0272e74db2/pr-9811--01-before-config-less.png" width="270" alt="Before: no Notifications row"> | <img src="https://raw.githubusercontent.com/block/buzz/47220acf78ad21639d2a3a8b79be3f0272e74db2/pr-9811--02-after-config-less.png" width="270" alt="After: Notifications row with the switch on"> | | After turning off, relay has not accepted yet | <img src="https://raw.githubusercontent.com/block/buzz/47220acf78ad21639d2a3a8b79be3f0272e74db2/pr-9811--03-before-turning-off.png" width="270" alt="Before: plain off switch"> | <img src="https://raw.githubusercontent.com/block/buzz/47220acf78ad21639d2a3a8b79be3f0272e74db2/pr-9811--04-after-turning-off.png" width="270" alt="After: Turning off. Notifications may continue until this finishes."> | ### Regressions - iOS enrollment is unchanged: the shared bridge only branches to the Dart authority on Android. iOS gains the pending opt-out subtitle, and its opt-out now also tombstones after a gateway change using the saved lease id. - The Android app gains the `firebase-messaging` dependency and a `POST_NOTIFICATIONS` permission. Backups exclude the push preferences file. ### Known limits - An FCM token rotation while the app is running is picked up on the next resume or launch, because the wake service owns the single messaging service. It could call into the registration plugin once both Android push PRs merge. - After an endpoint rotation, only the active community's lease is republished. Other enabled communities' grants are void until each is opened, the same way their leases lapse at grant expiry today. --------- Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
485 lines
16 KiB
Dart
485 lines
16 KiB
Dart
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';
|
|
import 'package:buzz/shared/relay/relay_provider.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
const _channel = MethodChannel('buzz/push');
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
setUp(() {
|
|
apnsDeviceToken.value = null;
|
|
apnsRegistrationError.value = null;
|
|
pushEndpointGrants.value = const [];
|
|
pushEndpointGrantError.value = null;
|
|
pushCommunitySnapshotError.value = null;
|
|
pendingPushNotificationLink.value = null;
|
|
installBuzzPushMethodHandler();
|
|
});
|
|
|
|
tearDown(() {
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, null);
|
|
debugDefaultTargetPlatformOverride = null;
|
|
});
|
|
|
|
test('captures APNs token success and clears the previous error', () async {
|
|
apnsRegistrationError.value = 'old error';
|
|
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.handlePlatformMessage(
|
|
_channel.name,
|
|
_channel.codec.encodeMethodCall(
|
|
const MethodCall('apnsTokenChanged', {'token': '01ab'}),
|
|
),
|
|
(_) {},
|
|
);
|
|
expect(apnsDeviceToken.value, '01ab');
|
|
expect(apnsRegistrationError.value, isNull);
|
|
});
|
|
|
|
test(
|
|
'starts native permission and APNs registration without a result gate',
|
|
() async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, (call) async {
|
|
expect(call.method, 'startRegistration');
|
|
return null;
|
|
});
|
|
|
|
await startBuzzPushRegistration();
|
|
},
|
|
);
|
|
|
|
test('purges notifications delivered before age restriction', () async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, (call) async {
|
|
expect(call.method, 'purgeAgeRestrictedNotifications');
|
|
expect(call.arguments, isNull);
|
|
return null;
|
|
});
|
|
|
|
await purgeAgeRestrictedBuzzNotifications();
|
|
});
|
|
|
|
test('reads and exposes persisted endpoint grants on iOS', () async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
final messenger =
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
|
messenger.setMockMethodCallHandler(_channel, (call) async {
|
|
expect(call.method, 'endpointGrants');
|
|
expect(call.arguments, {'gatewayUrl': Env.pushGatewayUrl});
|
|
return [_grantMap('opaque-grant')];
|
|
});
|
|
|
|
final grants = await readBuzzPushEndpointGrants();
|
|
|
|
expect(grants, hasLength(1));
|
|
expect(grants.single.relayOrigin, 'wss://relay.example');
|
|
expect(grants.single.relayPubkey, 'a' * 64);
|
|
expect(grants.single.installationId, 'c' * 32);
|
|
expect(grants.single.endpointGrant, 'opaque-grant');
|
|
expect(grants.single.endpointHash, 'b' * 64);
|
|
expect(grants.single.endpointEpoch, 1);
|
|
expect(grants.single.generation, 1);
|
|
expect(grants.single.expiresAt, 1752624000);
|
|
expect(pushEndpointGrants.value.single.endpointGrant, 'opaque-grant');
|
|
expect(pushEndpointGrantError.value, isNull);
|
|
});
|
|
|
|
test(
|
|
'debug enrollment carries the configured relay and gateway URLs',
|
|
() async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
final methods = <String>[];
|
|
final enrollmentArguments = <Object?>[];
|
|
final snapshotArguments = <Object?>[];
|
|
final messenger =
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
|
|
messenger.setMockMethodCallHandler(_channel, (call) async {
|
|
methods.add(call.method);
|
|
if (call.method == 'enrollPush') {
|
|
enrollmentArguments.add(call.arguments);
|
|
return _grantMap('new-grant');
|
|
}
|
|
if (call.method == 'endpointGrants') {
|
|
return [_grantMap('new-grant')];
|
|
}
|
|
if (call.method == 'syncPushSnapshot') {
|
|
snapshotArguments.add(call.arguments);
|
|
return null;
|
|
}
|
|
fail('Unexpected method ${call.method}');
|
|
});
|
|
|
|
final firstGrant = await enrollBuzzPush(
|
|
'wss://relay.example/',
|
|
'https://gateway-one.example/',
|
|
);
|
|
final secondGrant = await enrollBuzzPush(
|
|
'wss://relay.example/',
|
|
'https://gateway-two.example/',
|
|
communitiesForSnapshotRefresh: [
|
|
Community(
|
|
id: 'community-id',
|
|
name: 'Community',
|
|
relayUrl: 'wss://relay.example/',
|
|
pubkey: 'd' * 64,
|
|
pushNotificationsEnabled: true,
|
|
addedAt: DateTime.fromMillisecondsSinceEpoch(0),
|
|
),
|
|
],
|
|
);
|
|
|
|
expect(firstGrant!.endpointGrant, 'new-grant');
|
|
expect(secondGrant!.endpointGrant, 'new-grant');
|
|
expect(enrollmentArguments, [
|
|
{
|
|
'relayUrl': 'wss://relay.example/',
|
|
'gatewayUrl': 'https://gateway-one.example/',
|
|
},
|
|
{
|
|
'relayUrl': 'wss://relay.example/',
|
|
'gatewayUrl': 'https://gateway-two.example/',
|
|
},
|
|
]);
|
|
expect(methods, [
|
|
'enrollPush',
|
|
'endpointGrants',
|
|
'enrollPush',
|
|
'endpointGrants',
|
|
'syncPushSnapshot',
|
|
]);
|
|
expect(snapshotArguments, [
|
|
{
|
|
'section': 'communities',
|
|
'communities': [
|
|
{
|
|
'id': 'community-id',
|
|
'name': 'Community',
|
|
'relayUrl': 'wss://relay.example/',
|
|
'pubkey': 'd' * 64,
|
|
'policies': <Object?>[],
|
|
},
|
|
],
|
|
'signingKeys': <String, String>{},
|
|
},
|
|
]);
|
|
expect(pushEndpointGrants.value.single.endpointGrant, 'new-grant');
|
|
},
|
|
);
|
|
|
|
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<PlatformException>()),
|
|
);
|
|
|
|
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<PlatformException>()),
|
|
);
|
|
}
|
|
|
|
expect(reporter.operations, isEmpty);
|
|
});
|
|
|
|
test('shares and clears the hosted session with the extension', () async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
final shared = <Object?>[];
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, (call) async {
|
|
expect(call.method, 'syncPushSnapshot');
|
|
shared.add(call.arguments);
|
|
return null;
|
|
});
|
|
|
|
for (final credential in ['session-credential', null]) {
|
|
await shareHostedSessionWithNotifications(
|
|
origin: Uri.parse('https://app.example'),
|
|
credential: credential,
|
|
relayDomains: const ['communities.example'],
|
|
communityDomain: 'communities.example',
|
|
);
|
|
}
|
|
|
|
expect(shared, [
|
|
for (final credential in ['session-credential', null])
|
|
{
|
|
'section': 'hostedSession',
|
|
'origin': 'https://app.example',
|
|
'credential': credential,
|
|
'relayDomains': ['communities.example'],
|
|
'communityDomain': 'communities.example',
|
|
},
|
|
]);
|
|
});
|
|
|
|
test(
|
|
'a build without hosted relay auth clears a shared hosted session',
|
|
() async {
|
|
expect(HostedRelayAuth.configured && HostedEnvironment.enabled, isFalse);
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
final shared = <Object?>[];
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, (call) async {
|
|
shared.add((call.arguments as Map)['credential']);
|
|
return null;
|
|
});
|
|
final container = ProviderContainer();
|
|
addTearDown(container.dispose);
|
|
|
|
// A copy an earlier flag-on build shared is removed instead of kept.
|
|
await container.read(builderlabAccountProvider).onSessionChanged!('live');
|
|
expect(shared, [null]);
|
|
},
|
|
);
|
|
|
|
test('exposes APNs registration failure', () async {
|
|
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.handlePlatformMessage(
|
|
_channel.name,
|
|
_channel.codec.encodeMethodCall(
|
|
const MethodCall('apnsRegistrationFailed', {'message': 'denied'}),
|
|
),
|
|
(_) {},
|
|
);
|
|
expect(apnsRegistrationError.value, 'denied');
|
|
});
|
|
|
|
test('routes a warm notification response with opaque IDs', () async {
|
|
final response = Completer<ByteData?>();
|
|
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.handlePlatformMessage(
|
|
_channel.name,
|
|
_channel.codec.encodeMethodCall(
|
|
MethodCall('notificationOpened', {
|
|
'eventId': 'MESSAGE-ID',
|
|
'communityId': 'community-id',
|
|
'channelId': 'CHANNEL/GENERAL',
|
|
}),
|
|
),
|
|
response.complete,
|
|
);
|
|
|
|
final envelope = await response.future;
|
|
expect(envelope, isNotNull);
|
|
expect(_channel.codec.decodeEnvelope(envelope!), 'handled');
|
|
expect(
|
|
pendingPushNotificationLink.value,
|
|
MessageDeepLink(
|
|
communityId: 'community-id',
|
|
channelId: 'CHANNEL/GENERAL',
|
|
messageId: 'MESSAGE-ID',
|
|
),
|
|
);
|
|
});
|
|
|
|
test(
|
|
'preserves the exact reply and containing thread on a warm tap',
|
|
() async {
|
|
final response = Completer<ByteData?>();
|
|
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.handlePlatformMessage(
|
|
_channel.name,
|
|
_channel.codec.encodeMethodCall(
|
|
const MethodCall('notificationOpened', {
|
|
'eventId': 'older-reply',
|
|
'communityId': 'community-id',
|
|
'channelId': 'channel-id',
|
|
'threadRootId': 'direct-parent',
|
|
}),
|
|
),
|
|
response.complete,
|
|
);
|
|
expect(
|
|
_channel.codec.decodeEnvelope((await response.future)!),
|
|
'handled',
|
|
);
|
|
expect(
|
|
pendingPushNotificationLink.value,
|
|
const MessageDeepLink(
|
|
communityId: 'community-id',
|
|
channelId: 'channel-id',
|
|
messageId: 'older-reply',
|
|
threadRootId: 'direct-parent',
|
|
),
|
|
);
|
|
},
|
|
);
|
|
|
|
for (final invalidThread in ['', 42]) {
|
|
test('rejects malformed thread ID $invalidThread', () async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(
|
|
_channel,
|
|
(_) async => {
|
|
'eventId': 'reply',
|
|
'communityId': 'community',
|
|
'channelId': 'channel',
|
|
'threadRootId': invalidThread,
|
|
},
|
|
);
|
|
await syncPendingBuzzPushNotificationResponse();
|
|
expect(pendingPushNotificationLink.value, isNull);
|
|
});
|
|
}
|
|
|
|
test('rejects a notification response with an empty channel ID', () async {
|
|
final response = Completer<ByteData?>();
|
|
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.handlePlatformMessage(
|
|
_channel.name,
|
|
_channel.codec.encodeMethodCall(
|
|
MethodCall('notificationOpened', {
|
|
'eventId': 'message-id',
|
|
'communityId': 'community-id',
|
|
'channelId': '',
|
|
}),
|
|
),
|
|
response.complete,
|
|
);
|
|
|
|
final envelope = await response.future;
|
|
expect(envelope, isNotNull);
|
|
expect(_channel.codec.decodeEnvelope(envelope!), 'ignored');
|
|
expect(pendingPushNotificationLink.value, isNull);
|
|
});
|
|
|
|
test(
|
|
'rejects a notification response with a non-string message ID',
|
|
() async {
|
|
final response = Completer<ByteData?>();
|
|
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.handlePlatformMessage(
|
|
_channel.name,
|
|
_channel.codec.encodeMethodCall(
|
|
MethodCall('notificationOpened', {
|
|
'eventId': 42,
|
|
'communityId': 'community-id',
|
|
'channelId': 'channel-id',
|
|
}),
|
|
),
|
|
response.complete,
|
|
);
|
|
|
|
final envelope = await response.future;
|
|
expect(envelope, isNotNull);
|
|
expect(_channel.codec.decodeEnvelope(envelope!), 'ignored');
|
|
expect(pendingPushNotificationLink.value, isNull);
|
|
},
|
|
);
|
|
|
|
test('rejects a notification response with an absent message ID', () async {
|
|
final response = Completer<ByteData?>();
|
|
await TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.handlePlatformMessage(
|
|
_channel.name,
|
|
_channel.codec.encodeMethodCall(
|
|
const MethodCall('notificationOpened', {
|
|
'communityId': 'community-id',
|
|
'channelId': 'channel-id',
|
|
}),
|
|
),
|
|
response.complete,
|
|
);
|
|
|
|
final envelope = await response.future;
|
|
expect(envelope, isNotNull);
|
|
expect(_channel.codec.decodeEnvelope(envelope!), 'ignored');
|
|
expect(pendingPushNotificationLink.value, isNull);
|
|
});
|
|
|
|
test('pulls a cold-start notification response from native iOS', () async {
|
|
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
.setMockMethodCallHandler(_channel, (call) async {
|
|
expect(call.method, 'takePendingNotificationResponse');
|
|
return {
|
|
'eventId': 'b' * 64,
|
|
'threadRootId': 'a' * 64,
|
|
'communityId': 'community-id',
|
|
'channelId': '123e4567-e89b-42d3-a456-426614174000',
|
|
};
|
|
});
|
|
|
|
await syncPendingBuzzPushNotificationResponse();
|
|
|
|
expect(
|
|
pendingPushNotificationLink.value,
|
|
MessageDeepLink(
|
|
communityId: 'community-id',
|
|
channelId: '123e4567-e89b-42d3-a456-426614174000',
|
|
messageId: 'b' * 64,
|
|
threadRootId: 'a' * 64,
|
|
),
|
|
);
|
|
});
|
|
}
|
|
|
|
Map<String, Object> _grantMap(String endpointGrant) => {
|
|
'relayOrigin': 'wss://relay.example',
|
|
'relayPubkey': 'a' * 64,
|
|
'installationId': 'c' * 32,
|
|
'endpointGrant': endpointGrant,
|
|
'endpointHash': 'b' * 64,
|
|
'endpointEpoch': 1,
|
|
'generation': 1,
|
|
'expiresAt': 1752624000,
|
|
};
|
|
|
|
class _RecordingDiagnostics extends NoopDiagnosticReporter {
|
|
final operations = <DiagnosticOperation>[];
|
|
|
|
@override
|
|
void reportError(
|
|
Object error,
|
|
StackTrace? stackTrace, {
|
|
required DiagnosticOperation operation,
|
|
}) => operations.add(operation);
|
|
}
|