From cc127bf1728f2158fe2528f556774b697a5c81a7 Mon Sep 17 00:00:00 2001 From: Kenichi Tatsuhama Date: Mon, 30 Nov 2026 20:15:44 +0900 Subject: [PATCH 1/4] fix(auth, android): do not cache MultiFactor per uid so getEnrolledFactors is not stale after re-signing in `getAppMultiFactor` cached a `MultiFactor` per app and uid. The cache was only cleared in `signOut()` while `currentUser` was still non-null (#12488). When the session ends without an explicit sign-out while signed in (e.g. the backend revokes the session after a second factor is unenrolled and the native SDK signs the user out on its own), the entry stayed. After the same user signed in again, `getEnrolledFactors()` returned the factors of the previous `FirebaseUser` instance until the process was killed. Return `currentUser.multiFactor` every time instead. Sessions, resolvers and assertions are already kept in their own maps keyed by id, so nothing relies on the cached instance. Fixes #19021 --- .../plugins/firebase/auth/FlutterFirebaseAuthPlugin.kt | 4 ---- .../plugins/firebase/auth/FlutterFirebaseMultiFactor.kt | 7 ++++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseAuthPlugin.kt b/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseAuthPlugin.kt index a59e485660de..dcc0b7435721 100644 --- a/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseAuthPlugin.kt +++ b/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseAuthPlugin.kt @@ -273,10 +273,6 @@ class FlutterFirebaseAuthPlugin : override fun signOut(app: AuthPigeonFirebaseApp, callback: (Result) -> Unit) { try { val firebaseAuth = getAuthFromPigeon(app) - if (firebaseAuth.currentUser != null) { - FlutterFirebaseMultiFactor.multiFactorUserMap[app.appName]?.remove( - firebaseAuth.currentUser!!.uid) - } firebaseAuth.signOut() callback(Result.success(Unit)) } catch (e: Exception) { diff --git a/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseMultiFactor.kt b/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseMultiFactor.kt index da12c0ad6c17..4fc90e15bbb6 100644 --- a/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseMultiFactor.kt +++ b/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseMultiFactor.kt @@ -20,8 +20,10 @@ class FlutterFirebaseMultiFactor : MultiFactorUserHostApi, MultiFactoResolverHos val currentUser = FlutterFirebaseAuthUser.getCurrentUserFromPigeon(app) ?: throw FirebaseNoSignedInUserException("No user is signed in") - val appMultiFactorUser = multiFactorUserMap.getOrPut(app.appName) { HashMap() } - return appMultiFactorUser.getOrPut(currentUser.uid) { currentUser.multiFactor } + // Do not cache the MultiFactor per uid. It belongs to a specific FirebaseUser instance, so a + // cached one becomes stale when the same user signs in again after the session ended without + // an explicit signOut() (e.g. the session was revoked after unenrolling a factor). + return currentUser.multiFactor } override fun enrollPhone( @@ -181,7 +183,6 @@ class FlutterFirebaseMultiFactor : MultiFactorUserHostApi, MultiFactoResolverHos } companion object { - val multiFactorUserMap: MutableMap> = HashMap() val multiFactorSessionMap: MutableMap = HashMap() val multiFactorResolverMap: MutableMap = HashMap() val multiFactorAssertionMap: MutableMap = HashMap() From 3753e2aeb34176631d9c3f0411feb1f5e752f92d Mon Sep 17 00:00:00 2001 From: Kenichi Tatsuhama Date: Mon, 30 Nov 2026 20:24:02 +0900 Subject: [PATCH 2/4] chore: add Kenichi Tatsuhama to AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index da382e6f0055..15e2159471a1 100644 --- a/AUTHORS +++ b/AUTHORS @@ -74,3 +74,4 @@ Artem Shevchenko Adem Ocel Bilal Aksoy Zain Qureshi <43629888+ZainnQureshii@users.noreply.github.com> +Kenichi Tatsuhama From ccbde5efc5fbb76a25322f22e1da05086b734fcc Mon Sep 17 00:00:00 2001 From: Kenichi Tatsuhama Date: Tue, 1 Dec 2026 00:28:11 +0900 Subject: [PATCH 3/4] test(auth, android): cover getEnrolledFactors after the same user signs in again without signOut The native SDK signs out by itself when the session is no longer valid, so FirebaseAuth.signOut() is never called. The test makes that happen with a disabled user, changes the factors on the server, signs the same user in again and expects the current factors instead of the ones from the previous session. --- .../firebase_auth_multi_factor_e2e_test.dart | 50 +++++++++++++++++++ .../example/integration_test/test_utils.dart | 35 ++++++++++++- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_multi_factor_e2e_test.dart b/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_multi_factor_e2e_test.dart index c61f0aedc0a7..e1c27b5487bb 100644 --- a/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_multi_factor_e2e_test.dart +++ b/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_multi_factor_e2e_test.dart @@ -40,6 +40,56 @@ void main() { // Assertions expect((await multiFactor.getEnrolledFactors()).length, 0); }); + + test('should not return stale factors after the session ends without ' + 'signOut and the same user signs in again', () async { + final email = generateRandomEmail(); + final user = + (await FirebaseAuth.instance.createUserWithEmailAndPassword( + email: email, + password: testPassword, + )).user!; + + await emulatorSetPhoneMultiFactors(user.uid, ['+441444555777']); + await user.reload(); + expect( + (await FirebaseAuth.instance.currentUser!.multiFactor + .getEnrolledFactors()) + .length, + 1, + ); + + // The native SDK signs out by itself when the session is no longer + // valid, so FirebaseAuth.signOut() is never called. + await emulatorDisableUser(user.uid); + await expectLater( + user.reload(), + throwsA( + isA().having( + (e) => e.code, + 'code', + 'user-disabled', + ), + ), + ); + expect(FirebaseAuth.instance.currentUser, isNull); + + // The factor is removed elsewhere, then the same user signs in again. + await emulatorEnableUser(user.uid); + await emulatorSetPhoneMultiFactors(user.uid, []); + await FirebaseAuth.instance.signInWithEmailAndPassword( + email: email, + password: testPassword, + ); + + // Assertions + expect( + (await FirebaseAuth.instance.currentUser!.multiFactor + .getEnrolledFactors()) + .length, + 0, + ); + }); }); group('session', () { diff --git a/packages/firebase_auth/firebase_auth/example/integration_test/test_utils.dart b/packages/firebase_auth/firebase_auth/example/integration_test/test_utils.dart index f71af7bfa26b..fb788c43bf1f 100644 --- a/packages/firebase_auth/firebase_auth/example/integration_test/test_utils.dart +++ b/packages/firebase_auth/firebase_auth/example/integration_test/test_utils.dart @@ -61,8 +61,34 @@ Future emulatorClearAllUsers() async { /// Disable a specific user by uid. Future emulatorDisableUser(String uid) async { - String body = jsonEncode({'disableUser': true, 'localId': uid}); - await http.post( + await _emulatorUpdateAccount({'disableUser': true, 'localId': uid}); +} + +/// Enable a specific user by uid. +Future emulatorEnableUser(String uid) async { + await _emulatorUpdateAccount({'disableUser': false, 'localId': uid}); +} + +/// Replace the phone second factors of a specific user by uid on the server, +/// as an administrator would. An empty list removes all of them. +Future emulatorSetPhoneMultiFactors( + String uid, + List phoneNumbers, +) async { + await _emulatorUpdateAccount({ + 'localId': uid, + 'mfa': { + 'enrollments': [ + for (final (index, phoneNumber) in phoneNumbers.indexed) + {'mfaEnrollmentId': 'phone-$index', 'phoneInfo': phoneNumber}, + ], + }, + }); +} + +Future _emulatorUpdateAccount(Map fields) async { + String body = jsonEncode(fields); + final response = await http.post( Uri.parse( 'http://$testEmulatorHost:$testEmulatorPort/identitytoolkit.googleapis.com/v1/accounts:update', ), @@ -73,6 +99,11 @@ Future emulatorDisableUser(String uid) async { }, body: body, ); + if (response.statusCode != 200) { + throw Exception( + 'accounts:update failed: ${response.statusCode} ${response.body}', + ); + } } /// Retrieve a sms phone authentication code that may have been sent for a specific From bd7ead3ce216742ddbfbc56b81949df9af038f1e Mon Sep 17 00:00:00 2001 From: Kenichi Tatsuhama Date: Tue, 1 Dec 2026 22:37:39 +0900 Subject: [PATCH 4/4] chore(auth, android): remove comment on getAppMultiFactor --- .../plugins/firebase/auth/FlutterFirebaseMultiFactor.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseMultiFactor.kt b/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseMultiFactor.kt index 4fc90e15bbb6..39059b594441 100644 --- a/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseMultiFactor.kt +++ b/packages/firebase_auth/firebase_auth/android/src/main/kotlin/io/flutter/plugins/firebase/auth/FlutterFirebaseMultiFactor.kt @@ -20,9 +20,6 @@ class FlutterFirebaseMultiFactor : MultiFactorUserHostApi, MultiFactoResolverHos val currentUser = FlutterFirebaseAuthUser.getCurrentUserFromPigeon(app) ?: throw FirebaseNoSignedInUserException("No user is signed in") - // Do not cache the MultiFactor per uid. It belongs to a specific FirebaseUser instance, so a - // cached one becomes stale when the same user signs in again after the session ended without - // an explicit signOut() (e.g. the session was revoked after unenrolling a factor). return currentUser.multiFactor }