Skip to content

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

address reviews

Andrey502d94e21fe7
diff --git a/backend/src/db/migrations/20260928120000_approval-policies-unique-pam-folder-scope.ts b/backend/src/db/migrations/20260928120000_approval-policies-unique-pam-folder-scope.ts
new file mode 100644
index 0000000000..8ba46f5cc2
--- /dev/null
+++ b/backend/src/db/migrations/20260928120000_approval-policies-unique-pam-folder-scope.ts
@@ -0,0 +1,39 @@
+import { Knex } from "knex";
+
+import { TableName } from "../schemas";
+
+const INDEX_NAME = "approval_policies_pam_access_scope_unique";
+const PAM_ACCESS = "pam-access";
+
+export async function up(knex: Knex): Promise<void> {
+ const duplicates = await knex(TableName.ApprovalPolicies)
+ .where("type", PAM_ACCESS)
+ .whereNotNull("scopeId")
+ .select("id", "scopeType", "scopeId", "createdAt")
+ .orderBy([{ column: "scopeId" }, { column: "createdAt" }]);
+
+ const seen = new Set<string>();
+ const staleIds: string[] = [];
+ for (const policy of duplicates) {
+ const key = `${policy.scopeType as string}:${policy.scopeId as string}`;
+ if (seen.has(key)) staleIds.push(policy.id);
+ else seen.add(key);
+ }
+
+ if (staleIds.length > 0) {
+ await knex(TableName.ApprovalRequests)
+ .whereIn("policyId", staleIds)
+ .where("status", "pending")
+ .update({ status: "cancelled" });
+ await knex(TableName.ApprovalPolicies).whereIn("id", staleIds).delete();
+ }
+
+ await knex.raw(
+ `CREATE UNIQUE INDEX IF NOT EXISTS ?? ON ?? ("scopeType", "scopeId") WHERE type = ? AND "scopeId" IS NOT NULL`,
+ [INDEX_NAME, TableName.ApprovalPolicies, PAM_ACCESS]
+ );
+}
+
+export async function down(knex: Knex): Promise<void> {
+ await knex.raw(`DROP INDEX IF EXISTS ??`, [INDEX_NAME]);
+}
diff --git a/backend/src/services/approval-policy/approval-policy-service.ts b/backend/src/services/approval-policy/approval-policy-service.ts
index fa60c269a3..0f5b664102 100644
--- a/backend/src/services/approval-policy/approval-policy-service.ts
+++ b/backend/src/services/approval-policy/approval-policy-service.ts
@@ -24,7 +24,7 @@ import {
ResourcePermissionApprovalPolicyActions,
ResourcePermissionSub
} from "@app/ee/services/permission/resource-permission";
-import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
+import { BadRequestError, DatabaseError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
import { ms } from "@app/lib/ms";
import { ActorType } from "@app/services/auth/auth-type";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
@@ -617,7 +617,8 @@ export const approvalPolicyServiceFactory = ({
});
}
- if (resources[policyType]?.singlePolicyPerScope && dbScopeId) {
+ const onePolicyPerScope = Boolean(resources[policyType]?.singlePolicyPerScope && dbScopeId);
+ if (onePolicyPerScope) {
const existing = await approvalPolicyDAL.findOne({
type: policyType,
scopeType: dbScopeType,
@@ -640,7 +641,7 @@ export const approvalPolicyServiceFactory = ({
bypassers: bypassers ?? []
});
- const policy = await approvalPolicyDAL.transaction(async (tx) => {
+ const writePolicy = async (tx: Knex) => {
const newPolicy = await approvalPolicyDAL.create(
{
projectId,
@@ -705,7 +706,18 @@ export const approvalPolicyServiceFactory = ({
}
return newPolicy;
- });
+ };
+
+ const policy = await (onePolicyPerScope
+ ? approvalPolicyDAL.transaction(writePolicy).catch((err) => {
+ if (err instanceof DatabaseError && (err.error as { code?: string })?.code === "23505") {
+ throw new BadRequestError({
+ message: "An approval policy already governs this scope. Update it instead of creating another."
+ });
+ }
+ throw err;
+ })
+ : approvalPolicyDAL.transaction(writePolicy));
return {
policy: { ...policy, steps, bypassers: bypassers ?? [] }
@@ -926,7 +938,15 @@ export const approvalPolicyServiceFactory = ({
policy.type as ApprovalPolicyType
);
- await approvalPolicyDAL.deleteById(policyId);
+ await approvalPolicyDAL.transaction(async (tx) => {
+ await approvalRequestDAL.update(
+ { policyId, status: ApprovalRequestStatus.Pending },
+ { status: ApprovalRequestStatus.Cancelled },
+ tx
+ );
+
+ await approvalPolicyDAL.deleteById(policyId, tx);
+ });
return {
policyId,
@@ -1050,7 +1070,7 @@ export const approvalPolicyServiceFactory = ({
const errorMessage = constraintValidation.errors
? `Policy constraints not met: ${constraintValidation.errors.join("; ")}`
: "Policy constraints not met";
- throw new ForbiddenRequestError({ message: errorMessage });
+ throw new BadRequestError({ message: errorMessage });
}
if (assertDomainCanCreateRequest) {