Skip to content

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

fix(agent-vault): lock referenced variables before personal value rows

Scott Wilsone4a9a909abd9
diff --git a/backend/e2e-test/routes/v1/agent-vault.spec.ts b/backend/e2e-test/routes/v1/agent-vault.spec.ts
index f9cd03f6e4..aa09133db7 100644
--- a/backend/e2e-test/routes/v1/agent-vault.spec.ts
+++ b/backend/e2e-test/routes/v1/agent-vault.spec.ts
@@ -2543,6 +2543,54 @@ describe("Agent Vault V1 Router", async () => {
}
});
+ test("deleting a personal variable while its owner saves values that use it doesn't deadlock", async () => {
+ const bundle = await createAccessBundle("mp-variable-vs-save");
+ const service = await createService(bundle.id, {
+ name: "sentry",
+ hostPattern: "sentry-deadlock.example.com",
+ credential: { type: "bearer", memberProvidedFields: ["credential-value"] }
+ });
+
+ const member = await createMemberIdentity(`mp-variable-vs-save-${Date.now()}`);
+ try {
+ expect((await inject("POST", membersUrl(bundle.id), { machineIdentityIds: [member.id] })).statusCode).toBe(200);
+ const created = await member.as("POST", variablesUrl, { scope: "personal", key: "SENTRY_DSN", value: "s" });
+ expect(created.statusCode).toBe(200);
+ const variableId = (JSON.parse(created.payload) as { variable: { id: string } }).variable.id;
+ expect(
+ (await setValues(member.as, bundle.id, service.id, { "credential-value": "{{SENTRY_DSN}}" })).statusCode
+ ).toBe(200);
+
+ // What a delete does once it finds the service out of the owner's reach: lock the variable, delete their
+ // values there, then the variable. Meanwhile the save replaces the value that holds the reference.
+ const trx = await testDb.transaction();
+ let save: ReturnType<typeof inject> | undefined;
+ try {
+ const { rows } = await trx.raw("select pg_backend_pid() as pid");
+ await trx("agent_vault_variables").where({ id: variableId }).forUpdate().first();
+
+ save = setValues(member.as, bundle.id, service.id, { "credential-value": "token {{SENTRY_DSN}}" });
+ await waitUntilBlockedBy(rows[0].pid);
+ await trx("agent_vault_personal_values").where({ identityId: member.id, serviceId: service.id }).delete();
+ await trx("agent_vault_variables").where({ id: variableId }).delete();
+ await trx.commit();
+
+ const res = await save;
+ expect(res.statusCode).toBe(400);
+ expect(JSON.parse(res.payload).message).toBe(
+ "A variable one of your values uses was deleted while it was being saved. Check its variable references and save again."
+ );
+ } catch (err) {
+ if (!trx.isCompleted()) await trx.rollback();
+ await save?.catch(() => undefined);
+ throw err;
+ }
+ expect(await testDb("agent_vault_personal_values").where({ identityId: member.id })).toHaveLength(0);
+ } finally {
+ await member.cleanup();
+ }
+ });
+
test("until a member sets up their half, resolve sends no credential, not the admin's half", async () => {
const bundle = await createAccessBundle("mp-half");
await createService(bundle.id, {
diff --git a/backend/src/ee/services/agent-vault-personal-value/agent-vault-personal-value-service.ts b/backend/src/ee/services/agent-vault-personal-value/agent-vault-personal-value-service.ts
index d70e30c5ad..2ceab1bbb0 100644
--- a/backend/src/ee/services/agent-vault-personal-value/agent-vault-personal-value-service.ts
+++ b/backend/src/ee/services/agent-vault-personal-value/agent-vault-personal-value-service.ts
@@ -230,6 +230,15 @@ export const agentVaultPersonalValueServiceFactory = ({
};
});
+ // Read off the stored text, the way resolve reads it, never off the request.
+ const referencedByField = new Map<string, string[]>(
+ sealed.map(({ field, stored }) => [
+ field,
+ [...new Set(findStoredVariableIds(stored))].filter((variableId) => variableIds.has(variableId))
+ ])
+ );
+ const referenced = [...new Set([...referencedByField.values()].flat())];
+
const write = () =>
agentVaultPersonalValueDAL.transaction(async (tx) => {
const current = await agentVaultServiceDAL.lockForPersonalValues(service.id, tx);
@@ -255,6 +264,15 @@ export const agentVaultPersonalValueServiceFactory = ({
throw new NotFoundError({ message: `Service with ID '${dto.serviceId}' not found` });
}
+ // Before the values: a variable delete locks the variable and then deletes values.
+ const present = await agentVaultVariableDAL.lockForReference(
+ { projectId: dto.projectId, owner, variableIds: referenced },
+ tx
+ );
+ if (present.length !== referenced.length) {
+ throw new BadRequestError({ message: VARIABLE_DELETED_DURING_SAVE_MESSAGE });
+ }
+
// A field left out keeps its saved value, so a member adds a newly required field without retyping the rest.
// What they send replaces what was saved for those fields, and the reference rows cascade with it.
const existing = await agentVaultPersonalValueDAL.lockForOwnerAndService({ serviceId: service.id, owner }, tx);
@@ -280,24 +298,13 @@ export const agentVaultPersonalValueServiceFactory = ({
tx
);
- // Read off the stored text, the way resolve reads it, never off the request.
- const referenceRows: TAgentVaultPersonalValueReferencesInsert[] = sealed.flatMap(({ field, stored }) => {
- const personalValueId = saved.find((row) => row.field === field)!.id;
- return [...new Set(findStoredVariableIds(stored))]
- .filter((variableId) => variableIds.has(variableId))
- .map((variableId) => ({ personalValueId, variableId }));
- });
- if (referenceRows.length) {
- const referenced = [...new Set(referenceRows.map((row) => row.variableId))];
- const present = await agentVaultVariableDAL.lockForReference(
- { projectId: dto.projectId, owner, variableIds: referenced },
- tx
- );
- if (present.length !== referenced.length) {
- throw new BadRequestError({ message: VARIABLE_DELETED_DURING_SAVE_MESSAGE });
- }
- await agentVaultPersonalValueReferenceDAL.insertMany(referenceRows, tx);
- }
+ const referenceRows: TAgentVaultPersonalValueReferencesInsert[] = saved.flatMap((row) =>
+ (referencedByField.get(row.field) ?? []).map((variableId) => ({
+ personalValueId: row.id,
+ variableId
+ }))
+ );
+ if (referenceRows.length) await agentVaultPersonalValueReferenceDAL.insertMany(referenceRows, tx);
const [projected] = await loadServices(
{ projectId: dto.projectId, owner, accessBundleIds: [service.accessBundleId], serviceId: service.id },
diff --git a/backend/src/ee/services/agent-vault/AGENTS.md b/backend/src/ee/services/agent-vault/AGENTS.md
index be670d0313..f3e9dde214 100644
--- a/backend/src/ee/services/agent-vault/AGENTS.md
+++ b/backend/src/ee/services/agent-vault/AGENTS.md
@@ -305,8 +305,10 @@ reference.
bundles the owner still reaches.
- Personal variables follow the global variables' reference rules: sealed text names them by id, a reference row
per personal value (`agent_vault_personal_value_references`, a deferred foreign key), the delete takes the variable
- row `FOR UPDATE` and a personal values save takes the rows it references `FOR SHARE`. Used By on a variable reads
- both reference tables; any one variable only ever appears in one of them.
+ row `FOR UPDATE` and a personal values save takes the rows it references `FOR SHARE`. The save takes them before
+ its own value rows, because a delete on a bundle the owner no longer reaches goes on to delete values, so the
+ other order deadlocks. Used By on a variable reads both reference tables; any one variable only ever appears in
+ one of them.
- **The API calls them personal values**, the UI's word: a sub-resource of the service
(`PATCH`/`DELETE .../services/:serviceId/personal-values`), always the caller's own, with no collection route.
Reads ride along: the bundle read puts the caller's status on each service (`personalValues`, null when nothing