Skip to content

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

fix(kms): block cross-project key deletion via project cleanup

bernie-gbd2c91af4969
diff --git a/backend/src/services/kms/kms-service.ts b/backend/src/services/kms/kms-service.ts
index 5bfce2733a..524e549451 100644
--- a/backend/src/services/kms/kms-service.ts
+++ b/backend/src/services/kms/kms-service.ts
@@ -299,11 +299,14 @@ export const kmsServiceFactory = ({
return tx ? dbQuery(tx) : kmsDAL.transaction(dbQuery);
};
+ // a project only owns the reserved key generated for it. Anything else is the CMEK API's to delete
const deleteInternalKms = async (kmsId: string, orgId: string, tx?: Knex) => {
const kms = await kmsDAL.findByIdWithAssociatedKms(kmsId, tx);
if (!kms) return;
if (kms.isExternal) return;
if (kms.orgId !== orgId) throw new ForbiddenRequestError({ message: "KMS doesn't belong to organization" });
+ if (!kms.isReserved) return;
+ if (kms.orgKms.id === kms.id) return;
return kmsDAL.deleteById(kmsId, tx);
};
diff --git a/backend/src/services/project/project-service.ts b/backend/src/services/project/project-service.ts
index 64291fb8b9..ebd583ff94 100644
--- a/backend/src/services/project/project-service.ts
+++ b/backend/src/services/project/project-service.ts
@@ -305,6 +305,10 @@ export const projectServiceFactory = ({
}
if (kmsKeyId) {
+ if (permission.cannot(OrgPermissionActions.Read, OrgPermissionSubjects.Kms)) {
+ throw new ForbiddenRequestError({ message: "You don't have permission to use this KMS key" });
+ }
+
const kms = await kmsService.getKmsById(kmsKeyId, tx);
if (kms.orgId !== organization.id) {
@@ -312,6 +316,13 @@ export const projectServiceFactory = ({
message: "KMS does not belong in the organization"
});
}
+
+ // an internal key here belongs to another project or to the org
+ if (!kms.isExternal) {
+ throw new BadRequestError({
+ message: "Only an external KMS key can be assigned to a project"
+ });
+ }
}
let projectTemplate: Awaited<ReturnType<typeof projectTemplateService.findProjectTemplateByName>> | null = null;