Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
block deleting a project that other projects import from
DimitrisStaratzisc079f3fb254e
diff --git a/backend/e2e-test/routes/v1/project.spec.ts b/backend/e2e-test/routes/v1/project.spec.tsindex 69ed32d4b7..95ed586f22 100644--- a/backend/e2e-test/routes/v1/project.spec.ts+++ b/backend/e2e-test/routes/v1/project.spec.ts@@ -180,6 +180,57 @@ describe("Project deletion (soft-delete + async cleanup)", async () => {expect((await permanentlyDeleteProject(project.id)).statusCode).toBe(200);});+ test("refuses to delete a project that another project imports secrets from", async () => {+ const orgId = seedData1.organization.id;+ const headers = { authorization: `Bearer ${jwtAuthToken}` };+ const restoreLicense = overrideLicenseFeatures({ crossProjectSecretSharing: true });+ const org = await testDb(TableName.Organization).where({ id: orgId }).first();+ await testDb(TableName.Organization).where({ id: orgId }).update({ allowCrossProjectSecretSharing: true });+ try {+ const source = await createProject("e2e-import-source");+ const importer = await createProject("e2e-import-target");+ const grantRes = await testServer.inject({+ method: "POST",+ url: "/api/v1/project-folder-grants",+ headers,+ body: { sourceProjectId: source.id, environment: "dev", secretPath: "/", targetProjectId: importer.id }+ });+ expect(grantRes.statusCode).toBe(200);+ const importRes = await testServer.inject({+ method: "POST",+ url: "/api/v2/secret-imports",+ headers,+ body: {+ projectId: importer.id,+ environment: "dev",+ path: "/",+ import: { sourceProjectId: source.id, environment: "dev", path: "/" }+ }+ });+ expect(importRes.statusCode).toBe(200);++ const blockedRes = await deleteProject(source.id);+ expect(blockedRes.statusCode).toBe(400);+ expect(JSON.parse(blockedRes.payload).message).toContain(importer.name);++ const removeRes = await testServer.inject({+ method: "DELETE",+ url: `/api/v2/secret-imports/${importRes.json().secretImport.id}`,+ headers,+ body: { projectId: importer.id, environment: "dev", path: "/" }+ });+ expect(removeRes.statusCode).toBe(200);+ expect((await deleteProject(source.id, true)).statusCode).toBe(200);++ await deleteProject(importer.id, true);+ } finally {+ await testDb(TableName.Organization)+ .where({ id: orgId })+ .update({ allowCrossProjectSecretSharing: Boolean(org?.allowCrossProjectSecretSharing) });+ restoreLicense();+ }+ });+test("refuses to restore a project when the plan's project limit is reached", async () => {const project = await createProject("e2e-restore-limit");expect((await deleteProject(project.id)).statusCode).toBe(200);diff --git a/backend/src/services/project/project-dal.ts b/backend/src/services/project/project-dal.tsindex a1c1352e24..de6d622d95 100644--- a/backend/src/services/project/project-dal.ts+++ b/backend/src/services/project/project-dal.ts@@ -1087,6 +1087,29 @@ export const projectDALFactory = (db: TDbClient) => {}};+ // Other live projects with a secret import pointing at one of this project's live environments.+ const findProjectsImportingFrom = async (projectId: string, tx?: Knex) => {+ try {+ const sourceEnvIds = db+ .replicaNode()(TableName.Environment)+ .where({ projectId })+ .whereNull("deleteAfter")+ .select("id");+ const docs = await (tx || db.replicaNode())(TableName.SecretImport)+ .join(TableName.SecretFolder, `${TableName.SecretImport}.folderId`, `${TableName.SecretFolder}.id`)+ .join(TableName.Environment, `${TableName.SecretFolder}.envId`, `${TableName.Environment}.id`)+ .join(TableName.Project, `${TableName.Environment}.projectId`, `${TableName.Project}.id`)+ .whereIn(`${TableName.SecretImport}.importEnv`, sourceEnvIds)+ .whereNot(`${TableName.Project}.id`, projectId)+ .whereNull(`${TableName.Environment}.deleteAfter`)+ .whereNull(`${TableName.Project}.deleteAfter`)+ .distinct(db.ref("id").withSchema(TableName.Project), db.ref("name").withSchema(TableName.Project));+ return docs as { id: string; name: string }[];+ } catch (error) {+ throw new DatabaseError({ error, name: "Find projects importing from project" });+ }+ };+const countOfBillableOrgProjects = async (orgId: string | null, tx?: Knex) => {try {const subOrgProjects = db.replicaNode()(TableName.Organization).where({ rootOrgId: orgId }).select("id");@@ -1151,6 +1174,7 @@ export const projectDALFactory = (db: TDbClient) => {findProjectByEnvId,findProjectDeletedEnvironments,findDeletedProjects,+ findProjectsImportingFrom,countOfBillableOrgProjects};};diff --git a/backend/src/services/project/project-service.ts b/backend/src/services/project/project-service.tsindex b60be5c422..e18a3f444f 100644--- a/backend/src/services/project/project-service.ts+++ b/backend/src/services/project/project-service.ts@@ -868,6 +868,15 @@ export const projectServiceFactory = ({}}+ // Deleting a project that others import from would silently drop those secrets from their reads and+ // syncs, and the deleter may not be a member of those projects, so the imports must be removed first.+ const importingProjects = await projectDAL.findProjectsImportingFrom(project.id);+ if (importingProjects.length) {+ throw new BadRequestError({+ message: `Project '${project.name}' can't be deleted while other projects import secrets from it: ${importingProjects.map(({ name }) => `'${name}'`).join(", ")}. Remove those secret imports first, then retry.`+ });+ }+let lock: Awaited<ReturnType<typeof keyStore.acquireLock>> | undefined;try {lock = await keyStore.acquireLock([KeyStorePrefixes.ProjectDeleteLock(project.id)], 30_000, {