Skip to content

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

check sharing projects with a light query outside a transaction

DimitrisStaratzisc61324581294
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index dbe50e83ca..ac8574ac4c 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -2560,8 +2560,7 @@ export const registerRoutes = async (
projectAccessRequestDAL,
usageMeteringService,
dynamicSecretLeaseDAL,
- dynamicSecretQueueService,
- projectFolderGrantDAL
+ dynamicSecretQueueService
});
const projectEnvQueue = projectEnvQueueFactory({
diff --git a/backend/src/services/project/project-dal.ts b/backend/src/services/project/project-dal.ts
index a1c1352e24..de0b1fa419 100644
--- a/backend/src/services/project/project-dal.ts
+++ b/backend/src/services/project/project-dal.ts
@@ -1087,6 +1087,21 @@ export const projectDALFactory = (db: TDbClient) => {
}
};
+ // Other live projects this project shares folders with. Reads the primary so a grant made just before a
+ // delete is seen.
+ const findProjectsSharedWith = async (projectId: string, tx?: Knex) => {
+ try {
+ const docs = await (tx || db)(TableName.ProjectFolderGrant)
+ .join(TableName.Project, `${TableName.ProjectFolderGrant}.targetProjectId`, `${TableName.Project}.id`)
+ .where(`${TableName.ProjectFolderGrant}.sourceProjectId`, projectId)
+ .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 shared with" });
+ }
+ };
+
const countOfBillableOrgProjects = async (orgId: string | null, tx?: Knex) => {
try {
const subOrgProjects = db.replicaNode()(TableName.Organization).where({ rootOrgId: orgId }).select("id");
@@ -1151,6 +1166,7 @@ export const projectDALFactory = (db: TDbClient) => {
findProjectByEnvId,
findProjectDeletedEnvironments,
findDeletedProjects,
+ findProjectsSharedWith,
countOfBillableOrgProjects
};
};
diff --git a/backend/src/services/project/project-service.ts b/backend/src/services/project/project-service.ts
index a378647a25..8324c23c9c 100644
--- a/backend/src/services/project/project-service.ts
+++ b/backend/src/services/project/project-service.ts
@@ -92,7 +92,6 @@ import { TPkiAlertDALFactory } from "../pki-alert/pki-alert-dal";
import { TPkiCollectionDALFactory } from "../pki-collection/pki-collection-dal";
import { TProjectEnvDALFactory } from "../project-env/project-env-dal";
import { SOFT_DELETE_GRACE_MS } from "../project-env/project-env-queue";
-import { TProjectFolderGrantDALFactory } from "../project-folder-grant/project-folder-grant-dal";
import { TProjectMembershipDALFactory } from "../project-membership/project-membership-dal";
import { getPredefinedRoles } from "../project-role/project-role-fns";
import { TRoleDALFactory } from "../role/role-dal";
@@ -216,7 +215,6 @@ type TProjectServiceFactoryDep = {
TDynamicSecretLeaseQueueServiceFactory,
"setLeaseRevocation" | "unsetRestoredLeaseRevocation"
>;
- projectFolderGrantDAL: Pick<TProjectFolderGrantDALFactory, "listBySourceProject">;
};
export type TProjectServiceFactory = ReturnType<typeof projectServiceFactory>;
@@ -269,8 +267,7 @@ export const projectServiceFactory = ({
projectAccessRequestDAL,
usageMeteringService,
dynamicSecretLeaseDAL,
- dynamicSecretQueueService,
- projectFolderGrantDAL
+ dynamicSecretQueueService
}: TProjectServiceFactoryDep) => {
// The (orgId, slug) unique index already refuses a taken slug; this only names the cause when the holder is
// a project pending deletion.
@@ -897,6 +894,21 @@ export const projectServiceFactory = ({
}
try {
+ // Deleting a project that shares folders with other projects would silently drop those secrets from
+ // their imports, references and syncs, so that access must be revoked first. With sharing off, grants
+ // are ignored.
+ const sharedWith = allowCrossProjectSecretSharing ? await projectDAL.findProjectsSharedWith(project.id) : [];
+ if (sharedWith.length) {
+ const preview = sharedWith
+ .slice(0, 5)
+ .map(({ name }) => `'${name}'`)
+ .join(", ");
+ const remaining = sharedWith.length - 5;
+ throw new BadRequestError({
+ message: `Project '${project.name}' can't be deleted while it shares secrets with other projects: ${remaining > 0 ? `${preview}, and ${remaining} more` : preview}. Revoke their access in Project Settings → Cross-Project Secret Sharing first, then retry.`
+ });
+ }
+
// This returns immediately (one UPDATE) and the project disappears from every read, but the
// expensive cascade runs asynchronously in the hard-delete worker once deleteAfter passes. Only
// secret manager projects get a restore grace period; the rest (and hard deletes) are eligible on
@@ -904,36 +916,12 @@ export const projectServiceFactory = ({
// unchanged; otherwise the slug is freed so a same-named project can be recreated right away.
const now = new Date();
const hasGracePeriod = !hardDelete && project.type === ProjectType.SecretManager;
- const softDeletedProject = await projectDAL.transaction(async (tx) => {
- // Deleting a project that shares folders with other projects would silently drop those secrets from
- // their imports, references and syncs, so that access must be revoked first. Read under the lock from
- // the primary so a grant made just before the delete is seen. With sharing off, grants are ignored.
- const grants = allowCrossProjectSecretSharing
- ? await projectFolderGrantDAL.listBySourceProject(project.id, tx)
- : [];
- const sharedWith = [...new Set(grants.map(({ targetProjectName }) => targetProjectName))];
- if (sharedWith.length) {
- const preview = sharedWith
- .slice(0, 5)
- .map((name) => `'${name}'`)
- .join(", ");
- const remaining = sharedWith.length - 5;
- throw new BadRequestError({
- message: `Project '${project.name}' can't be deleted while it shares secrets with other projects: ${remaining > 0 ? `${preview}, and ${remaining} more` : preview}. Revoke their access in Project Settings → Cross-Project Secret Sharing first, then retry.`
- });
- }
-
- return projectDAL.softDeleteById(
- project.id,
- {
- deleteAfter: hasGracePeriod ? new Date(now.getTime() + SOFT_DELETE_GRACE_MS) : now,
- softDeletedAt: now,
- deletedByActorType: actor,
- deletedByActorId: actorId,
- slug: hasGracePeriod ? project.slug : `del-${alphaNumericNanoId(20)}`
- },
- tx
- );
+ const softDeletedProject = await projectDAL.softDeleteById(project.id, {
+ deleteAfter: hasGracePeriod ? new Date(now.getTime() + SOFT_DELETE_GRACE_MS) : now,
+ softDeletedAt: now,
+ deletedByActorType: actor,
+ deletedByActorId: actorId,
+ slug: hasGracePeriod ? project.slug : `del-${alphaNumericNanoId(20)}`
});
if (!softDeletedProject) {