Skip to content

Opting out of declarative security reports success when deleting the managed service account fails #11551

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 as an owner and confirm the prompt. This creates a managed service account named firebase-fn-<id>, where <id> is random.
  2. Switch to a deployer that can deploy functions but doesn't have iam.serviceAccounts.delete (for example roles/cloudfunctions.admin, roles/run.admin and roles/iam.serviceAccountUser).
  3. Remove the requiresRole line, run firebase deploy --only functions:repro-dsec --debug, and confirm the prompt.
  4. Run gcloud iam service-accounts describe firebase-fn-<id>@<project>.iam.gserviceaccount.com.

[REQUIRED] Expected behavior

The deploy fails with the CLI's existing error: "Failed to delete managed service account ... Please ask an IAM administrator to delete it manually."

[REQUIRED] Actual behavior

The delete returns 403, but the deploy reports success and exits 0:

✔  functions[repro-dsec:reproDsecFn(us-central1)] Successful update operation.
i  functions: Deleting managed service account firebase-fn-<id>@<project>.iam.gserviceaccount.com...
✔  Deploy complete!

From the debug log:

<<< [apiv2][status] DELETE https://iam.googleapis.com/v1/projects/<project>/serviceAccounts/firebase-fn-<id>@<project>.iam.gserviceaccount.com 403
<<< [apiv2][body] ... {"error":{"code":403,"message":"Permission 'iam.serviceAccounts.delete' denied on resource ...

The account still exists and keeps its roles. The function now runs as the default compute service account and no longer has the firebase-declarative-security-etag label, so later deploys don't find the account and nothing reports it.

Cause: deleteServiceAccount in src/gcp/iam.ts passes resolveOnHTTPError: true:

await apiClient.delete(`/projects/${projectId}/serviceAccounts/${accountEmail}`, {
  resolveOnHTTPError: true,
});

So an HTTP error is returned instead of thrown, and the catch around the call in Fabricator.removeOldRoles never runs. Checking the status, ignoring a 404 and throwing on anything else, would fix this.

Workaround: To avoid this, make sure the deployer has iam.serviceAccounts.delete (for example roles/iam.serviceAccountAdmin) before opting out.

To clean up an account that was left behind, first 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)"

Check that no function still uses the account. This prints nothing for an orphaned account:

SA=firebase-fn-<id>@<project>.iam.gserviceaccount.com
gcloud functions list --project <project> \
  --filter="serviceConfig.serviceAccountEmail:$SA" --format="value(name)"

List its project roles:

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

Remove each role, then delete the account, as someone who has iam.serviceAccounts.delete. Removing the roles first means 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