Skip to content

fix(auth, android): do not cache MultiFactor per uid so getEnrolledFactors is not stale after re-signing in - #19022

Open
tatsuhama wants to merge 4 commits into
firebase:mainfrom
tatsuhama:fix/auth-android-stale-multi-factor
Open

tatsuhama wants to merge 4 commits into
firebase:mainfrom
tatsuhama:fix/auth-android-stale-multi-factor

Conversation

@tatsuhama

@tatsuhama tatsuhama commented Nov 30, 2026 •

Copy link
Copy Markdown

Description

On Android, FlutterFirebaseMultiFactor.getAppMultiFactor cached a MultiFactor per app and uid in multiFactorUserMap. #12488 clears the entry in signOut(), but only while currentUser is still non-null.

When the session ends without an explicit sign-out while signed in, the entry is never removed. For example, unenrolling a second factor makes the backend revoke the session, and the native SDK signs the user out on its own. After the same user signs in again in the same process, getEnrolledFactors() keeps returning the factors of the previous FirebaseUser instance (including the unenrolled one) until the app is killed. user.reload() doesn't help.

This PR stops caching the MultiFactor and returns currentUser.multiFactor every time. The now-unused cache and its cleanup in signOut() are removed. Sessions, resolvers and assertions are already kept in their own maps keyed by id, so nothing relied on the cached instance. The change is Android-only; iOS doesn't have this cache.

Manual verification

The same three-line change applied to firebase_auth 6.7.1 (the code is identical there), on an Android emulator (Pixel 10 AVD):

  1. Enroll a TOTP factor, then unenroll it after reauthenticating with it. The backend revokes the session and the native SDK signs out (Notifying auth state listeners about a sign-out event).
  2. Sign in again as the same user in the same process (no restart).
  3. getEnrolledFactors() no longer returns the TOTP factor. Before the change it still returned it until the app was restarted.

I wrote this change with the help of Claude Code (an AI coding assistant) and reviewed it myself.

Related Issues

Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
    • Added an e2e test in firebase_auth_multi_factor_e2e_test.dart. It makes the native SDK sign out by itself (a disabled user fails reload()), removes the factor on the Auth emulator, signs the same user in again and expects no enrolled factors.
  • All existing and new tests are passing.
    • I ran the multi-factor e2e group locally on an Android emulator with the Auth emulator: all 8 tests pass. With only the Kotlin change reverted, the new test fails (Expected: <0>, Actual: <1>). I didn't run the other suites locally.
  • I updated/added relevant documentation (doc comments with ///). (No public API change; added a code comment explaining why the instance isn't cached.)
  • The analyzer (melos run analyze) does not report any problems on my PR. (Ran dart analyze on the changed test files: no issues.)
  • I read and followed the Flutter Style Guide.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

…ctors 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 (firebase#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 firebase#19021

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request removes the caching of MultiFactor instances per user ID (multiFactorUserMap) in the Android Firebase Auth plugin. Instead, currentUser.multiFactor is retrieved directly to prevent stale cached multi-factor data when a session ends without an explicit sign-out. Additionally, a new contributor has been added to the AUTHORS file. I have no feedback to provide as there are no review comments.

@SelaseKay SelaseKay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tatsuhama, could you add tests for this changes?

…ns 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.
@tatsuhama

Copy link
Copy Markdown
Author

Thanks for the review! I added an e2e test in ccbde5e.

should not return stale factors after the session ends without signOut and the same user signs in again in firebase_auth_multi_factor_e2e_test.dart:

  1. Adds a phone factor to the user on the Auth emulator and reloads, so getEnrolledFactors() returns 1 factor.
  2. Disables the user, so reload() fails with user-disabled and the native SDK signs out by itself. FirebaseAuth.signOut() is never called, as when the backend revokes the session.
  3. Enables the user, removes the factor on the emulator and signs the same user in again.
  4. Expects getEnrolledFactors() to return no factors.

To set this up I added emulatorEnableUser and emulatorSetPhoneMultiFactors to test_utils.dart, sharing the accounts:update call with emulatorDisableUser.

Locally on an Android emulator, all 8 tests in the multi-factor group pass. With only the Kotlin change reverted, the new test fails with Expected: <0>, Actual: <1>.

@tatsuhama
tatsuhama requested a review from SelaseKay November 30, 2026 15:32
@tatsuhama
tatsuhama marked this pull request as ready for review November 30, 2026 15:32

@SelaseKay SelaseKay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tatsuhama, thanks for the PR. LGTM!

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).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tatsuhama, Nit: I think we can remove the comment.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Removed in bd7ead3.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [firebase_auth] Android: getEnrolledFactors returns stale factors after re-signing in, when the previous session ended by token revocation

2 participants