Skip to content

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

prevent re-use by using a stable ID

Andrey123c4e931e73
diff --git a/backend/src/services/approval-policy/approval-policy-service.ts b/backend/src/services/approval-policy/approval-policy-service.ts
index 63444d74c4..874b9138fc 100644
--- a/backend/src/services/approval-policy/approval-policy-service.ts
+++ b/backend/src/services/approval-policy/approval-policy-service.ts
@@ -90,9 +90,7 @@ import { TAutoApprovalServiceFactory } from "./auto-approval/auto-approval-servi
import {
AUTO_APPROVAL_UNUSED_CONSTRAINT,
TAutoApprovalConfig,
- TAutoApprovalEvidence,
- TParsedAutoApprovalInput,
- TStoredAutoApproval
+ TAutoApprovalEvidence
} from "./auto-approval/auto-approval-types";
type TApprovalPolicyServiceFactoryDep = {
@@ -1120,35 +1118,6 @@ export const approvalPolicyServiceFactory = ({
};
// Inputs a rule requires to be unused whose record an earlier approved request on this policy already linked
- const $findUsedInputIds = async (
- policyId: string,
- stored: TStoredAutoApproval,
- parsedInputs: TParsedAutoApprovalInput[]
- ) => {
- const checkedInputIds = new Set(
- stored.rules.flatMap((rule) =>
- rule.conditions
- .filter((condition) => condition.constraint === AUTO_APPROVAL_UNUSED_CONSTRAINT && condition.inputId)
- .map((condition) => condition.inputId as string)
- )
- );
- const used = await Promise.all(
- parsedInputs
- .filter((input) => checkedInputIds.has(input.inputId))
- .map(async (input) =>
- (await approvalRequestDAL.hasApprovedRequestLinkingRecord({
- policyId,
- connectionId: input.connectionId,
- subject: input.subject,
- recordId: input.id
- }))
- ? input.inputId
- : null
- )
- );
- return new Set(used.filter((inputId): inputId is string => Boolean(inputId)));
- };
-
// Two requests can pass the reuse check at once, so the approving one locks and rechecks its records
const $claimUnusedRecords = async (policyId: string, evidence: TAutoApprovalEvidence, tx: Knex) => {
const rule = evidence.rules.find((candidate) => candidate.id === evidence.approvedByRuleId);
@@ -1162,13 +1131,15 @@ export const approvalPolicyServiceFactory = ({
);
if (!claimed.length) return;
- const keys = claimed.map((input) => `auto-approval:${policyId}:${input.connectionId}:${input.subject}:${input.id}`);
+ const keys = claimed.map(
+ (input) => `auto-approval:${policyId}:${input.connectionId}:${input.subject}:${input.stableId}`
+ );
await tx.raw("SELECT pg_advisory_xact_lock(hashtext(key)) FROM unnest(?::text[]) AS key ORDER BY key", [keys]);
const used = await Promise.all(
claimed.map(async (input) =>
(await approvalRequestDAL.hasApprovedRequestLinkingRecord(
- { policyId, connectionId: input.connectionId, subject: input.subject, recordId: input.id },
+ { policyId, connectionId: input.connectionId, subject: input.subject, stableId: input.stableId },
tx
))
? input
@@ -1363,7 +1334,8 @@ export const approvalPolicyServiceFactory = ({
autoApprovalEvidence = await autoApprovalService.evaluate({
stored: storedAutoApproval,
parsedInputs,
- usedInputIds: await $findUsedInputIds(policy.id, storedAutoApproval, parsedInputs),
+ isRecordUsed: (record) =>
+ approvalRequestDAL.hasApprovedRequestLinkingRecord({ policyId: policy.id, ...record }),
orgId: actor.orgId,
projectId,
ctx: { requester: { email: requesterEmail || null } }
diff --git a/backend/src/services/approval-policy/approval-request-dal.ts b/backend/src/services/approval-policy/approval-request-dal.ts
index ec0ca64d0a..38713a7a6e 100644
--- a/backend/src/services/approval-policy/approval-request-dal.ts
+++ b/backend/src/services/approval-policy/approval-request-dal.ts
@@ -84,15 +84,15 @@ export const approvalRequestDALFactory = (db: TDbClient) => {
policyId,
connectionId,
subject,
- recordId
- }: { policyId: string; connectionId: string; subject: string; recordId: string },
+ stableId
+ }: { policyId: string; connectionId: string; subject: string; stableId: string },
tx?: Knex
) => {
try {
const row = await (tx || db.replicaNode())(TableName.ApprovalRequests)
.where({ policyId, status: ApprovalRequestStatus.Approved })
.whereRaw(`"autoApprovalEvidence" -> 'inputs' @> ?::jsonb`, [
- JSON.stringify([{ connectionId, subject, id: recordId }])
+ JSON.stringify([{ connectionId, subject, stableId }])
])
.first("id");
return Boolean(row);
diff --git a/backend/src/services/approval-policy/auto-approval/auto-approval-fns.test.ts b/backend/src/services/approval-policy/auto-approval/auto-approval-fns.test.ts
index a705a581fc..8e8b4afa89 100644
--- a/backend/src/services/approval-policy/auto-approval/auto-approval-fns.test.ts
+++ b/backend/src/services/approval-policy/auto-approval/auto-approval-fns.test.ts
@@ -153,7 +153,8 @@ describe("toAutoApprovalOutcome", () => {
label: "Jira ticket",
value: "https://acme.atlassian.net/browse/PAY-123",
id: "PAY-123",
- url: "https://acme.atlassian.net/browse/PAY-123"
+ url: "https://acme.atlassian.net/browse/PAY-123",
+ stableId: "10001"
}
],
subjects: [
diff --git a/backend/src/services/approval-policy/auto-approval/auto-approval-fns.ts b/backend/src/services/approval-policy/auto-approval/auto-approval-fns.ts
index 4bddd3026e..b2062d89a8 100644
--- a/backend/src/services/approval-policy/auto-approval/auto-approval-fns.ts
+++ b/backend/src/services/approval-policy/auto-approval/auto-approval-fns.ts
@@ -24,6 +24,7 @@ export type TResolvedAutoApprovalSubject = {
facts: unknown;
error: string | null;
recordId?: string | null;
+ stableId?: string | null;
previouslyUsed?: boolean;
};
diff --git a/backend/src/services/approval-policy/auto-approval/auto-approval-service.ts b/backend/src/services/approval-policy/auto-approval/auto-approval-service.ts
index 9b180cc8cd..bd6a21f71e 100644
--- a/backend/src/services/approval-policy/auto-approval/auto-approval-service.ts
+++ b/backend/src/services/approval-policy/auto-approval/auto-approval-service.ts
@@ -325,15 +325,15 @@ export const autoApprovalServiceFactory = ({
const evaluate = async ({
stored,
parsedInputs,
- usedInputIds,
+ isRecordUsed,
orgId,
projectId,
ctx
}: {
stored: TStoredAutoApproval;
parsedInputs: TParsedAutoApprovalInput[];
- // Inputs whose linked record an earlier approved request on the policy already used
- usedInputIds: Set<string>;
+ // Whether an earlier approved request on the policy already linked this record
+ isRecordUsed: (record: { connectionId: string; subject: string; stableId: string }) => Promise<boolean>;
orgId: string;
projectId: string;
ctx: TAutoApprovalContext;
@@ -407,8 +407,9 @@ export const autoApprovalServiceFactory = ({
];
}
+ let facts: unknown;
try {
- const facts: unknown = await withTimeout(
+ facts = await withTimeout(
(signal) =>
definition.resolve(connection, parsedInput?.id ?? null, ctx, [...target.constraintIds], {
...providerDeps,
@@ -417,18 +418,6 @@ export const autoApprovalServiceFactory = ({
RESOLVE_TIMEOUT_MS,
`${getAppName(connection.app)} didn't respond in time`
);
- return [
- key,
- {
- ...target,
- label,
- definition,
- facts,
- error: null,
- recordId: parsedInput?.id ?? null,
- previouslyUsed: Boolean(target.inputId && usedInputIds.has(target.inputId))
- }
- ];
} catch (error) {
if (!(error instanceof BadRequestError)) {
logger.error(error, `Auto-approval subject resolution failed [connectionId=${target.connectionId}]`);
@@ -444,18 +433,41 @@ export const autoApprovalServiceFactory = ({
}
];
}
+
+ const stableId = parsedInput ? (definition.getStableId?.(facts) ?? parsedInput.id) : null;
+ const previouslyUsed = Boolean(
+ stableId &&
+ target.constraintIds.has(AUTO_APPROVAL_UNUSED_CONSTRAINT) &&
+ (await isRecordUsed({ connectionId: target.connectionId, subject: target.subject, stableId }))
+ );
+ return [
+ key,
+ {
+ ...target,
+ label,
+ definition,
+ facts,
+ error: null,
+ recordId: parsedInput?.id ?? null,
+ stableId,
+ previouslyUsed
+ }
+ ];
}
)
);
const resolved = new Map(resolvedEntries);
+ const stableIdByInputId = new Map(
+ [...resolved.values()].flatMap(({ inputId, stableId }) => (inputId && stableId ? [[inputId, stableId]] : []))
+ );
const { rules, approvedByRuleId } = evaluateAutoApprovalRules(stored.rules, resolved, ctx);
return {
version: 1,
evaluatedAt: new Date().toISOString(),
approvedByRuleId,
- inputs: parsedInputs,
+ inputs: parsedInputs.map((input) => ({ ...input, stableId: stableIdByInputId.get(input.inputId) ?? input.id })),
subjects: [...resolved.values()].map(({ connectionId, subject, inputId, facts, error }) => ({
connectionId,
subject,
diff --git a/backend/src/services/approval-policy/auto-approval/auto-approval-types.ts b/backend/src/services/approval-policy/auto-approval/auto-approval-types.ts
index 2f21f0b8bf..db071d4a73 100644
--- a/backend/src/services/approval-policy/auto-approval/auto-approval-types.ts
+++ b/backend/src/services/approval-policy/auto-approval/auto-approval-types.ts
@@ -65,6 +65,8 @@ export type TAutoApprovalSubject<TConn, TFacts> = {
deps: TAutoApprovalProviderDeps
) => Promise<TFacts>;
constraints: Record<string, TAutoApprovalConstraint<TConn, TFacts>>;
+ // An ID that survives renames and moves, so linking the same record under another name still counts as reuse
+ getStableId?: (facts: TFacts) => string | null;
};
export type TAutoApprovalProvider<TConn = unknown> = {
@@ -120,7 +122,7 @@ export type TAutoApprovalEvidence = {
version: 1;
evaluatedAt: string;
approvedByRuleId: string | null;
- inputs: TParsedAutoApprovalInput[];
+ inputs: (TParsedAutoApprovalInput & { stableId: string })[];
subjects: {
connectionId: string;
subject: string;
diff --git a/backend/src/services/approval-policy/auto-approval/providers/jira-auto-approval-provider.test.ts b/backend/src/services/approval-policy/auto-approval/providers/jira-auto-approval-provider.test.ts
index 267ee21003..9bc104fd5e 100644
--- a/backend/src/services/approval-policy/auto-approval/providers/jira-auto-approval-provider.test.ts
+++ b/backend/src/services/approval-policy/auto-approval/providers/jira-auto-approval-provider.test.ts
@@ -32,6 +32,7 @@ const ctx: TAutoApprovalContext = {
};
const facts = (overrides: Partial<TJiraTicketFacts> = {}): TJiraTicketFacts => ({
+ issueId: "10001",
key: "PAY-123",
status: "Approved",
projectKey: "PAY",
@@ -209,6 +210,13 @@ describe("resolving a Jira ticket", () => {
expect((await assigneeOf(null))?.reason).toBe("PAY-123 isn't assigned to anyone");
});
+ test("identifies a ticket by Jira's issue id, which survives a move to another key", async () => {
+ vi.mocked(getJiraIssue).mockResolvedValue({ ...issue(), key: "OPS-9" });
+ const { getStableId } = JIRA_AUTO_APPROVAL_PROVIDER.subjects[JIRA_SUBMITTED_TICKET_SUBJECT];
+
+ expect(getStableId?.(await resolveTicket([]))).toBe("10001");
+ });
+
test("only reads approvals when a rule checks them", async () => {
const ticket = await resolveTicket([JiraTicketConstraint.Status]);
diff --git a/backend/src/services/approval-policy/auto-approval/providers/jira-auto-approval-provider.ts b/backend/src/services/approval-policy/auto-approval/providers/jira-auto-approval-provider.ts
index cefd8cad6b..71aedc3d2d 100644
--- a/backend/src/services/approval-policy/auto-approval/providers/jira-auto-approval-provider.ts
+++ b/backend/src/services/approval-policy/auto-approval/providers/jira-auto-approval-provider.ts
@@ -38,6 +38,8 @@ export enum JiraTicketConstraint {
}
export type TJiraTicketFacts = {
+ // Stays the same when the ticket moves to another project and gets a new key
+ issueId: string | null;
key: string;
status: string | null;
projectKey: string | null;
@@ -212,6 +214,7 @@ export const JIRA_AUTO_APPROVAL_PROVIDER: TAutoApprovalProvider<TJiraConnection>
]);
return {
+ issueId: issue.id || null,
key,
status: issue.fields?.status?.name ?? null,
projectKey: issue.fields?.project?.key ?? null,
@@ -221,6 +224,7 @@ export const JIRA_AUTO_APPROVAL_PROVIDER: TAutoApprovalProvider<TJiraConnection>
approval
};
},
+ getStableId: (facts: TJiraTicketFacts) => facts.issueId,
constraints: {
[JiraTicketConstraint.AssignedToRequester]: {
label: "is assigned to the requester",