Skip to content

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

fix(agent-vault): write session label edits atomically on the primary

Scott Wilsoned44d90cd4d1
diff --git a/backend/e2e-test/routes/v1/agent-vault.spec.ts b/backend/e2e-test/routes/v1/agent-vault.spec.ts
index b2aefbebc3..27142626e9 100644
--- a/backend/e2e-test/routes/v1/agent-vault.spec.ts
+++ b/backend/e2e-test/routes/v1/agent-vault.spec.ts
@@ -3707,12 +3707,12 @@ describe("Agent Vault V1 Router", async () => {
const rejected = {
tab: "a\tb",
- zeroWidthSpace: "​",
- rightToLeftOverride: "revo ‮ltr",
- lineSeparator: "a
b",
- paragraphSeparator: "a
b",
+ zeroWidthSpace: "\u200b",
+ rightToLeftOverride: "revo \u202eltr",
+ lineSeparator: "a\u2028b",
+ paragraphSeparator: "a\u2029b",
c1Control: "a\u0085b",
- privateUse: "ab",
+ privateUse: "a\ue000b",
loneSurrogate: "a\ud800b"
};
for await (const [name, label] of Object.entries(rejected)) {
@@ -3747,11 +3747,18 @@ describe("Agent Vault V1 Router", async () => {
// The label is the only field, so leaving it out must not read as clearing it.
expect((await inject("PATCH", `/api/v1/agent-vault/sessions/${id}`, {})).statusCode).toBe(422);
- expect((await inject("PATCH", `/api/v1/agent-vault/sessions/${id}`, { label: "a​b" })).statusCode).toBe(
- 422
- );
+ expect((await inject("PATCH", `/api/v1/agent-vault/sessions/${id}`, { label: "a\u200bb" })).statusCode).toBe(422);
expect((await inject("GET", `/api/v1/agent-vault/sessions/${id}`)).json().session.label).toBe("first pass");
+ // Back to a label it held a moment ago: the write must land even though nothing looks changed from the
+ // label two edits back.
+ expect((await inject("PATCH", `/api/v1/agent-vault/sessions/${id}`, { label: "second pass" })).statusCode).toBe(
+ 200
+ );
+ const back = await inject("PATCH", `/api/v1/agent-vault/sessions/${id}`, { label: "first pass" });
+ expect(back.json().session.label).toBe("first pass");
+ expect((await testDb("agent_vault_sessions").where({ id }).first()).label).toBe("first pass");
+
const cleared = await inject("PATCH", `/api/v1/agent-vault/sessions/${id}`, { label: null });
expect(cleared.statusCode).toBe(200);
expect(cleared.json().session.label).toBeNull();
diff --git a/backend/src/ee/services/agent-vault-session/agent-vault-session-dal.ts b/backend/src/ee/services/agent-vault-session/agent-vault-session-dal.ts
index bdfe78ae1e..7e5a32a80c 100644
--- a/backend/src/ee/services/agent-vault-session/agent-vault-session-dal.ts
+++ b/backend/src/ee/services/agent-vault-session/agent-vault-session-dal.ts
@@ -315,6 +315,22 @@ export const agentVaultSessionDALFactory = (db: TDbClient) => {
}
};
+ // One statement on the primary: the row is locked, rewritten, and its label before the write comes back, so
+ // two quick edits can't both read a stale label and the caller audits what actually changed.
+ const updateLabel = async (id: string, projectId: string, label: string | null, tx?: Knex) => {
+ try {
+ const { rows } = (await (tx || db).raw(
+ `WITH previous AS (SELECT id, label FROM ?? WHERE id = ? AND "projectId" = ? FOR UPDATE)
+ UPDATE ?? AS session SET label = ? FROM previous WHERE session.id = previous.id
+ RETURNING previous.label AS "previousLabel"`,
+ [TableName.AgentVaultSession, id, projectId, TableName.AgentVaultSession, label]
+ )) as { rows: { previousLabel: string | null }[] };
+ return rows[0];
+ } catch (error) {
+ throw new DatabaseError({ error, name: "Update agent vault session label" });
+ }
+ };
+
// The id is chosen by the caller because the session log key is wrapped with it before the row exists.
const createWithId = async (data: TAgentVaultSessionsInsert & { id: string }, tx?: Knex) => {
try {
@@ -327,5 +343,5 @@ export const agentVaultSessionDALFactory = (db: TDbClient) => {
}
};
- return { ...orm, findByTokenHash, findForList, revokeIfActive, createWithId };
+ return { ...orm, findByTokenHash, findForList, revokeIfActive, updateLabel, createWithId };
};
diff --git a/backend/src/ee/services/agent-vault-session/agent-vault-session-service.ts b/backend/src/ee/services/agent-vault-session/agent-vault-session-service.ts
index 12352f2831..1642051a61 100644
--- a/backend/src/ee/services/agent-vault-session/agent-vault-session-service.ts
+++ b/backend/src/ee/services/agent-vault-session/agent-vault-session-service.ts
@@ -247,11 +247,11 @@ export const agentVaultSessionServiceFactory = ({
);
const session = await findVisibleSession({ projectId, ctx, sessionId, isAdmin });
- if (session.label !== label) {
- await agentVaultSessionDAL.updateById(session.id, { label });
- }
+ // The visible session can come from a replica that lags, so the previous label is the one the write replaced.
+ const updated = await agentVaultSessionDAL.updateLabel(session.id, projectId, label);
+ if (!updated) throw new NotFoundError({ message: `Session with ID '${sessionId}' not found` });
- return { session: toSessionView({ ...session, label }), previousLabel: session.label };
+ return { session: toSessionView({ ...session, label }), previousLabel: updated.previousLabel };
};
return {