From ebbbe4779963131de45960ca1c70d94fd850c5f6 Mon Sep 17 00:00:00 2001 From: k-nkmr Date: Tue, 27 Oct 2026 20:07:17 +0900 Subject: [PATCH 1/4] fix(auth): align Android blocking function error code with Apple platforms --- AUTHORS | 1 + docs/auth/errors.md | 5 +- .../src/method_channel/utils/exception.dart | 24 ++-- .../utils_tests/exception_test.dart | 122 ++++++++++-------- 4 files changed, 82 insertions(+), 70 deletions(-) diff --git a/AUTHORS b/AUTHORS index 736f6e70a8c2..4e85bfc111ba 100644 --- a/AUTHORS +++ b/AUTHORS @@ -72,3 +72,4 @@ Max Wojczuk Julian Pscheid Artem Shevchenko Zain Qureshi <43629888+ZainnQureshii@users.noreply.github.com> +Kanako Nakamura diff --git a/docs/auth/errors.md b/docs/auth/errors.md index 5670ab222152..020da051cf60 100644 --- a/docs/auth/errors.md +++ b/docs/auth/errors.md @@ -164,7 +164,7 @@ Situation | Android | Apple | Web | Windows ----------|---------|-------|-----|-------- Wrong email or password, with email enumeration protection enabled | `invalid-credential` | `invalid-credential` | `invalid-credential` | `unknown-error` Error without a more specific code | `unknown` | `unknown` or `internal-error` | `unknown` or `internal-error` | `unknown-error` -A blocking function rejected the operation | `blocking-function-error-response` | `blocking-cloud-function-returned-error` | `internal-error` | `unknown-error` +A blocking function rejected the operation | `blocking-cloud-function-returned-error` | `blocking-cloud-function-returned-error` | `internal-error` | `unknown-error` Password does not meet the password policy | `unknown` | `password-does-not-meet-requirements` | `password-does-not-meet-requirements` | `unknown-error` SMS quota exceeded | `too-many-requests` | `quota-exceeded` | `quota-exceeded` | Not supported No user signed in | `no-current-user` | `no-current-user` | `no-current-user` | `no-signed-in-user` @@ -294,8 +294,7 @@ Code | Description | Platforms Code | Description | Platforms -----|-------------|---------- -`blocking-function-error-response` | A blocking function (such as `beforeUserCreated` or `beforeUserSignedIn`) rejected the operation. `message` contains the error returned by the function. | Android -`blocking-cloud-function-returned-error` | A blocking function rejected the operation. `message` contains the error returned by the function. | Apple +`blocking-cloud-function-returned-error` | A blocking function (such as `beforeUserCreated` or `beforeUserSignedIn`) rejected the operation. `message` contains the error returned by the function. | Android, Apple ### Email actions and links diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart index 6daa60da6f31..d762dcf34f1e 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart @@ -143,17 +143,18 @@ FirebaseException platformExceptionToFirebaseAuthException( // Check for custom error codes that are not returned in the normal errors by Firebase SDKs // The error code is only returned in a String on Android String? _getCustomCode(Map? additionalData, String? message) { - final listOfRecognizedCode = [ + final recognizedCodes = { // This code happens when using Enumerate Email protection - 'INVALID_LOGIN_CREDENTIALS', - // This code happens when using using pre-auth functions - 'BLOCKING_FUNCTION_ERROR_RESPONSE', - ]; - - for (final recognizedCode in listOfRecognizedCode) { - if (additionalData?['message'] == recognizedCode || - (message?.contains(recognizedCode) ?? false)) { - return recognizedCode.toLowerCase().replaceAll('_', '-'); + 'INVALID_LOGIN_CREDENTIALS': 'invalid-login-credentials', + // Use the same error code as on Apple platforms for blocking function errors. + 'BLOCKING_FUNCTION_ERROR_RESPONSE': + 'blocking-cloud-function-returned-error', + }; + + for (final entry in recognizedCodes.entries) { + if (additionalData?['message'] == entry.key || + (message?.contains(entry.key) ?? false)) { + return entry.value; } } @@ -177,7 +178,8 @@ FirebaseAuthMultiFactorExceptionPlatform parseMultiFactorError( } final pigeonMultiFactorInfo = - (additionalData['multiFactorHints'] as List? ?? []).nonNulls + (additionalData['multiFactorHints'] as List? ?? []) + .nonNulls .map(InternalMultiFactorInfo.decode) .toList(); diff --git a/packages/firebase_auth/firebase_auth_platform_interface/test/method_channel_tests/utils_tests/exception_test.dart b/packages/firebase_auth/firebase_auth_platform_interface/test/method_channel_tests/utils_tests/exception_test.dart index 7a3b5de62a25..ffde76e80166 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/test/method_channel_tests/utils_tests/exception_test.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/test/method_channel_tests/utils_tests/exception_test.dart @@ -39,33 +39,25 @@ void main() { }, ); - test( - 'should catch a [PlatformException] and throw a [FirebaseException] with the correct message', - () async { - PlatformException platformException = PlatformException( - code: 'UNKNOWN', - message: - 'An internal error has occurred. [ BLOCKING_FUNCTION_ERROR_RESPONSE:HTTP Cloud Function returned an error: {"error":{"details":"The user is not allowed to log in","message":"","status":"PERMISSION_DENIED"}} ]', - ); + test('normalizes blocking function errors and preserves the JSON message', + () async { + PlatformException platformException = PlatformException( + code: 'UNKNOWN', + message: + 'An internal error has occurred. [ BLOCKING_FUNCTION_ERROR_RESPONSE:HTTP Cloud Function returned an error: {"error":{"details":"The user is not allowed to log in","message":"","status":"PERMISSION_DENIED"}} ]', + ); - expect( - () => convertPlatformException(platformException, StackTrace.empty), - throwsA( - isA() - .having( - (e) => e.code, - 'code', - 'blocking-function-error-response', - ) - .having( - (e) => e.message, - 'message', - '{"error":{"details":"The user is not allowed to log in","message":"","status":"PERMISSION_DENIED"}}', - ), - ), - ); - }, - ); + expect( + () => convertPlatformException(platformException, StackTrace.empty), + throwsA( + isA() + .having((e) => e.code, 'code', + 'blocking-cloud-function-returned-error') + .having((e) => e.message, 'message', + '{"error":{"details":"The user is not allowed to log in","message":"","status":"PERMISSION_DENIED"}}'), + ), + ); + }); test('should catch a [PlatformException] with non-Map details', () async { PlatformException platformException = PlatformException( @@ -90,6 +82,34 @@ void main() { }); group('platformExceptionToFirebaseAuthException()', () { + test('normalizes a blocking function error from pigeon details', () { + PlatformException platformException = PlatformException( + code: 'UNKNOWN', + details: {'message': 'BLOCKING_FUNCTION_ERROR_RESPONSE'}, + ); + + FirebaseAuthException result = platformExceptionToFirebaseAuthException( + platformException, + ) as FirebaseAuthException; + + expect(result.code, equals('blocking-cloud-function-returned-error')); + expect(result.message, isNull); + }); + + test('preserves the invalid login credentials error code', () { + PlatformException platformException = PlatformException( + code: 'UNKNOWN', + message: + 'An internal error has occurred. [ INVALID_LOGIN_CREDENTIALS ]', + ); + + FirebaseAuthException result = platformExceptionToFirebaseAuthException( + platformException, + ) as FirebaseAuthException; + + expect(result.code, equals('invalid-login-credentials')); + }); + test('handles non-Map pigeon details', () { PlatformException platformException = PlatformException( code: 'ERROR_INTERNAL_ERROR', @@ -122,12 +142,10 @@ void main() { }, ); - FirebaseAuthException result = - platformExceptionToFirebaseAuthException( - platformException, - fromPigeon: false, - ) - as FirebaseAuthException; + FirebaseAuthException result = platformExceptionToFirebaseAuthException( + platformException, + fromPigeon: false, + ) as FirebaseAuthException; expect(result.code, equals('unknown')); expect(result.message, equals('PlatformException Message')); expect(result.email, isNull); @@ -160,12 +178,10 @@ void main() { }, ); - FirebaseAuthException result = - platformExceptionToFirebaseAuthException( - platformException, - fromPigeon: false, - ) - as FirebaseAuthException; + FirebaseAuthException result = platformExceptionToFirebaseAuthException( + platformException, + fromPigeon: false, + ) as FirebaseAuthException; expect(result.code, equals('A Known Code')); expect(result.message, equals('A Known Message')); expect(result.email, 'test@email.com'); @@ -185,12 +201,10 @@ void main() { message: 'a message', ); - FirebaseAuthException result = - platformExceptionToFirebaseAuthException( - platformException, - fromPigeon: false, - ) - as FirebaseAuthException; + FirebaseAuthException result = platformExceptionToFirebaseAuthException( + platformException, + fromPigeon: false, + ) as FirebaseAuthException; expect(result.code, equals('unknown')); expect(result.message, equals('a message')); expect(result.email, null); @@ -205,12 +219,10 @@ void main() { details: {'additionalData': null}, ); - FirebaseAuthException result = - platformExceptionToFirebaseAuthException( - platformException, - fromPigeon: false, - ) - as FirebaseAuthException; + FirebaseAuthException result = platformExceptionToFirebaseAuthException( + platformException, + fromPigeon: false, + ) as FirebaseAuthException; expect(result.code, equals('unknown')); expect(result.message, equals('a message')); expect(result.email, isNull); @@ -229,12 +241,10 @@ void main() { }, ); - FirebaseAuthException result = - platformExceptionToFirebaseAuthException( - platformException, - fromPigeon: false, - ) - as FirebaseAuthException; + FirebaseAuthException result = platformExceptionToFirebaseAuthException( + platformException, + fromPigeon: false, + ) as FirebaseAuthException; expect(result.code, equals('A Known Code')); expect(result.message, equals('A Known Message')); expect(result.email, 'test@email.com'); From 602121a24d9f4841e791ce7d1b8a97d5c1ac9a04 Mon Sep 17 00:00:00 2001 From: k-nkmr Date: Tue, 27 Oct 2026 20:33:43 +0900 Subject: [PATCH 2/4] refactor(auth): make custom error code mappings const --- .../lib/src/method_channel/utils/exception.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart index d762dcf34f1e..e6bef5ecb2ec 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart @@ -143,7 +143,7 @@ FirebaseException platformExceptionToFirebaseAuthException( // Check for custom error codes that are not returned in the normal errors by Firebase SDKs // The error code is only returned in a String on Android String? _getCustomCode(Map? additionalData, String? message) { - final recognizedCodes = { + const recognizedCodes = { // This code happens when using Enumerate Email protection 'INVALID_LOGIN_CREDENTIALS': 'invalid-login-credentials', // Use the same error code as on Apple platforms for blocking function errors. From edb9fecc750ac48a9842184356d789c9a00194d5 Mon Sep 17 00:00:00 2001 From: k-nkmr Date: Wed, 28 Oct 2026 19:58:05 +0900 Subject: [PATCH 3/4] test(auth): add e2e coverage for blocking function errors --- .../workflows/scripts/functions/src/index.ts | 13 +++++++- .../firebase_auth_instance_e2e_test.dart | 32 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/.github/workflows/scripts/functions/src/index.ts b/.github/workflows/scripts/functions/src/index.ts index 659834d2e991..70ce787e1419 100644 --- a/.github/workflows/scripts/functions/src/index.ts +++ b/.github/workflows/scripts/functions/src/index.ts @@ -1,6 +1,17 @@ import * as assert from 'assert'; import * as functions from 'firebase-functions'; import * as functionsv2 from 'firebase-functions/v2'; +import { beforeUserCreated } from 'firebase-functions/v2/identity'; + +// Only reject the dedicated Auth e2e address so other tests are unaffected. +export const rejectBlockedAuthUser = beforeUserCreated((event) => { + if (event.data?.email === 'blocked-by-function@example.com') { + throw new functionsv2.https.HttpsError( + 'permission-denied', + 'Registration blocked by the e2e test function.', + ); + } +}); // For example app. @@ -221,4 +232,4 @@ export const testStreamResponse = functions.https.onCall(async (request, respons // Fallback for non-streaming clients return Promise.all(allFruits); -}); \ No newline at end of file +}); diff --git a/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_instance_e2e_test.dart b/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_instance_e2e_test.dart index c81d5decf256..d9d831942637 100644 --- a/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_instance_e2e_test.dart +++ b/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_instance_e2e_test.dart @@ -502,6 +502,38 @@ void main() { } }); + test( + 'normalizes the error when a blocking function rejects registration', + () async { + // Matches rejectBlockedAuthUser in the Functions emulator fixtures. + await expectLater( + FirebaseAuth.instance.createUserWithEmailAndPassword( + email: 'blocked-by-function@example.com', + password: testPassword, + ), + throwsA( + isA() + .having( + (e) => e.code, + 'code', + 'blocking-cloud-function-returned-error', + ) + .having( + (e) => e.message, + 'message', + contains( + 'Registration blocked by the e2e test function.', + ), + ), + ), + ); + expect(FirebaseAuth.instance.currentUser, isNull); + }, + // This regression covers the shared Android and Apple error code. + skip: kIsWeb || + (!Platform.isAndroid && !Platform.isIOS && !Platform.isMacOS), + ); + test('fails if creating a user with an invalid email', () async { await ensureSignedIn(testEmail); try { From b68991e4ed5bda6959688478732b430065bccdca Mon Sep 17 00:00:00 2001 From: k-nkmr Date: Wed, 25 Nov 2026 14:48:17 +0900 Subject: [PATCH 4/4] style(auth): format with latest dart format --- .../firebase_auth_instance_e2e_test.dart | 3 +- .../src/method_channel/utils/exception.dart | 3 +- .../utils_tests/exception_test.dart | 106 ++++++++++-------- 3 files changed, 65 insertions(+), 47 deletions(-) diff --git a/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_instance_e2e_test.dart b/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_instance_e2e_test.dart index d9d831942637..559372b7fe6d 100644 --- a/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_instance_e2e_test.dart +++ b/packages/firebase_auth/firebase_auth/example/integration_test/firebase_auth_instance_e2e_test.dart @@ -530,7 +530,8 @@ void main() { expect(FirebaseAuth.instance.currentUser, isNull); }, // This regression covers the shared Android and Apple error code. - skip: kIsWeb || + skip: + kIsWeb || (!Platform.isAndroid && !Platform.isIOS && !Platform.isMacOS), ); diff --git a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart index e6bef5ecb2ec..f3547ec5be66 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/lib/src/method_channel/utils/exception.dart @@ -178,8 +178,7 @@ FirebaseAuthMultiFactorExceptionPlatform parseMultiFactorError( } final pigeonMultiFactorInfo = - (additionalData['multiFactorHints'] as List? ?? []) - .nonNulls + (additionalData['multiFactorHints'] as List? ?? []).nonNulls .map(InternalMultiFactorInfo.decode) .toList(); diff --git a/packages/firebase_auth/firebase_auth_platform_interface/test/method_channel_tests/utils_tests/exception_test.dart b/packages/firebase_auth/firebase_auth_platform_interface/test/method_channel_tests/utils_tests/exception_test.dart index ffde76e80166..38a0b25142bd 100644 --- a/packages/firebase_auth/firebase_auth_platform_interface/test/method_channel_tests/utils_tests/exception_test.dart +++ b/packages/firebase_auth/firebase_auth_platform_interface/test/method_channel_tests/utils_tests/exception_test.dart @@ -39,25 +39,33 @@ void main() { }, ); - test('normalizes blocking function errors and preserves the JSON message', - () async { - PlatformException platformException = PlatformException( - code: 'UNKNOWN', - message: - 'An internal error has occurred. [ BLOCKING_FUNCTION_ERROR_RESPONSE:HTTP Cloud Function returned an error: {"error":{"details":"The user is not allowed to log in","message":"","status":"PERMISSION_DENIED"}} ]', - ); + test( + 'normalizes blocking function errors and preserves the JSON message', + () async { + PlatformException platformException = PlatformException( + code: 'UNKNOWN', + message: + 'An internal error has occurred. [ BLOCKING_FUNCTION_ERROR_RESPONSE:HTTP Cloud Function returned an error: {"error":{"details":"The user is not allowed to log in","message":"","status":"PERMISSION_DENIED"}} ]', + ); - expect( - () => convertPlatformException(platformException, StackTrace.empty), - throwsA( - isA() - .having((e) => e.code, 'code', - 'blocking-cloud-function-returned-error') - .having((e) => e.message, 'message', - '{"error":{"details":"The user is not allowed to log in","message":"","status":"PERMISSION_DENIED"}}'), - ), - ); - }); + expect( + () => convertPlatformException(platformException, StackTrace.empty), + throwsA( + isA() + .having( + (e) => e.code, + 'code', + 'blocking-cloud-function-returned-error', + ) + .having( + (e) => e.message, + 'message', + '{"error":{"details":"The user is not allowed to log in","message":"","status":"PERMISSION_DENIED"}}', + ), + ), + ); + }, + ); test('should catch a [PlatformException] with non-Map details', () async { PlatformException platformException = PlatformException( @@ -88,9 +96,9 @@ void main() { details: {'message': 'BLOCKING_FUNCTION_ERROR_RESPONSE'}, ); - FirebaseAuthException result = platformExceptionToFirebaseAuthException( - platformException, - ) as FirebaseAuthException; + FirebaseAuthException result = + platformExceptionToFirebaseAuthException(platformException) + as FirebaseAuthException; expect(result.code, equals('blocking-cloud-function-returned-error')); expect(result.message, isNull); @@ -103,9 +111,9 @@ void main() { 'An internal error has occurred. [ INVALID_LOGIN_CREDENTIALS ]', ); - FirebaseAuthException result = platformExceptionToFirebaseAuthException( - platformException, - ) as FirebaseAuthException; + FirebaseAuthException result = + platformExceptionToFirebaseAuthException(platformException) + as FirebaseAuthException; expect(result.code, equals('invalid-login-credentials')); }); @@ -142,10 +150,12 @@ void main() { }, ); - FirebaseAuthException result = platformExceptionToFirebaseAuthException( - platformException, - fromPigeon: false, - ) as FirebaseAuthException; + FirebaseAuthException result = + platformExceptionToFirebaseAuthException( + platformException, + fromPigeon: false, + ) + as FirebaseAuthException; expect(result.code, equals('unknown')); expect(result.message, equals('PlatformException Message')); expect(result.email, isNull); @@ -178,10 +188,12 @@ void main() { }, ); - FirebaseAuthException result = platformExceptionToFirebaseAuthException( - platformException, - fromPigeon: false, - ) as FirebaseAuthException; + FirebaseAuthException result = + platformExceptionToFirebaseAuthException( + platformException, + fromPigeon: false, + ) + as FirebaseAuthException; expect(result.code, equals('A Known Code')); expect(result.message, equals('A Known Message')); expect(result.email, 'test@email.com'); @@ -201,10 +213,12 @@ void main() { message: 'a message', ); - FirebaseAuthException result = platformExceptionToFirebaseAuthException( - platformException, - fromPigeon: false, - ) as FirebaseAuthException; + FirebaseAuthException result = + platformExceptionToFirebaseAuthException( + platformException, + fromPigeon: false, + ) + as FirebaseAuthException; expect(result.code, equals('unknown')); expect(result.message, equals('a message')); expect(result.email, null); @@ -219,10 +233,12 @@ void main() { details: {'additionalData': null}, ); - FirebaseAuthException result = platformExceptionToFirebaseAuthException( - platformException, - fromPigeon: false, - ) as FirebaseAuthException; + FirebaseAuthException result = + platformExceptionToFirebaseAuthException( + platformException, + fromPigeon: false, + ) + as FirebaseAuthException; expect(result.code, equals('unknown')); expect(result.message, equals('a message')); expect(result.email, isNull); @@ -241,10 +257,12 @@ void main() { }, ); - FirebaseAuthException result = platformExceptionToFirebaseAuthException( - platformException, - fromPigeon: false, - ) as FirebaseAuthException; + FirebaseAuthException result = + platformExceptionToFirebaseAuthException( + platformException, + fromPigeon: false, + ) + as FirebaseAuthException; expect(result.code, equals('A Known Code')); expect(result.message, equals('A Known Message')); expect(result.email, 'test@email.com');