firebase-functions icon indicating copy to clipboard operation
firebase-functions copied to clipboard

V2 RTDB functions *suddenly* trigger with authType=unknown and uid=null

Open bezysoftware opened this issue 6 months ago • 6 comments

Related issues

https://github.com/firebase/firebase-functions/pull/1770

[REQUIRED] Version info

"firebase-admin": "^13.8.0", "firebase-functions": "^7.2.5", "firebase-tools": 15.1.0

node: 22

export const writeTransactionChangeV2 = onValueWritten(
  { ref: '/transactions/{groupId}/{entityId}', ...MAX_MEMORY_TIMEOUT_OPTIONS },
  event => {
    logger.info('AUTH-TEST v2', {
      authId: event.authId ?? null,
      authType: event.authType ?? null,
      instance: event.instance,
      firebaseDatabaseHost: event.firebaseDatabaseHost,
      params: event.params,
      eventId: event.id,
    });
  });

export const writeTransactionChangeV1 = functionsV1
  .database
  .ref('/transactions/{groupId}/{entityId}')
  .onWrite((_change, context) => {
    logger.info('AUTH-TEST v1', {
      auth: context.auth ?? null,
      authType: context.authType ?? null,
      params: context.params,
      eventType: context.eventType,
      eventId: context.eventId,
    });
  });

[REQUIRED] Steps to reproduce

Client app (user authenticated) writes to /transactions/{groupId}/{entityId}.

[REQUIRED] Expected behavior

Logs should show user's uid & authType 'user' for both v1 AND v2 functions

[REQUIRED] Actual behavior

V1 is correct while V2 has unknown authType:

V1 (correct uid)

Image

V2 (uid missing, unknown auth type)

Image

Were you able to successfully deploy your functions?

No issues with deploy

Notes

This used to work. We took months slowly migrating all our functions to v2 and tested this and we new THIS WORKED. But somewhere between May6 and May15 this broke - we suddenly started receiving unknown authType and null uid.

We have 2 Firebase projects (sandbox and live) and only one of them is affected. Both have the same packages, same node, same functions, no functional differences we're aware of. Again, we took migration seriously and this WORKED.

I can share both project IDs if needed

bezysoftware avatar May 20 '26 11:05 bezysoftware

Hi @bezysoftware, thanks for raising this! We will begin looking into this and get back to you as soon as possible with any updates.

IzaakGough avatar May 20 '26 15:05 IzaakGough

I was able to reproduce this using a slightly modified version of your functions above.

import { onValueWritten } from "firebase-functions/v2/database";
import { logger } from "firebase-functions/logger";
import { database } from "firebase-functions/v1";

export const writeTransactionChangeV2 = onValueWritten(
    { ref: '/transactions/{groupId}/{entityId}'},
    event => {
      logger.info('AUTH-TEST v2', {
        authId: event.authId ?? null,
        authType: event.authType ?? null,
        instance: event.instance,
        firebaseDatabaseHost: event.firebaseDatabaseHost,
        params: event.params,
        eventId: event.id,
      });
    });
  
  export const writeTransactionChangeV1 = database
    .ref('/transactions/{groupId}/{entityId}')
    .onWrite((_change, context) => {
      logger.info('AUTH-TEST v1', {
        auth: context.auth ?? null,
        authType: context.authType ?? null,
        params: context.params,
        eventType: context.eventType,
        eventId: context.eventId,
      });
    });

I used the following web to write to the RTDB:

<!doctype html>
  <html lang="en">
    <head>
      <meta charset="utf-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1" />
      <title>RTDB Auth Repro</title>
    </head>
    <body>
      <button id="write">Sign in and write</button>

      <script type="module">
        import { initializeApp } from "https://www.gstatic.com/firebasejs/12.1.0/firebase-app.js";
        import { getAuth, signInAnonymously } from "https://www.gstatic.com/firebasejs/12.1.0/firebase-auth.js";
        import { getDatabase, ref, set } from "https://www.gstatic.com/firebasejs/12.1.0/firebase-database.js";

        const firebaseConfig = {
          apiKey: "<your_api_key>",
          authDomain: "<your-project-id>.firebaseapp.com",
          databaseURL: "https://<your-RTDB-id>.firebaseio.com",
          projectId: "<your-project-id>",
          appId: "<your-app-id>",
        };

        const app = initializeApp(firebaseConfig);
        const auth = getAuth(app);
        const db = getDatabase(app);

        document.getElementById("write").onclick = async () => {
          try {
            const cred = await signInAnonymously(auth);
            console.log("signed in uid:", cred.user.uid);

            const entityId = crypto.randomUUID();
            const path = `transactions/group1/${entityId}`;

            await set(ref(db, path), {
              writtenAt: Date.now(),
              uidSeenByClient: cred.user.uid,
              source: "browser-anon-auth-repro",
            });

            console.log("write complete:", path);
          } catch (err) {
            console.error("repro failed:", err);
          }
        };
      </script>
    </body>
  </html>

Logs from V1 function:

Image

Logs from V2 function:

Image

We will investigate this further and keep you posted on any updates.

IzaakGough avatar May 21 '26 10:05 IzaakGough

@IzaakGough this is now impacting our live environment too. I raised support ticket 10407733

bezysoftware avatar May 21 '26 10:05 bezysoftware

This is a weird one. It appears as though #1770 should fix this.

Interestingly, when logging the raw (non-normalized fields) e.g. event["authid"] and event["authtype"], they are both null. Since master maps (authtype, authid) -> (authType, authId), this suggests the auth context is not present in the CloudEvent payload being delivered to gen2 RTDB triggers, and isn't directly an issue with firebase-functions SDK issue.

I bring this up in an internal meeting and get this escalated.

IzaakGough avatar May 21 '26 14:05 IzaakGough

MRE

IzaakGough avatar May 21 '26 15:05 IzaakGough

Hi there, we have escalated this internally and will provide updates as soon as we have them.

IzaakGough avatar Jul 03 '26 10:07 IzaakGough