|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Guardrail for https://github.com/aws-amplify/amplify-flutter/issues/5302. |
| 5 | +// |
| 6 | +// `AmplifyAuthCognito` registers itself with the process-wide native Cognito SDK |
| 7 | +// during `addPlugin`, starting with `NativeAuthPlugin.setUp`. Without this guard |
| 8 | +// a secondary isolate gets a bare `StateError` about |
| 9 | +// `BackgroundIsolateBinaryMessenger` from deep inside Pigeon, which says nothing |
| 10 | +// about Amplify, Auth, or isolates. |
| 11 | +// |
| 12 | +// The guard is gated on messenger availability, not root-isolate identity, so it |
| 13 | +// provably cannot fire in a headless `FlutterEngine` — see the comment at the |
| 14 | +// call site. The cost is a known gap, pinned by the last test in this file. |
| 15 | + |
| 16 | +@TestOn('vm') |
| 17 | +library; |
| 18 | + |
| 19 | +import 'dart:async'; |
| 20 | +import 'dart:isolate'; |
| 21 | + |
| 22 | +import 'package:amplify_auth_cognito/amplify_auth_cognito.dart'; |
| 23 | +import 'package:amplify_core/amplify_core.dart'; |
| 24 | +// ignore: implementation_imports |
| 25 | +import 'package:amplify_flutter/src/amplify_isolate.dart'; |
| 26 | +import 'package:flutter/services.dart'; |
| 27 | +import 'package:flutter_test/flutter_test.dart'; |
| 28 | + |
| 29 | +/// What [addPluginInIsolate] observed, sent back over a [SendPort]. |
| 30 | +typedef IsolateReport = Map<String, Object?>; |
| 31 | + |
| 32 | +/// Adds [AmplifyAuthCognito] in the isolate this runs in. |
| 33 | +/// |
| 34 | +/// When [message] carries a [RootIsolateToken], the isolate is bootstrapped with |
| 35 | +/// a binary messenger first, which is the case the guard deliberately does not |
| 36 | +/// catch. |
| 37 | +Future<void> addPluginInIsolate((SendPort, RootIsolateToken?) message) async { |
| 38 | + final (sendPort, token) = message; |
| 39 | + final report = <String, Object?>{}; |
| 40 | + if (token != null) { |
| 41 | + BackgroundIsolateBinaryMessenger.ensureInitialized(token); |
| 42 | + } |
| 43 | + // ignore: invalid_use_of_internal_member |
| 44 | + report['isolateIsInitialized'] = amplifyIsolateIsInitialized; |
| 45 | + try { |
| 46 | + await AmplifyAuthCognito().addPlugin( |
| 47 | + authProviderRepo: AmplifyAuthProviderRepository(), |
| 48 | + ); |
| 49 | + report['errorType'] = null; |
| 50 | + } on Object catch (e) { |
| 51 | + report['errorType'] = e.runtimeType.toString(); |
| 52 | + report['error'] = '$e'; |
| 53 | + } |
| 54 | + sendPort.send(report); |
| 55 | +} |
| 56 | + |
| 57 | +/// Spawns [addPluginInIsolate] and waits for its [IsolateReport]. |
| 58 | +Future<IsolateReport> runInIsolate({RootIsolateToken? token}) async { |
| 59 | + final receivePort = ReceivePort(); |
| 60 | + final errorPort = ReceivePort(); |
| 61 | + final result = Completer<IsolateReport>(); |
| 62 | + |
| 63 | + receivePort.listen((message) { |
| 64 | + if (!result.isCompleted) { |
| 65 | + result.complete((message as Map).cast<String, Object?>()); |
| 66 | + } |
| 67 | + }); |
| 68 | + errorPort.listen((message) { |
| 69 | + if (!result.isCompleted) { |
| 70 | + result.completeError(StateError('Isolate errored: $message')); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + final isolate = await Isolate.spawn( |
| 75 | + addPluginInIsolate, |
| 76 | + (receivePort.sendPort, token), |
| 77 | + onError: errorPort.sendPort, |
| 78 | + errorsAreFatal: true, |
| 79 | + debugName: 'auth-guard', |
| 80 | + ); |
| 81 | + try { |
| 82 | + return await result.future.timeout(const Duration(seconds: 30)); |
| 83 | + } finally { |
| 84 | + isolate.kill(priority: Isolate.immediate); |
| 85 | + receivePort.close(); |
| 86 | + errorPort.close(); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +void main() { |
| 91 | + TestWidgetsFlutterBinding.ensureInitialized(); |
| 92 | + |
| 93 | + group('AmplifyAuthCognito.addPlugin', () { |
| 94 | + test('is refused in a secondary isolate with no messenger', () async { |
| 95 | + final report = await runInIsolate(); |
| 96 | + |
| 97 | + expect(report['isolateIsInitialized'], isFalse); |
| 98 | + expect(report['errorType'], 'PluginError'); |
| 99 | + expect(report['error'], contains('secondary isolate')); |
| 100 | + expect( |
| 101 | + report['error'], |
| 102 | + contains('single instance'), |
| 103 | + reason: 'The message must name the real constraint', |
| 104 | + ); |
| 105 | + expect( |
| 106 | + report['error'], |
| 107 | + contains('root isolate'), |
| 108 | + reason: 'The message must say where to configure instead', |
| 109 | + ); |
| 110 | + expect( |
| 111 | + report['error'], |
| 112 | + contains('AmplifyAuthCognitoDart'), |
| 113 | + reason: 'The message must point at the Dart-only alternative', |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + test('is not refused on the root isolate', () async { |
| 118 | + // The guard must be invisible to every single-isolate app, and to the |
| 119 | + // headless `FlutterEngine` that `amplifyBackgroundProcessing` runs in — |
| 120 | + // both have a messenger. |
| 121 | + // ignore: invalid_use_of_internal_member |
| 122 | + expect(amplifyIsolateIsInitialized, isTrue); |
| 123 | + |
| 124 | + await expectLater( |
| 125 | + AmplifyAuthCognito().addPlugin( |
| 126 | + authProviderRepo: AmplifyAuthProviderRepository(), |
| 127 | + ), |
| 128 | + completes, |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + // KNOWN GAP, pinned deliberately. A secondary isolate bootstrapped with |
| 133 | + // `BackgroundIsolateBinaryMessenger.ensureInitialized` has a messenger, so |
| 134 | + // the guard lets it through. It still cannot work, but for a reason that |
| 135 | + // belongs to Flutter rather than Amplify: registering a host-to-Dart handler |
| 136 | + // is unsupported off the root isolate, so `NativeAuthPlugin.setUp` throws |
| 137 | + // `UnsupportedError`. Reporting that clearly would need root-isolate |
| 138 | + // identity, which is only safe once a headless engine's token has been |
| 139 | + // verified on a device. Whoever closes this gap should expect this to change. |
| 140 | + test( |
| 141 | + 'is NOT refused in a bootstrapped secondary isolate (known gap)', |
| 142 | + () async { |
| 143 | + final report = await runInIsolate(token: RootIsolateToken.instance); |
| 144 | + |
| 145 | + expect( |
| 146 | + report['isolateIsInitialized'], |
| 147 | + isTrue, |
| 148 | + reason: 'Bootstrapping gave this isolate a messenger', |
| 149 | + ); |
| 150 | + // ignore: avoid_print |
| 151 | + print('GAP_ERROR=${report['errorType']}: ${report['error']}'); |
| 152 | + |
| 153 | + // The guard did not fire... |
| 154 | + expect( |
| 155 | + report['errorType'], |
| 156 | + isNot('PluginError'), |
| 157 | + reason: 'Gating on the messenger deliberately lets this case through', |
| 158 | + ); |
| 159 | + // ...but Flutter forbids it anyway, one layer deeper. |
| 160 | + expect(report['errorType'], 'UnsupportedError'); |
| 161 | + expect(report['error'], contains('setMessageHandler')); |
| 162 | + expect( |
| 163 | + report['error'], |
| 164 | + contains('always go to the root isolate'), |
| 165 | + reason: |
| 166 | + 'Host-to-Dart callbacks are impossible in any secondary isolate, ' |
| 167 | + 'which constrains what #5302 can ever support', |
| 168 | + ); |
| 169 | + }, |
| 170 | + ); |
| 171 | + }); |
| 172 | +} |
0 commit comments