Skip to content

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

address check that policy exists in case it was migrated

adilsond015a4fd8528
diff --git a/backend/e2e-test/routes/v1/access-approval-global-policy-routing.spec.ts b/backend/e2e-test/routes/v1/access-approval-global-policy-routing.spec.ts
index 35f97b06e1..1f496d790f 100644
--- a/backend/e2e-test/routes/v1/access-approval-global-policy-routing.spec.ts
+++ b/backend/e2e-test/routes/v1/access-approval-global-policy-routing.spec.ts
@@ -27,12 +27,13 @@ const getSeedEnv = async () => {
// The create route always writes to the global tables now, so a legacy policy
// has to be seeded straight into the legacy tables.
-const createLegacyPolicy = async (dto: { name: string; secretPath: string }) => {
+const createLegacyPolicy = async (dto: { name: string; secretPath: string; id?: string }) => {
const db = getDb();
const env = await getSeedEnv();
const [policy] = await db(TableName.AccessApprovalPolicy)
.insert({
+ id: dto.id,
name: dto.name,
secretPath: dto.secretPath,
envId: env.id,
@@ -250,6 +251,27 @@ describe("Access approval policy routing", () => {
expect(samePath.statusCode).toBe(200);
});
+ test("A migrated policy's legacy row does not block the scope it moved away from", async () => {
+ const migrated = await createGlobalPolicy({
+ name: "policy-routing-migrated",
+ secretPath: "/policy-routing-migrated"
+ });
+ await createLegacyPolicy({
+ id: migrated.id,
+ name: "policy-routing-migrated",
+ secretPath: "/policy-routing-migrated"
+ });
+
+ const moved = await patchPolicy(migrated.id, { secretPath: "/policy-routing-migrated-moved" });
+ expect(moved.statusCode).toBe(200);
+
+ const replacement = await createGlobalPolicy({
+ name: "policy-routing-migrated-replacement",
+ secretPath: "/policy-routing-migrated"
+ });
+ expect(replacement.secretPath).toBe("/policy-routing-migrated");
+ });
+
test("Getting a global policy by id carries approver and bypasser names", async () => {
const policy = await createGlobalPolicy({
name: "policy-routing-get",
diff --git a/backend/e2e-test/routes/v1/access-approval-global-request-routing.spec.ts b/backend/e2e-test/routes/v1/access-approval-global-request-routing.spec.ts
index 2c279ddc78..8c4443fbc1 100644
--- a/backend/e2e-test/routes/v1/access-approval-global-request-routing.spec.ts
+++ b/backend/e2e-test/routes/v1/access-approval-global-request-routing.spec.ts
@@ -18,7 +18,7 @@ const globalPolicyIds: string[] = [];
// The create route always writes to the global tables now, so a legacy policy
// has to be seeded straight into the legacy tables.
-const createLegacyPolicy = async (dto: { name: string; secretPath: string }) => {
+const createLegacyPolicy = async (dto: { name: string; secretPath: string; id?: string }) => {
const db = getDb();
const env = await db(TableName.Environment)
.where({ projectId: seedData1.project.id, slug: seedData1.environment.slug })
@@ -27,6 +27,7 @@ const createLegacyPolicy = async (dto: { name: string; secretPath: string }) =>
const [policy] = await db(TableName.AccessApprovalPolicy)
.insert({
+ id: dto.id,
name: dto.name,
secretPath: dto.secretPath,
envId: env.id,
@@ -278,6 +279,33 @@ describe("Access approval request routing", () => {
}
}, 30_000);
+ test("A migrated policy's legacy row does not take requests for the path it moved away from", async () => {
+ const secretPath = "/routing-migrated";
+ const created = await createGlobalPolicy({ name: "routing-migrated", secretPath });
+ expect(created.statusCode).toBe(200);
+ const policyId: string = created.json().approval.id;
+ await createLegacyPolicy({ id: policyId, name: "routing-migrated", secretPath });
+
+ const moved = await testServer.inject({
+ method: "PATCH",
+ url: `/api/v1/access-approvals/policies/${policyId}`,
+ headers: {
+ authorization: `Bearer ${jwtAuthToken}`
+ },
+ body: {
+ secretPath: "/routing-migrated-moved",
+ approvers: [{ type: ApproverType.User, id: seedData1.id }],
+ approvals: 1
+ }
+ });
+ expect(moved.statusCode).toBe(200);
+
+ const res = await createAccessRequest(secretPath);
+
+ expect(res.statusCode).toBe(404);
+ expect(res.json().message).toContain("No policy in environment");
+ });
+
test("A request with no policy on either system is rejected", async () => {
const res = await createAccessRequest("/routing-none");
diff --git a/backend/src/ee/services/access-approval-policy/access-approval-policy-dal.ts b/backend/src/ee/services/access-approval-policy/access-approval-policy-dal.ts
index 2f65d28fec..d646813e61 100644
--- a/backend/src/ee/services/access-approval-policy/access-approval-policy-dal.ts
+++ b/backend/src/ee/services/access-approval-policy/access-approval-policy-dal.ts
@@ -599,6 +599,12 @@ export const accessApprovalPolicyDALFactory = (db: TDbClient): TAccessApprovalPo
`${TableName.AccessApprovalPolicy}.id`
)
.where(`${TableName.AccessApprovalPolicyEnvironment}.envId`, "=", envId)
+ .whereNotExists((qb) => {
+ void qb
+ .select(db.raw("1"))
+ .from(TableName.ApprovalPolicies)
+ .whereRaw(`"${TableName.ApprovalPolicies}"."id" = "${TableName.AccessApprovalPolicy}"."id"`);
+ })
.orderBy("deletedAt", "desc")
.orderByRaw(`"deletedAt" IS NULL`)
.select(selectAllTableCols(TableName.AccessApprovalPolicy))
@@ -647,6 +653,12 @@ export const accessApprovalPolicyDALFactory = (db: TDbClient): TAccessApprovalPo
)
)
.whereNull(`${TableName.AccessApprovalPolicy}.deletedAt`)
+ .whereNotExists((qb) => {
+ void qb
+ .select(db.raw("1"))
+ .from(TableName.ApprovalPolicies)
+ .whereRaw(`"${TableName.ApprovalPolicies}"."id" = "${TableName.AccessApprovalPolicy}"."id"`);
+ })
.where((qb) => {
if (excludePolicyId) void qb.whereNot(`${TableName.AccessApprovalPolicy}.id`, excludePolicyId);
})