Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(frontend): revoke the exact project role binding on delete (BYT-9770) (#20664)

boojack633993e23924
diff --git a/frontend/src/react/pages/settings/MembersPage.tsx b/frontend/src/react/pages/settings/MembersPage.tsx
index 98a286ab2d..bc74d5b631 100644
--- a/frontend/src/react/pages/settings/MembersPage.tsx
+++ b/frontend/src/react/pages/settings/MembersPage.tsx
@@ -111,6 +111,7 @@ import {
hasProjectPermissionV2,
hasWorkspacePermissionV2,
isBindingPolicyExpired,
+ revokeMemberFromBinding,
sortRoles,
} from "@/utils";
import {
@@ -1501,17 +1502,11 @@ function EditMemberRoleDrawer({
return;
setIsRequesting(true);
try {
- const policy = structuredClone(getProjectIamPolicy(projectName));
- const match = policy.bindings.find(
- (b) =>
- b.role === roleBinding.role &&
- (b.condition?.expression ?? "") ===
- (roleBinding.condition?.expression ?? "")
+ const policy = revokeMemberFromBinding(
+ getProjectIamPolicy(projectName),
+ roleBinding,
+ member.binding
);
- if (match) {
- match.members = match.members.filter((m) => m !== member.binding);
- }
- policy.bindings = policy.bindings.filter((b) => b.members.length > 0);
await updateProjectIamPolicy(projectName, policy);
pushNotification({
module: "bytebase",
diff --git a/frontend/src/utils/v1/iam.test.ts b/frontend/src/utils/v1/iam.test.ts
new file mode 100644
index 0000000000..883c4f37b4
--- /dev/null
+++ b/frontend/src/utils/v1/iam.test.ts
@@ -0,0 +1,107 @@
+import { create } from "@bufbuild/protobuf";
+import { describe, expect, test } from "vitest";
+import { ExprSchema as ConditionExprSchema } from "@/types/proto-es/google/type/expr_pb";
+import {
+ BindingSchema,
+ IamPolicySchema,
+} from "@/types/proto-es/v1/iam_policy_pb";
+import { revokeMemberFromBinding } from "./iam";
+
+// A database-scoped grant with no expiration. Several users granted the same
+// database via separate access requests end up as separate single-member
+// bindings that share this exact role + condition expression.
+const DB_SCOPED =
+ '(resource.database in ["instances/nf-prod-jfapp/databases/ekb_app2"])';
+
+const grant = (member: string) =>
+ create(BindingSchema, {
+ role: "roles/sqlEditorReadUser",
+ members: [member],
+ condition: create(ConditionExprSchema, { expression: DB_SCOPED }),
+ });
+
+describe("revokeMemberFromBinding", () => {
+ test("revokes the clicked binding when several bindings share role and condition", () => {
+ const grantA = grant("user:[email protected]");
+ const grantB = grant("user:[email protected]");
+ const policy = create(IamPolicySchema, { bindings: [grantA, grantB] });
+
+ const result = revokeMemberFromBinding(
+ policy,
+ grantB,
+ );
+
+ const members = result.bindings.flatMap((b) => b.members);
+ expect(members).not.toContain("user:[email protected]");
+ expect(members).toContain("user:[email protected]");
+ });
+
+ test("removes only the targeted member from a shared multi-member binding", () => {
+ const binding = create(BindingSchema, {
+ role: "roles/sqlEditorReadUser",
+ members: [
+ ],
+ condition: create(ConditionExprSchema, { expression: DB_SCOPED }),
+ });
+ const policy = create(IamPolicySchema, { bindings: [binding] });
+
+ const result = revokeMemberFromBinding(
+ policy,
+ binding,
+ );
+
+ expect(result.bindings).toHaveLength(1);
+ expect(result.bindings[0].members).toEqual([
+ ]);
+ });
+
+ test("drops the binding once its last member is revoked", () => {
+ const binding = grant("user:[email protected]");
+ const policy = create(IamPolicySchema, { bindings: [binding] });
+
+ const result = revokeMemberFromBinding(
+ policy,
+ binding,
+ );
+
+ expect(result.bindings).toHaveLength(0);
+ });
+
+ test("does not mutate the input policy", () => {
+ const grantA = grant("user:[email protected]");
+ const grantB = grant("user:[email protected]");
+ const policy = create(IamPolicySchema, { bindings: [grantA, grantB] });
+
+ revokeMemberFromBinding(policy, grantB, "user:[email protected]");
+
+ expect(policy.bindings).toHaveLength(2);
+ expect(policy.bindings[1].members).toEqual(["user:[email protected]"]);
+ });
+
+ test("falls back to role + condition + membership when the target is not the same reference", () => {
+ const grantA = grant("user:[email protected]");
+ const grantB = grant("user:[email protected]");
+ const policy = create(IamPolicySchema, { bindings: [grantA, grantB] });
+ // A structurally-equal copy with a different object identity, e.g. after a
+ // round-trip through the cache.
+ const targetCopy = grant("user:[email protected]");
+
+ const result = revokeMemberFromBinding(
+ policy,
+ targetCopy,
+ );
+
+ const members = result.bindings.flatMap((b) => b.members);
+ expect(members).not.toContain("user:[email protected]");
+ expect(members).toContain("user:[email protected]");
+ });
+});
diff --git a/frontend/src/utils/v1/iam.ts b/frontend/src/utils/v1/iam.ts
index 41e4163f46..f3246ac1dd 100644
--- a/frontend/src/utils/v1/iam.ts
+++ b/frontend/src/utils/v1/iam.ts
@@ -44,6 +44,41 @@ export const isBindingPolicyExpired = (binding: Binding): boolean => {
return false;
};
+// Revoke a single member from one specific role binding, returning a new policy
+// (the input is left untouched).
+//
+// The target binding is resolved by reference identity first, then by role +
+// condition expression + membership. Identity matching matters because a policy
+// can hold several bindings that share the same (role, condition) — e.g. when
+// multiple users are granted the same database with no expiration, each grant is
+// stored as a separate single-member binding. Resolving only by (role,
+// condition) would match the first such binding, which need not contain the
+// member being revoked, silently leaving the intended grant in place.
+export const revokeMemberFromBinding = (
+ policy: IamPolicy,
+ targetBinding: Binding,
+ member: string
+): IamPolicy => {
+ let index = policy.bindings.indexOf(targetBinding);
+ if (index < 0) {
+ index = policy.bindings.findIndex(
+ (b) =>
+ b.role === targetBinding.role &&
+ (b.condition?.expression ?? "") ===
+ (targetBinding.condition?.expression ?? "") &&
+ b.members.includes(member)
+ );
+ }
+ const next = structuredClone(policy);
+ if (index >= 0) {
+ next.bindings[index].members = next.bindings[index].members.filter(
+ (m) => m !== member
+ );
+ }
+ next.bindings = next.bindings.filter((b) => b.members.length > 0);
+ return next;
+};
+
export const convertMemberToFullname = (member: string) => {
if (member.startsWith(groupBindingPrefix)) {
return ensureGroupIdentifier(member);