Repository navigation
Description
[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
- Run
firebase deploy --only functions:reproand confirm the prompt. This creates a managed service account namedfirebase-fn-<id>, where<id>is random, withroles/cloudtasks.enqueuerandroles/run.invoker. - 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
gauranshahuja commentedon Nov 25, 2026
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 thefirebase-fn-<id>account and then adds the codebase's roles at the project level throughresourcemanager.addServiceAccountRoles(). Nothing gives the accountiam.serviceAccounts.actAson itself, which is what Cloud Tasks checks when firebase-admin sets the task'soidcToken.serviceAccountEmailto the function's own account.The narrowest fix I can see is a binding on the account itself (
roles/iam.serviceAccountUsergranted to the account, on the account), added when the codebase has a task queue function or asks forroles/cloudtasks.enqueuer, and removed again on opt-out. A project-levelroles/iam.serviceAccountUserwould 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?