Skip to content

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

fix(agent-vault): judge a group or identity by the membership the organization holds, not by who owns the row

Scott Wilson013970a0e163
diff --git a/backend/src/ee/services/agent-vault-member/agent-vault-membership-service.test.ts b/backend/src/ee/services/agent-vault-member/agent-vault-membership-service.test.ts
index 17c55ae4de..7def4d8516 100644
--- a/backend/src/ee/services/agent-vault-member/agent-vault-membership-service.test.ts
+++ b/backend/src/ee/services/agent-vault-member/agent-vault-membership-service.test.ts
@@ -11,6 +11,7 @@ const PROJECT_ID = "project-1";
const OTHER_PROJECT_ID = "project-2";
const ACTOR_ID = "actor-1";
const IDENTITY_ID = "identity-1";
+const GROUP_ID = "group-1";
const addIds = { userIds: [], groupIds: [], machineIdentityIds: [IDENTITY_ID], emails: [] };
@@ -66,11 +67,16 @@ const buildService = ({
)
},
membershipDAL: {
- find: vi.fn(({ scope }: { scope: string }) =>
+ // The organization read is how an actor is judged to exist here, so it answers for whatever it is
+ // asked about. That is what an org holds for every actor it reaches, including one it does not own
+ // itself and one another product scoped to its own project.
+ find: vi.fn(({ scope, $in }: { scope: string; $in?: Record<string, string[] | undefined> }) =>
Promise.resolve(
scope === AccessScope.Project
? productMemberships.map((row) => ({ ...row, actorIdentityId: IDENTITY_ID, createdAt: new Date() }))
- : []
+ : Object.entries($in ?? {}).flatMap(([column, ids]) =>
+ (ids ?? []).map((id) => ({ id: `org-${id}`, [column]: id }))
+ )
)
),
// assertWillRetainProjectAdmin takes an advisory lock through tx.raw, then counts live admins
@@ -168,6 +174,46 @@ describe("agentVaultMembership guards", () => {
expect(deps.membershipDAL.delete).toHaveBeenCalledWith({ $in: { id: ["mem-1"] } }, expect.anything());
});
+ // A sub-organization reaches one of its parent's groups through a membership of its own, and the group
+ // row keeps pointing at the parent. Judging it by that row refused every linked group.
+ test("accepts a group the organization reaches but does not own", async () => {
+ const { service, deps } = buildService();
+
+ const { members } = await service.addProductMembers({
+ projectId: PROJECT_ID,
+ userIds: [],
+ machineIdentityIds: [],
+ groupIds: [GROUP_ID],
+ emails: [],
+ role: ProjectMembershipRole.Member,
+ ctx
+ });
+
+ expect(members[0].id).toBe("mem-new");
+ expect(deps.membershipDAL.find).toHaveBeenCalledWith(
+ expect.objectContaining({ scope: AccessScope.Organization, $in: { actorGroupId: [GROUP_ID] } })
+ );
+ });
+
+ test("refuses a group the organization does not reach at all", async () => {
+ const { service, deps } = buildService();
+ deps.membershipDAL.find.mockResolvedValue([]);
+
+ await expect(
+ service.addProductMembers({
+ projectId: PROJECT_ID,
+ userIds: [],
+ machineIdentityIds: [],
+ groupIds: [GROUP_ID],
+ emails: [],
+ role: ProjectMembershipRole.Member,
+ ctx
+ })
+ ).rejects.toThrow("not found");
+
+ expect(deps.membershipDAL.insertMany).not.toHaveBeenCalled();
+ });
+
test("refuses to change your own role", async () => {
const { service, deps } = buildService();
diff --git a/backend/src/ee/services/agent-vault-member/agent-vault-membership-service.ts b/backend/src/ee/services/agent-vault-member/agent-vault-membership-service.ts
index 72eab7a2f8..3eb48d1e1c 100644
--- a/backend/src/ee/services/agent-vault-member/agent-vault-membership-service.ts
+++ b/backend/src/ee/services/agent-vault-member/agent-vault-membership-service.ts
@@ -339,9 +339,18 @@ export const agentVaultMembershipServiceFactory = ({
const machineIdentityIds = named(actors, AgentVaultMemberType.MachineIdentity);
const userIds = named(actors, AgentVaultMemberType.User);
- const [groups, identities, userMemberIds, identityMemberIds] = await Promise.all([
- groupIds.length ? groupDAL.find({ $in: { id: groupIds }, orgId }) : [],
- machineIdentityIds.length ? identityDAL.find({ $in: { id: machineIdentityIds }, orgId }) : [],
+ // An org reaches a group or an identity through an org-scope membership row, not by owning the row:
+ // that is how a sub-org is given one of its parent's, and how the generic project membership path
+ // decides the same question. Reading orgId off the group or identity refuses every linked one.
+ const orgScoped = (column: "actorGroupId" | "actorIdentityId", ids: string[]) =>
+ ids.length
+ ? membershipDAL.find({ scope: AccessScope.Organization, scopeOrgId: orgId, $in: { [column]: ids } })
+ : [];
+
+ const [groupMemberships, identityMemberships, identities, userMemberIds, identityMemberIds] = await Promise.all([
+ orgScoped("actorGroupId", groupIds),
+ orgScoped("actorIdentityId", machineIdentityIds),
+ machineIdentityIds.length ? identityDAL.find({ $in: { id: machineIdentityIds } }) : [],
orgDAL.findActiveEffectiveOrgMemberActorIds({ actorType: ActorType.USER, actorIds: userIds, orgId }),
orgDAL.findActiveEffectiveOrgMemberActorIds({
actorType: ActorType.IDENTITY,
@@ -350,14 +359,17 @@ export const agentVaultMembershipServiceFactory = ({
})
]);
- const foundGroups = new Set(groups.map((group) => group.id));
+ const foundGroups = new Set(groupMemberships.map((membership) => membership.actorGroupId));
const missingGroups = groupIds.filter((id) => !foundGroups.has(id));
if (missingGroups.length) {
throw new NotFoundError({ message: `Group(s) ${missingGroups.map((el) => `'${el}'`).join(", ")} not found` });
}
+ // Keyed on the membership, so an identity in another org stays a 404 rather than reaching the
+ // active-member check below, which would confirm it exists.
+ const foundIdentities = new Set(identityMemberships.map((membership) => membership.actorIdentityId));
const identityById = new Map(identities.map((identity) => [identity.id, identity]));
- const missingIdentities = machineIdentityIds.filter((id) => !identityById.has(id));
+ const missingIdentities = machineIdentityIds.filter((id) => !foundIdentities.has(id));
if (missingIdentities.length) {
throw new NotFoundError({
message: `Machine identity(s) ${missingIdentities.map((el) => `'${el}'`).join(", ")} not found`