Skip to content

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

share the reserved slug check and skip it when not needed

DimitrisStaratzisbf67eaf5731e
diff --git a/backend/e2e-test/routes/v1/project.spec.ts b/backend/e2e-test/routes/v1/project.spec.ts
index 95ed586f22..e0fd764aaa 100644
--- a/backend/e2e-test/routes/v1/project.spec.ts
+++ b/backend/e2e-test/routes/v1/project.spec.ts
@@ -154,6 +154,12 @@ describe("Project deletion (soft-delete + async cleanup)", async () => {
expect(blockedRes.statusCode).toBe(400);
expect(JSON.parse(blockedRes.payload).message).toContain("pending-deletion");
+ const other = await createProject("e2e-slug-reserved-other");
+ const renameRes = await updateProject(other.id, { slug });
+ expect(renameRes.statusCode).toBe(400);
+ expect(JSON.parse(renameRes.payload).message).toContain("pending-deletion");
+ await deleteProject(other.id, true);
+
expect((await permanentlyDeleteProject(first.id)).statusCode).toBe(200);
expect((await listDeletedProjects()).some((p) => p.id === first.id)).toBe(false);
diff --git a/backend/src/services/project/project-service.ts b/backend/src/services/project/project-service.ts
index cfe2978aed..b5ede62497 100644
--- a/backend/src/services/project/project-service.ts
+++ b/backend/src/services/project/project-service.ts
@@ -1,6 +1,7 @@
import { createMongoAbility, ForbiddenError, MongoAbility, RawRuleOf, subject } from "@casl/ability";
import { PackRule, unpackRules } from "@casl/ability/extra";
import slugify from "@sindresorhus/slugify";
+import { Knex } from "knex";
import {
AccessScope,
@@ -268,6 +269,17 @@ export const projectServiceFactory = ({
dynamicSecretLeaseDAL,
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.
+ const $throwIfSlugHeldByDeletedProject = async (orgId: string, slug: string, tx?: Knex) => {
+ const [slugHolder] = await projectDAL.findIncludingExpired({ orgId, slug }, { tx });
+ if (slugHolder?.deleteAfter) {
+ throw new BadRequestError({
+ message: `Project slug '${slug}' is held by a pending-deletion project. Permanently delete that project first, then retry.`
+ });
+ }
+ };
+
/*
* Create workspace. Make user the admin
* */
@@ -377,12 +389,9 @@ export const projectServiceFactory = ({
const slug = projectSlug || slugify(`${workspaceName}-${alphaNumericNanoId(4)}`);
- const [slugHolder] = await projectDAL.findIncludingExpired({ orgId: organization.id, slug }, { tx });
- if (slugHolder?.deleteAfter) {
- throw new BadRequestError({
- message: `Project slug '${slug}' is held by a pending-deletion project. Permanently delete that project first, then retry.`
- });
- }
+ // Checked up front because a failed insert aborts this transaction. Generated slugs carry a random suffix,
+ // so only a chosen one is worth the lookup.
+ if (projectSlug) await $throwIfSlugHeldByDeletedProject(organization.id, slug, tx);
let project: TProjects;
try {
@@ -1237,15 +1246,6 @@ export const projectServiceFactory = ({
}
}
- if (update.slug) {
- const [slugHolder] = await projectDAL.findIncludingExpired({ orgId: project.orgId, slug: update.slug });
- if (slugHolder?.deleteAfter) {
- throw new BadRequestError({
- message: `Project slug '${update.slug}' is held by a pending-deletion project. Permanently delete that project first, then retry.`
- });
- }
- }
-
try {
const updatedProject = await projectDAL.updateById(project.id, {
name: update.name,
@@ -1268,6 +1268,7 @@ export const projectServiceFactory = ({
if (err instanceof DatabaseError) {
const code = (err.error as { code?: string })?.code;
if (code === DatabaseErrorCode.UniqueViolation) {
+ if (update.slug) await $throwIfSlugHeldByDeletedProject(project.orgId, update.slug);
throw new BadRequestError({
message: `Failed to update project. A project with the slug "${update.slug}" already exists in your organization. Please choose a different slug.`
});