Skip to content

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

fix(agent-vault): lock delegation parties in deletion order and check live coverage

Scott Wilsonbb96d19397a5
diff --git a/backend/e2e-test/routes/v1/agent-vault.spec.ts b/backend/e2e-test/routes/v1/agent-vault.spec.ts
index 4cfd206587..83a0673459 100644
--- a/backend/e2e-test/routes/v1/agent-vault.spec.ts
+++ b/backend/e2e-test/routes/v1/agent-vault.spec.ts
@@ -5214,6 +5214,75 @@ describe("Agent Vault V1 Router", async () => {
}
});
+ test("a save locks the identity before its membership, the order deleting the identity takes them", async () => {
+ const projectId = await getProjectId();
+ const bot = await createMemberIdentity(`obo-lock-order-bot-${Date.now()}`);
+ // Deleting the identity locks its row, then the cascade deletes its memberships. With the membership locked
+ // first, the save would hold it while its foreign key waited on the identity, and Postgres would abort one of
+ // the two as a deadlock.
+ const trx = await testDb.transaction();
+ try {
+ const { rows } = await trx.raw("select pg_backend_pid() as pid");
+ await trx("identities").where({ id: bot.id }).forUpdate().first();
+
+ const pending = inject("PUT", delegationUrl(bot.id), { scope: "all-members" });
+ await waitUntilBlockedBy(rows[0].pid);
+
+ await trx("memberships")
+ .where({ scope: AccessScope.Project, scopeProjectId: projectId, actorIdentityId: bot.id })
+ .delete();
+ await trx.commit();
+
+ const saved = await pending;
+ expect(saved.statusCode).toBe(404);
+ expect((await inject("GET", delegationUrl(bot.id))).statusCode).toBe(404);
+ } catch (err) {
+ if (!trx.isCompleted()) await trx.rollback();
+ throw err;
+ } finally {
+ await bot.cleanup();
+ }
+ });
+
+ test("a user or group stops covering anyone once it leaves Agent Vault some other way", async () => {
+ const projectId = await getProjectId();
+ const stamp = Date.now();
+ const bundle = await createAccessBundle(`obo-stale-${stamp}`);
+ const bot = await createMemberIdentity(`obo-stale-bot-${stamp}`);
+ const named = await createProjectGroup(projectId, `obo-stale-named-${stamp}`, ProjectMembershipRole.Member);
+ const other = await createProjectGroup(projectId, `obo-stale-other-${stamp}`, ProjectMembershipRole.Member);
+ // Carol is a direct member and in the other group, so she keeps Agent Vault access after either removal.
+ const carol = await createAgentVaultUser("obo-stale-carol", projectId);
+ const dave = await createAgentVaultUser("obo-stale-dave", null);
+ await testDb("user_group_membership").insert([
+ { groupId: other.id, userId: carol.userId },
+ { groupId: named.id, userId: dave.userId },
+ { groupId: other.id, userId: dave.userId }
+ ]);
+ // The generic project membership routes remove a member without clearing delegations.
+ const leaveAgentVault = (actor: { actorUserId: string } | { actorGroupId: string }) =>
+ testDb("memberships")
+ .where({ scope: AccessScope.Project, scopeProjectId: projectId, ...actor })
+ .delete();
+ try {
+ await grantBundle(bundle.id, { groupIds: [other.id], machineIdentityIds: [bot.id] });
+ await delegate(bot, { scope: "selected", userIds: [carol.userId], groupIds: [named.id] });
+ expect((await mintFor(bot, { userId: carol.userId }, bundle.name)).statusCode).toBe(200);
+ expect((await mintFor(bot, { userId: dave.userId }, bundle.name)).statusCode).toBe(200);
+
+ await leaveAgentVault({ actorGroupId: named.id });
+ expect((await mintFor(bot, { userId: dave.userId }, bundle.name)).statusCode).toBe(403);
+
+ await leaveAgentVault({ actorUserId: carol.userId });
+ expect((await mintFor(bot, { userId: carol.userId }, bundle.name)).statusCode).toBe(403);
+ } finally {
+ await bot.cleanup();
+ await deleteUsers([carol.userId, dave.userId]);
+ await named.cleanup();
+ await other.cleanup();
+ }
+ });
+
test("a delegation shows the bundles each user and group can use that the identity can't", async () => {
const projectId = await getProjectId();
const stamp = Date.now();
diff --git a/backend/src/ee/services/agent-vault-delegation/agent-vault-delegation-dal.ts b/backend/src/ee/services/agent-vault-delegation/agent-vault-delegation-dal.ts
index d24908f707..f827777a01 100644
--- a/backend/src/ee/services/agent-vault-delegation/agent-vault-delegation-dal.ts
+++ b/backend/src/ee/services/agent-vault-delegation/agent-vault-delegation-dal.ts
@@ -147,24 +147,36 @@ export const agentVaultDelegationDALFactory = (db: TDbClient) => {
/**
* Under all-members every user is covered, otherwise the users the delegation names and the members of the groups
- * it names. Group membership is read the way the permission engine reads it, without isPending.
+ * it names. Group membership is read the way the permission engine reads it, without isPending. A named user or
+ * group counts only while it holds its own Agent Vault membership, so a row left behind by a removal that didn't
+ * clear it, such as through the generic project membership routes, covers no one.
*/
const coversUser = async (
- { delegation, userId }: { delegation: Pick<TAgentVaultDelegations, "id" | "scope">; userId: string },
+ { delegation, userId }: { delegation: Pick<TAgentVaultDelegations, "id" | "scope" | "projectId">; userId: string },
tx?: Knex
): Promise<boolean> => {
if (delegation.scope === AgentVaultDelegationScope.AllMembers) return true;
try {
const conn = tx || db.replicaNode();
- const subject = await conn(TableName.AgentVaultDelegationSubject)
- .where(`${TableName.AgentVaultDelegationSubject}.delegationId`, delegation.id)
+ const subjects = TableName.AgentVaultDelegationSubject;
+ const inAgentVault = (column: "actorUserId" | "actorGroupId", subjectColumn: "userId" | "groupId") =>
+ conn(TableName.Membership)
+ .where(`${TableName.Membership}.scope`, AccessScope.Project)
+ .where(`${TableName.Membership}.scopeProjectId`, delegation.projectId)
+ .whereRaw(`??.?? = ??.??`, [TableName.Membership, column, subjects, subjectColumn])
+ .select(conn.raw("1"));
+ const subject = await conn(subjects)
+ .where(`${subjects}.delegationId`, delegation.id)
.where((qb) => {
void qb
- .where(`${TableName.AgentVaultDelegationSubject}.userId`, userId)
- .orWhereIn(
- `${TableName.AgentVaultDelegationSubject}.groupId`,
- conn(TableName.UserGroupMembership).select("groupId").where({ userId })
- );
+ .where((named) => {
+ void named.where(`${subjects}.userId`, userId).whereExists(inAgentVault("actorUserId", "userId"));
+ })
+ .orWhere((viaGroup) => {
+ void viaGroup
+ .whereIn(`${subjects}.groupId`, conn(TableName.UserGroupMembership).select("groupId").where({ userId }))
+ .whereExists(inAgentVault("actorGroupId", "groupId"));
+ });
})
.select(db.ref("id").withSchema(TableName.AgentVaultDelegationSubject))
.first();
@@ -193,7 +205,9 @@ export const agentVaultDelegationDALFactory = (db: TDbClient) => {
/**
* The Agent Vault memberships of the identity and of the users and groups a save names, taken FOR SHARE. Removing a
* member deletes its row in a transaction of its own, so a save waits for a removal in flight and then sees the row
- * gone, and a removal that starts later waits for the save and then clears what it wrote.
+ * gone, and a removal that starts later waits for the save and then clears what it wrote. The identity, user and
+ * group rows are taken FOR KEY SHARE first, the lock the save's foreign keys take anyway: deleting one of them
+ * deletes it before the cascade reaches its memberships, so locking in that order keeps the two from deadlocking.
*/
const lockMemberships = async (
{
@@ -205,6 +219,9 @@ export const agentVaultDelegationDALFactory = (db: TDbClient) => {
tx: Knex
) => {
try {
+ await tx(TableName.Identity).where({ id: identityId }).forKeyShare().select("id");
+ if (userIds.length) await tx(TableName.Users).whereIn("id", userIds).orderBy("id").forKeyShare().select("id");
+ if (groupIds.length) await tx(TableName.Groups).whereIn("id", groupIds).orderBy("id").forKeyShare().select("id");
return (await tx(TableName.Membership)
.where(`${TableName.Membership}.scope`, AccessScope.Project)
.where(`${TableName.Membership}.scopeProjectId`, projectId)