firebase / firebase-functions

Firebase SDK for Cloud Functions
https://firebase.google.com/docs/functions/
MIT License
1.01k stars 202 forks source link

Firebase Blocking Auth Functions Run Incorrectly on Failed Signup #1558

Open lox opened 2 months ago

lox commented 2 months ago

[REQUIRED] Version info

node: n/a

firebase-functions: n/a

firebase-tools: n/a

firebase-admin: n/a

[REQUIRED] Test case

Firebase Blocking Auth functions beforeUserCreated and beforeUserSignedIn fire incorrectly when the a user that already exists in Firebase Auth attempts to register.

Function:

import { initializeApp } from "firebase-admin/app"
import {
  beforeUserCreated,
  beforeUserSignedIn,
} from "firebase-functions/v2/identity";

export const beforecreated = beforeUserCreated((event) => {
  console.log('befoerUserCreated', event)
  return;
});

export const beforesignedin = beforeUserSignedIn((event) => {
  console.log('beforeUserSignedIn', event)
});

initializeApp();

registerUser.js:

import { initializeApp } from "firebase/app";
import { createUserWithEmailAndPassword } from 'firebase/auth'
import { getAuth } from 'firebase/auth'

const firebaseConfig = {
  apiKey: "xxx",
  authDomain: "xxx.firebaseapp.com",
  projectId: "xxx",
  storageBucket: "xxx.appspot.com",
  messagingSenderId: "xxx",
  appId: "xxx"
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app)
const testEmail = `user@example.com`

const userCredential = await createUserWithEmailAndPassword(auth, testEmail, 'testPassword')
console.log(`ℹ️  Created test user ${userCredential.user.uid} with email ${testEmail}`)

When registerUser.js is called multiple times, the beforeSignedIn and beforeUserCreated functions will fire. After the first time for the correct user creation, they will STILL FIRE, and provide random / nonexistent uid's.

This means any code that makes the assumption that these functions will only fire on authenticated sign-ins or post user-creation can potentially be exploited.

[REQUIRED] Steps to reproduce

  1. Deploy functions
  2. Run script multiple times, observe Firebase: Error (auth/email-already-in-use).
  3. Check logs

[REQUIRED] Expected behavior

I'd expect the functions fire as described in the documentation:

[REQUIRED] Actual behavior

Blocking functions fire despite the error Firebase: Error (auth/email-already-in-use).

google-oss-bot commented 2 months ago

I found a few problems with this issue:

lox commented 2 months ago

I reported this to Google Support, Firebase Support and our Account Management In Feb 2024. The issue was passed on to a product team and we didn't hear back.