Skip to content

A codebase that uses another codebase's managed service account can revoke its roles or delete it #11552

Open

Description

@IzaakGough

[REQUIRED] Environment info

firebase-tools: 15.36.0, firebase-functions 7.4.0

Platform: macOS

[REQUIRED] Test case

firebase.json with two codebases:

{
  "functions": [
    { "source": "a", "codebase": "a" },
    { "source": "b", "codebase": "b" }
  ]
}

a/index.js:

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

requiresRole("roles/datastore.user");
requiresRole("roles/storage.objectViewer");

exports.afn = onRequest((req, res) => res.send("a"));

b/index.js, using codebase a's managed service account:

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

const SA = "firebase-fn-<id>@<project>.iam.gserviceaccount.com";

exports.bfn = onRequest({ serviceAccount: SA }, (req, res) => res.send("b"));

[REQUIRED] Steps to reproduce

  1. Run firebase deploy --only functions:a. This creates a managed service account named firebase-fn-<id>, where <id> is random, with both roles. Put that name in b/index.js.
  2. Run firebase deploy --only functions:b. This is allowed, and no roles change.

Then either:

  • Revoke: add requiresRole("roles/datastore.user") to b/index.js, run firebase deploy --only functions:b, then run firebase deploy --only functions:a without changes.
  • Delete: run firebase functions:delete b, then run firebase deploy --only functions:a without changes.

[REQUIRED] Expected behavior

Codebase b can't change or delete a service account that codebase a manages. The account's description already records its owner: Managed by Firebase CLI for codebase a.

[REQUIRED] Actual behavior

Revoke: deploying b takes over a's account and removes the role that b doesn't declare:

? Deploying this code will modify the managed service account for codebase
All functions in this codebase will lose access to the following role(s):
* Storage Object Viewer
i  functions: Revoking unneeded IAM roles from firebase-fn-<id>@<project>.iam.gserviceaccount.com for codebase b...

The account is left with only roles/datastore.user. Redeploying a doesn't grant the role back, because a's firebase-declarative-security-etag label hasn't changed. Codebase a's functions lose access to Cloud Storage.

Delete: deleting b also deletes a's account, even though b never declared roles:

? You are about to delete the following Cloud Functions:
	b:bfn(us-central1)
The following managed service accounts will also be deleted:
	firebase-fn-<id>@<project>.iam.gserviceaccount.com
  Are you sure? Yes
✔  functions[b:bfn(us-central1)] Successful delete operation.
i  functions: Deleting managed service account firebase-fn-<id>@<project>.iam.gserviceaccount.com...

Codebase a's function still points at the deleted account, and redeploying a fails:

HTTP Error: 404, Service account projects/-/serviceAccounts/firebase-fn-<id>@<project>.iam.gserviceaccount.com was not found.
⚠  functions:  failed to update function projects/<project>/locations/us-central1/functions/afn

The prompts don't say that the account belongs to another codebase. With --force, no prompt is shown at all.

Cause: discoverSecurityDetails in src/deploy/functions/prepare.ts treats any deployed function in the codebase whose account starts with firebase-fn- as that codebase's managed account. It doesn't check which codebase created it:

const existingManagedSA =
  backend.findEndpoint(
    have,
    (e) => typeof e.serviceAccount === "string" && e.serviceAccount.startsWith("firebase-fn-"),
  )?.serviceAccount ?? undefined;

From there, the planner revokes undeclared roles from that account, or deletes it once the codebase has no functions left. The deploy path requires the etag label before deleting, but the delete path doesn't.

Workaround: Don't set another codebase's firebase-fn-* account as serviceAccount. Use your own service account instead.

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