Skip to content

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

fix(agent-vault): stamp revokedAt when a session loses its owner

Scott Wilson11effd477115
diff --git a/backend/e2e-test/routes/v1/agent-vault-session-logs.spec.ts b/backend/e2e-test/routes/v1/agent-vault-session-logs.spec.ts
index 802a0e18e8..31f1acbbdf 100644
--- a/backend/e2e-test/routes/v1/agent-vault-session-logs.spec.ts
+++ b/backend/e2e-test/routes/v1/agent-vault-session-logs.spec.ts
@@ -759,6 +759,38 @@ describe("Agent Vault session logs", async () => {
expect(res.statusCode).toBe(401);
});
+ test("dates a session's retirement from its owner's deletion, not from a later edit", async () => {
+ await configure();
+ const bundle = await createAccessBundle(`session-logs-ownerless-${Date.now()}`);
+ const member = await createMemberIdentity(`session-logs-ownerless-${Date.now()}`);
+ const granted = await inject("POST", `/api/v1/agent-vault/access-bundles/${bundle.id}/members`, {
+ machineIdentityIds: [member.id]
+ });
+ expect(granted.statusCode).toBe(200);
+ const minted = await member.as("POST", "/api/v1/agent-vault/sessions", {
+ accessBundles: [bundle.name],
+ ttl: "1h"
+ });
+ expect(minted.statusCode, minted.payload).toBe(200);
+ const { id: sessionId } = minted.json().session as { id: string };
+ const proxy = await createProxy(`session-logs-ownerless-${Date.now()}`);
+
+ expect((await member.cleanup()).statusCode).toBe(200);
+ const ownerless = await testDb("agent_vault_sessions").where({ id: sessionId }).first();
+ expect(ownerless.identityId).toBeNull();
+ expect(ownerless.revokedAt).not.toBeNull();
+
+ // Owner deleted two days ago, then the session relabelled just now.
+ await testDb("agent_vault_sessions")
+ .where({ id: sessionId })
+ .update({ revokedAt: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000) });
+ const relabelled = await inject("PATCH", `/api/v1/agent-vault/sessions/${sessionId}`, { label: "afterwards" });
+ expect(relabelled.statusCode, relabelled.payload).toBe(200);
+
+ const res = await proxy.postChunk(sessionId, chunkBody());
+ expect(res.statusCode).toBe(401);
+ });
+
test("still accepts a chunk from a session revoked minutes ago", async () => {
await configure();
const { session, proxy } = await setup("grace");
diff --git a/backend/src/db/migrations/20261027190000_agent-vault-session-owner-removed-revoked-at.ts b/backend/src/db/migrations/20261027190000_agent-vault-session-owner-removed-revoked-at.ts
new file mode 100644
index 0000000000..ec322e7377
--- /dev/null
+++ b/backend/src/db/migrations/20261027190000_agent-vault-session-owner-removed-revoked-at.ts
@@ -0,0 +1,45 @@
+import { Knex } from "knex";
+
+import { TableName } from "../schemas";
+
+const FUNCTION_NAME = "agent_vault_session_revoke_on_owner_removed";
+const TRIGGER_NAME = "agent_vault_sessions_revoke_on_owner_removed";
+
+export async function up(knex: Knex): Promise<void> {
+ // Until now the removal time was only implied by updatedAt, which nothing else had touched on these rows.
+ await knex.raw(
+ `UPDATE ?? SET "revokedAt" = "updatedAt" WHERE "userId" IS NULL AND "identityId" IS NULL AND "revokedAt" IS NULL`,
+ [TableName.AgentVaultSession]
+ );
+
+ // The owner columns are cleared by ON DELETE SET NULL from whichever path deletes the user or identity, so only
+ // the database sees the moment a session loses its owner.
+ await knex.raw(`
+ CREATE OR REPLACE FUNCTION ${FUNCTION_NAME}() RETURNS TRIGGER AS $$
+ BEGIN
+ NEW."revokedAt" = NOW();
+ RETURN NEW;
+ END;
+ $$ LANGUAGE plpgsql;
+ `);
+ await knex.raw(`DROP TRIGGER IF EXISTS "${TRIGGER_NAME}" ON ??`, [TableName.AgentVaultSession]);
+ await knex.raw(
+ `
+ CREATE TRIGGER "${TRIGGER_NAME}"
+ BEFORE UPDATE ON ??
+ FOR EACH ROW
+ WHEN (
+ NEW."userId" IS NULL AND NEW."identityId" IS NULL
+ AND (OLD."userId" IS NOT NULL OR OLD."identityId" IS NOT NULL)
+ AND NEW."revokedAt" IS NULL
+ )
+ EXECUTE PROCEDURE ${FUNCTION_NAME}();
+ `,
+ [TableName.AgentVaultSession]
+ );
+}
+
+export async function down(knex: Knex): Promise<void> {
+ await knex.raw(`DROP TRIGGER IF EXISTS "${TRIGGER_NAME}" ON ??`, [TableName.AgentVaultSession]);
+ await knex.raw(`DROP FUNCTION IF EXISTS ${FUNCTION_NAME}()`);
+}
diff --git a/backend/src/ee/services/agent-vault-proxy/agent-vault-proxy-service.ts b/backend/src/ee/services/agent-vault-proxy/agent-vault-proxy-service.ts
index 8767065d20..1011e0b52f 100644
--- a/backend/src/ee/services/agent-vault-proxy/agent-vault-proxy-service.ts
+++ b/backend/src/ee/services/agent-vault-proxy/agent-vault-proxy-service.ts
@@ -638,16 +638,17 @@ export const agentVaultProxyServiceFactory = ({
throw new NotFoundError({ message: "Session not found" });
}
- if (session.revokedAt) throw new UnauthorizedError({ message: "This session has been revoked" });
- if (session.expiresAt && session.expiresAt <= new Date()) {
- throw new UnauthorizedError({ message: "This session has expired" });
- }
// The actor columns are SET NULL so the row outlives its owner for history. A null id must not reach the
// lookups below: knex compiles `where col = null` to `IS NULL`, which matches every user row and made an
- // ownerless session resolve as an admin.
+ // ownerless session resolve as an admin. Checked before revokedAt, which a trigger also sets when the owner
+ // goes, so the message says why.
if (!session.userId && !session.identityId) {
throw new UnauthorizedError({ message: "The identity this session belonged to has been deleted" });
}
+ if (session.revokedAt) throw new UnauthorizedError({ message: "This session has been revoked" });
+ if (session.expiresAt && session.expiresAt <= new Date()) {
+ throw new UnauthorizedError({ message: "This session has expired" });
+ }
const actor = session.userId
? { type: ActorType.USER as const, id: session.userId }
diff --git a/backend/src/ee/services/agent-vault-session-log/agent-vault-session-log-service.ts b/backend/src/ee/services/agent-vault-session-log/agent-vault-session-log-service.ts
index 30c48cd079..54c55619f6 100644
--- a/backend/src/ee/services/agent-vault-session-log/agent-vault-session-log-service.ts
+++ b/backend/src/ee/services/agent-vault-session-log/agent-vault-session-log-service.ts
@@ -27,7 +27,7 @@ import { ObjectStorageType } from "@app/services/object-storage/object-storage-t
import { getAgentVaultPermission } from "../agent-vault/agent-vault-permission";
import { TAgentVaultProxyDALFactory } from "../agent-vault-proxy/agent-vault-proxy-dal";
import { TAgentVaultSessionDALFactory } from "../agent-vault-session/agent-vault-session-dal";
-import { isOwnerlessSession, isSessionOwnedBy } from "../agent-vault-session/agent-vault-session-fns";
+import { isSessionOwnedBy } from "../agent-vault-session/agent-vault-session-fns";
import { TAgentVaultSessionLogConfigDALFactory } from "./agent-vault-session-log-config-dal";
import {
AGENT_VAULT_SESSION_LOG_CHUNK_ID_TIME_TOLERANCE_MS,
@@ -185,12 +185,8 @@ export const agentVaultSessionLogServiceFactory = ({
if (!session) throw sessionNotFound();
const now = new Date();
- // Deleting the owner nulls it through the FK, which fires the update trigger, so updatedAt is when it happened.
- const retirements = [
- session.revokedAt,
- session.expiresAt && session.expiresAt <= now ? session.expiresAt : null,
- isOwnerlessSession(session) ? session.updatedAt : null
- ]
+ // A session that loses its owner is stamped revoked by a database trigger, so revokedAt covers that case too.
+ const retirements = [session.revokedAt, session.expiresAt && session.expiresAt <= now ? session.expiresAt : null]
.filter((at): at is Date => Boolean(at))
.map((at) => at.getTime());
const retiredAt = retirements.length ? Math.min(...retirements) : null;
diff --git a/backend/src/ee/services/agent-vault/AGENTS.md b/backend/src/ee/services/agent-vault/AGENTS.md
index 828950f606..b9be587cad 100644
--- a/backend/src/ee/services/agent-vault/AGENTS.md
+++ b/backend/src/ee/services/agent-vault/AGENTS.md
@@ -107,6 +107,10 @@ and `packages/agentvault/` in the CLI repo. Frontend: `frontend/src/pages/agent-
id: a null actor id reaches the membership lookups as `IS NULL`, matches user rows, and resolved as admin.
`actorType` is stored at mint because `SET NULL` erases which kind of owner it was. It stays nullable (a pod
on the previous release writes none) and `toSessionActor` falls back to the backfill's guess.
+- When `SET NULL` leaves a session with neither actor id, the `agent_vault_sessions_revoke_on_owner_removed`
+ trigger stamps `revokedAt`, because no service sees the deletion of a user or machine identity happen. So
+ `revokedAt` is the one retirement time: never read `updatedAt` as when the owner went, since any edit to
+ the row (a label change) moves it.
- Status is derived from `revokedAt`, `expiresAt` and the actor columns, never stored: a session with neither
actor id reads as revoked in the list and the status filter, so they agree with resolve refusing it. Expiry
is enforced against the clock on every resolve; there is no expiry audit event, matching every other product.