Skip to content

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

add role slug validation to service

x03220535ef1204184c
diff --git a/backend/src/ee/services/project-template/project-template-service.ts b/backend/src/ee/services/project-template/project-template-service.ts
index 2abcc8850a..90d12020a3 100644
--- a/backend/src/ee/services/project-template/project-template-service.ts
+++ b/backend/src/ee/services/project-template/project-template-service.ts
@@ -1,7 +1,7 @@
import { ForbiddenError } from "@casl/ability";
import { packRules } from "@casl/ability/extra";
-import { OrganizationActionScope, ProjectType, TProjectTemplates } from "@app/db/schemas";
+import { OrganizationActionScope, ProjectMembershipRole, ProjectType, TProjectTemplates } from "@app/db/schemas";
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
import { OrgPermissionActions, OrgPermissionSubjects } from "@app/ee/services/permission/org-permission";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
@@ -252,6 +252,25 @@ export const projectTemplateServiceFactory = ({
});
}
+ if (users) {
+ const templateRoles = roles ?? (projectTemplate.roles as TProjectTemplateRole[]);
+
+ const availableRoleSlugs = new Set([
+ ...Object.values(ProjectMembershipRole),
+ ...templateRoles.map((r) => r.slug)
+ ]);
+
+ users.forEach((user) => {
+ user.roles.forEach((roleSlug) => {
+ if (!availableRoleSlugs.has(roleSlug)) {
+ throw new BadRequestError({
+ message: `User "${user.username}" references invalid role slug "${roleSlug}". Role must be a predefined role or defined in the template roles.`
+ });
+ }
+ });
+ });
+ }
+
if (params.name && projectTemplate.name !== params.name) {
const isConflictingName = Boolean(
await projectTemplateDAL.findOne({