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
refuse access requests when the policy changed after matching
adilsondfe2ceb9fe2c
diff --git a/backend/src/services/approval-policy/secret-access/secret-access-policy-fns.ts b/backend/src/services/approval-policy/secret-access/secret-access-policy-fns.tsindex a529b5ea1e..38df6ecab2 100644--- a/backend/src/services/approval-policy/secret-access/secret-access-policy-fns.ts+++ b/backend/src/services/approval-policy/secret-access/secret-access-policy-fns.ts@@ -82,11 +82,6 @@ const StoredConstraintsSchema = z.object({constraints: SecretAccessPolicyConstraintsSchema});-export const readSecretAccessPolicyConstraints = (constraints: unknown): TSecretAccessPolicyConstraints | null => {- const parsed = StoredConstraintsSchema.safeParse(constraints);- return parsed.success ? parsed.data.constraints : null;-};-export const getSecretAccessAllowedSelfApprovals = (policy: {allowedSelfApprovals?: boolean;constraints?: unknown;diff --git a/backend/src/services/secret-access-approval-global-request-bridge/secret-access-approval-global-request-bridge-service.test.ts b/backend/src/services/secret-access-approval-global-request-bridge/secret-access-approval-global-request-bridge-service.test.tsindex 7279a0c6c8..24990a8aad 100644--- a/backend/src/services/secret-access-approval-global-request-bridge/secret-access-approval-global-request-bridge-service.test.ts+++ b/backend/src/services/secret-access-approval-global-request-bridge/secret-access-approval-global-request-bridge-service.test.ts@@ -217,6 +217,7 @@ const snapshotPolicy = {id: "policy-1",name: "Prod access",enforcementLevel: "hard",+ updatedAt: new Date("2026-10-28T10:00:00.000Z"),constraints: {version: 1 as const,constraints: {@@ -227,17 +228,8 @@ const snapshotPolicy = {}};-const lockedConstraints = (requestExpirationTime: string | null, maxTimePeriod: string | null) => ({- version: 1 as const,- constraints: {- allowedSelfApprovals: true,- requestExpirationTime,- maxTimePeriod- }-});--const createRequestService = ({ constraints = lockedConstraints("2h", null) } = {}) => {- const findByIdForShare = vi.fn().mockResolvedValue({ id: "policy-1", constraints });+const createRequestService = ({ lockedUpdatedAt = snapshotPolicy.updatedAt } = {}) => {+ const findByIdForShare = vi.fn().mockResolvedValue({ id: "policy-1", updatedAt: lockedUpdatedAt });const create = vi.fn().mockResolvedValue({ id: "req-1" });const createRequest = vi.fn().mockResolvedValue({ id: "delegated-1" });@@ -299,10 +291,12 @@ const submitAccessRequest = (} as Parameters<ReturnType<typeof createRequestService>["service"]["createAccessApprovalRequest"]>[0]);describe("secretAccessApprovalGlobalRequestBridge.createAccessApprovalRequest", () => {- test("creates a delegated request using the expiration on the locked policy", async () => {+ test("creates a delegated request while holding the lock on the unchanged policy", async () => {vi.useFakeTimers();vi.setSystemTime(new Date("2026-10-28T12:00:00.000Z"));- const { service, findByIdForShare, create, createRequest } = createRequestService();+ const { service, findByIdForShare, create, createRequest } = createRequestService({+ lockedUpdatedAt: new Date(snapshotPolicy.updatedAt.getTime())+ });try {await submitAccessRequest(service);@@ -310,7 +304,7 @@ describe("secretAccessApprovalGlobalRequestBridge.createAccessApprovalRequest",expect(findByIdForShare).toHaveBeenCalledWith("policy-1", "tx");expect(findByIdForShare.mock.invocationCallOrder[0]).toBeLessThan(create.mock.invocationCallOrder[0]);expect(create).toHaveBeenCalledWith(- expect.objectContaining({ expiresAt: new Date("2026-10-28T14:00:00.000Z") }),+ expect.objectContaining({ expiresAt: new Date("2026-10-28T13:00:00.000Z") }),"tx");expect(findByIdForShare.mock.invocationCallOrder[0]).toBeLessThan(createRequest.mock.invocationCallOrder[0]);@@ -322,13 +316,13 @@ describe("secretAccessApprovalGlobalRequestBridge.createAccessApprovalRequest",}});- test("refuses when the locked max time period is shorter than the requested range", async () => {+ test("refuses when the policy was updated after the request matched it", async () => {const { service, create, createRequest } = createRequestService({- constraints: lockedConstraints(null, "1h")+ lockedUpdatedAt: new Date("2026-10-28T10:05:00.000Z")});- await expect(submitAccessRequest(service, { isTemporary: true, temporaryRange: "2h" })).rejects.toThrow(- "Requested access time range is limited to 1h by policy"+ await expect(submitAccessRequest(service)).rejects.toThrow(+ "Policy 'Prod access' was changed while the request was being created. Submit the request again.");expect(create).not.toHaveBeenCalled();expect(createRequest).not.toHaveBeenCalled();diff --git a/backend/src/services/secret-access-approval-global-request-bridge/secret-access-approval-global-request-bridge-service.ts b/backend/src/services/secret-access-approval-global-request-bridge/secret-access-approval-global-request-bridge-service.tsindex 576c4e2099..b9d352ce37 100644--- a/backend/src/services/secret-access-approval-global-request-bridge/secret-access-approval-global-request-bridge-service.ts+++ b/backend/src/services/secret-access-approval-global-request-bridge/secret-access-approval-global-request-bridge-service.ts@@ -31,7 +31,6 @@ import {getSecretAccessRequestData,hasSameAccessCriteria,parseSecretAccessRequestData,- readSecretAccessPolicyConstraints,validateSecretAccessConstraints} from "@app/services/approval-policy/secret-access/secret-access-policy-fns";import { TSecretAccessRequestData } from "@app/services/approval-policy/secret-access/secret-access-policy-types";@@ -346,32 +345,24 @@ export const secretAccessApprovalGlobalRequestBridgeServiceFactory = ({throw new BadRequestError({ message: "You already have a pending access request with the same criteria" });}- // A policy update locks this row, then rewrites its time limits, approver steps, and delegated reviewer- // together. Sharing the row waits that update out, so the limits are read from it before a request is- // created against the reviewer the update left in place.+ const { requestExpirationTime } = policy.constraints.constraints;+ const parsedMs = requestExpirationTime ? ms(requestExpirationTime) : null;+ const expiresAt = parsedMs && !Number.isNaN(parsedMs) ? new Date(Date.now() + parsedMs) : null;++ // A policy update locks this row, then rewrites its environments, secret path, time limits, approver+ // steps, and delegated reviewer in the same transaction, which moves updatedAt. Sharing the row waits that+ // update out, and an unchanged updatedAt means the scope and limits this request was checked against still hold.const { approvalRequest: created, policySteps } = await approvalRequestDAL.transaction(async (tx) => {const lockedPolicy = await approvalPolicyDAL.findByIdForShare(policy.id, tx);if (!lockedPolicy) {throw new NotFoundError({ message: `Policy '${policy.name}' was deleted before the request could be created` });}-- const lockedConstraints = readSecretAccessPolicyConstraints(lockedPolicy.constraints);- if (!lockedConstraints) {+ if (new Date(lockedPolicy.updatedAt).getTime() !== new Date(policy.updatedAt).getTime()) {throw new BadRequestError({- message: `Policy '${policy.name}' could not be read. Submit the request again.`+ message: `Policy '${policy.name}' was changed while the request was being created. Submit the request again.`});}- const lockedConstraintValidation = validateSecretAccessConstraints(lockedConstraints, requestData);- if (!lockedConstraintValidation.valid) {- throw new BadRequestError({- message: lockedConstraintValidation.errors?.join("; ") ?? "Policy constraints not met"- });- }-- const parsedMs = lockedConstraints.requestExpirationTime ? ms(lockedConstraints.requestExpirationTime) : null;- const expiresAt = parsedMs && !Number.isNaN(parsedMs) ? new Date(Date.now() + parsedMs) : null;-const currentSteps = await approvalPolicyDAL.findStepsByPolicyId(policy.id, tx);const delegatedPolicy = currentSteps.length? null