Skip to content

Opting out of declarative security leaves the managed service account behind if the etag label is missing #11553

Open

Description

@IzaakGough

[REQUIRED] Environment info

firebase-tools: 15.36.0, firebase-functions 7.4.0

Platform: macOS

[REQUIRED] Test case

firebase.json:

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

fn/index.js:

const { onRequest } = require("firebase-functions/v2/https");
const { requiresRole } = require("firebase-functions/v2");

requiresRole("roles/datastore.viewer");

exports.reproDsecFn = onRequest((req, res) => res.send("ok"));

[REQUIRED] Steps to reproduce

  1. Run firebase deploy --only functions:repro-dsec and confirm the prompt. This creates a managed service account named firebase-fn-<id>, where <id> is random, and adds the label firebase-declarative-security-etag to the function.
  2. Remove that label from the function, keeping its other labels. For example, send PATCH https://cloudfunctions.googleapis.com/v2/projects/<project>/locations/us-central1/functions/reproDsecFn?updateMask=labels with a body containing every current label except firebase-declarative-security-etag. Other tools that rewrite a function's labels, such as Terraform, would likely have the same effect, but only the API call was tested.
  3. Remove the requiresRole line and run firebase deploy --only functions:repro-dsec.
  4. Run gcloud iam service-accounts describe firebase-fn-<id>@<project>.iam.gserviceaccount.com.

[REQUIRED] Expected behavior

The CLI shows the opt-out prompt and deletes the managed service account. If it can't tell that the account belongs to this codebase, it warns and names the account it is leaving behind.

[REQUIRED] Actual behavior

There is no opt-out prompt and no "Deleting managed service account" line:

✔  functions[repro-dsec:reproDsecFn(us-central1)] Successful update operation.
✔  Deploy complete!

The function now runs as the default compute service account. firebase-fn-<id> still exists and still has roles/datastore.viewer. No function uses it any more, so no later deploy finds it or reports it.

Cause: discoverSecurityDetails in src/deploy/functions/prepare.ts only treats a codebase as opting out when it finds both a firebase-fn-* account and the etag label:

if (!requiredRoles && (!existingManagedSA || !haveRolesEtag)) {
  return {};
}

Without the label it returns {}, so the planner never sees the managed account.

The label check stops the CLI from deleting a firebase-fn-* account that a codebase only set by hand, so removing it isn't the fix (see #11552). The account's description already records its owner, for example Managed by Firebase CLI for codebase repro-dsec, which could be a more reliable ownership check than the label.

Workaround: Find the managed accounts the codebase created. Their description names the codebase:

gcloud iam service-accounts list --project <project> \
  --filter="email~^firebase-fn- AND description~'codebase <codebase>'" \
  --format="value(email,description)"

For an account no function uses any more, list its project roles:

SA=firebase-fn-<id>@<project>.iam.gserviceaccount.com
gcloud projects get-iam-policy <project> --flatten=bindings \
  --filter="bindings.members:serviceAccount:$SA" --format="value(bindings.role)"

Remove each role before deleting the account, so no deleted:serviceAccount: entries are left (#11550):

gcloud projects remove-iam-policy-binding <project> --member="serviceAccount:$SA" --role=<role>
gcloud iam service-accounts delete "$SA" --project <project>

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