Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

hasGroupsReferencingGroup fix

IgorHorta6f792c769ed6
diff --git a/backend/src/ee/services/group/group-dal.ts b/backend/src/ee/services/group/group-dal.ts
index bf2aa87895..2f24af5388 100644
--- a/backend/src/ee/services/group/group-dal.ts
+++ b/backend/src/ee/services/group/group-dal.ts
@@ -594,6 +594,22 @@ export const groupDALFactory = (db: TDbClient) => {
}
};
+ /**
+ * Returns true if any group references the given group via sourceGroupId (self-referential FK).
+ * Used to block delete when this group is the source of linked/copied groups.
+ */
+ const hasGroupsReferencingGroup = async (groupId: string, tx?: Knex): Promise<boolean> => {
+ try {
+ const row = await (tx || db.replicaNode())(TableName.Groups)
+ .whereRaw('"sourceGroupId" = ?', [groupId])
+ .select("id")
+ .first();
+ return Boolean(row);
+ } catch (error) {
+ throw new DatabaseError({ error, name: "Has groups referencing group" });
+ }
+ };
+
return {
...groupOrm,
findGroups,
@@ -605,6 +621,7 @@ export const groupDALFactory = (db: TDbClient) => {
findAllGroupProjects,
findGroupsByProjectId,
findById,
- findOne
+ findOne,
+ hasGroupsReferencingGroup
};
};
diff --git a/backend/src/ee/services/group/group-service.ts b/backend/src/ee/services/group/group-service.ts
index fd044b1f59..5f10b8338d 100644
--- a/backend/src/ee/services/group/group-service.ts
+++ b/backend/src/ee/services/group/group-service.ts
@@ -61,6 +61,7 @@ type TGroupServiceFactoryDep = {
| "transaction"
| "findAllGroupProjects"
| "listAvailableGroups"
+ | "hasGroupsReferencingGroup"
>;
membershipGroupDAL: Pick<TMembershipGroupDALFactory, "find" | "findOne" | "create" | "getGroupById" | "deleteById">;
membershipDAL: Pick<TMembershipDALFactory, "find" | "findOne">;
@@ -338,6 +339,14 @@ export const groupServiceFactory = ({
return group;
}
+ const isReferencedAsSource = await groupDAL.hasGroupsReferencingGroup(id);
+ if (isReferencedAsSource) {
+ throw new BadRequestError({
+ message:
+ "Cannot delete this group because it is used as a source group by other groups. Unlink or delete those groups first."
+ });
+ }
+
const [deletedGroup] = await groupDAL.delete({
id,
orgId: actorOrgId