Skip to content

With declarative security, a function can't enqueue to its own task queue because the managed service account lacks actAs on itself #11554

Open

Description

@IzaakGough

[REQUIRED] Environment info

firebase-tools: 15.36.0, firebase-functions 7.4.0, firebase-admin 13.10.0

Platform: macOS

[REQUIRED] Test case

firebase.json:

{ "functions": [ { "source": "functions", "codebase": "repro", "runtime": "nodejs22" } ] }

functions/index.js:

const { onTaskDispatched } = require("firebase-functions/v2/tasks");
const { onRequest } = require("firebase-functions/v2/https");
const { requiresRole } = require("firebase-functions/v2");
const { initializeApp } = require("firebase-admin/app");
const { getFunctions } = require("firebase-admin/functions");

initializeApp();

requiresRole("roles/cloudtasks.enqueuer");
requiresRole("roles/run.invoker");

exports.reproQueue = onTaskDispatched(async (req) => {
  console.log("dispatched", JSON.stringify(req.data));
});

exports.reproEnqueue = onRequest({ invoker: "private" }, async (req, res) => {
  try {
    await getFunctions()
      .taskQueue("locations/us-central1/functions/reproQueue")
      .enqueue({ at: new Date().toISOString() });
    res.send("ENQUEUED\n");
  } catch (e) {
    res.status(500).send(`ENQUEUE_ERROR ${e.code} ${e.message}\n`);
  }
});

[REQUIRED] Steps to reproduce

  1. Run firebase deploy --only functions:repro and confirm the prompt. This creates a managed service account named firebase-fn-<id>, where <id> is random, with roles/cloudtasks.enqueuer and roles/run.invoker.
  2. Call the function: curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" https://us-central1-<project>.cloudfunctions.net/reproEnqueue

[REQUIRED] Expected behavior

The task is enqueued and reproQueue runs, the same as without declarative security.

[REQUIRED] Actual behavior

The enqueue fails:

ENQUEUE_ERROR functions/permission-denied The principal (user or service account) lacks IAM permission "iam.serviceAccounts.actAs" for the resource "firebase-fn-<id>@<project>.iam.gserviceaccount.com" (or the resource may not exist).

firebase-admin sets the task's oidcToken.serviceAccountEmail to the function's own account, so Cloud Tasks requires the caller to have actAs on that account. Here the caller and the account are the same managed account, and it has no actAs on itself.

Without declarative security, the same code works. Both functions run as the default compute service account, which gets actAs from its project-level roles/editor. So a codebase that works today breaks once it adopts requiresRole.

requiresRole only grants project-level roles. Declaring roles/iam.serviceAccountUser would let the managed account act as every service account in the project.

Kits are not affected. When firebase-admin runs in a kit or extension, it sends its own ID token instead of an oidcToken, so Cloud Tasks doesn't check actAs.

Workaround: Grant the managed account roles/iam.serviceAccountUser on itself:

SA=firebase-fn-<id>@<project>.iam.gserviceaccount.com
gcloud iam service-accounts add-iam-policy-binding "$SA" --project <project> \
  --member="serviceAccount:$SA" --role=roles/iam.serviceAccountUser

With this binding, enqueueing works. It stayed in place through an unchanged redeploy, a redeploy that added a role, and one that removed it again, because the CLI only changes project-level roles. Opting out and back in creates a new managed account, so the binding has to be added again.

A possible fix: when a codebase that uses declarative security has task queue functions, the CLI grants the managed account roles/iam.serviceAccountUser on itself.

Activity

  1. gauranshahuja commented on Nov 25, 2026

    @gauranshahuja
    Contributor

    Hi @IzaakGough, is this one open for an outside contributor? I'd be glad to take it if you're not already on it.

    From reading src/deploy/functions/release/fabricator.ts, grantNewRoles() creates the firebase-fn-<id> account and then adds the codebase's roles at the project level through resourcemanager.addServiceAccountRoles(). Nothing gives the account iam.serviceAccounts.actAs on itself, which is what Cloud Tasks checks when firebase-admin sets the task's oidcToken.serviceAccountEmail to the function's own account.

    The narrowest fix I can see is a binding on the account itself (roles/iam.serviceAccountUser granted to the account, on the account), added when the codebase has a task queue function or asks for roles/cloudtasks.enqueuer, and removed again on opt-out. A project-level roles/iam.serviceAccountUser would also work, but it would let the account act as every service account in the project.

    Does that match what you had in mind, or would you rather handle this one inside the team?

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions