Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(database): scope group-agent removal by ownership (IDOR) (#16586)

Arvin Xu9ed5a7e20d8a
diff --git a/packages/database/src/repositories/agentGroup/index.test.ts b/packages/database/src/repositories/agentGroup/index.test.ts
index ea26b89f469..d869a1450c8 100644
--- a/packages/database/src/repositories/agentGroup/index.test.ts
+++ b/packages/database/src/repositories/agentGroup/index.test.ts
@@ -731,6 +731,32 @@ describe('AgentGroupRepository', () => {
expect(agent).toBeDefined();
});
+ it('should not remove agents from another user’s group (IDOR)', async () => {
+ // Attacker scoped to a different user targets the victim's group + agents.
+ const attackerRepo = new AgentGroupRepository(serverDB, otherUserId);
+
+ const result = await attackerRepo.removeAgentsFromGroup('remove-group', [
+ 'remove-virtual',
+ 'remove-regular',
+ ]);
+
+ // Nothing removed: junction rows belong to the victim, not the attacker.
+ expect(result.removedFromGroup).toBe(0);
+ expect(result.deletedVirtualAgentIds).toEqual([]);
+
+ // Victim's group membership is untouched.
+ const groupAgents = await serverDB.query.chatGroupsAgents.findMany({
+ where: (cga, { eq }) => eq(cga.chatGroupId, 'remove-group'),
+ });
+ expect(groupAgents).toHaveLength(3);
+
+ // Victim's virtual agent is not deleted.
+ const virtualAgent = await serverDB.query.agents.findFirst({
+ where: (a, { eq }) => eq(a.id, 'remove-virtual'),
+ });
+ expect(virtualAgent).toBeDefined();
+ });
+
it('should handle multiple virtual agents', async () => {
// Add another virtual agent
await serverDB.insert(agents).values({
diff --git a/packages/database/src/repositories/agentGroup/index.ts b/packages/database/src/repositories/agentGroup/index.ts
index 223b7f041d8..ccfcd0d7ea4 100644
--- a/packages/database/src/repositories/agentGroup/index.ts
+++ b/packages/database/src/repositories/agentGroup/index.ts
@@ -92,6 +92,8 @@ export class AgentGroupRepository {
buildWorkspaceWhere({ userId: this.userId, workspaceId: this.workspaceId }, chatGroups);
private agentOwnership = () =>
buildWorkspaceWhere({ userId: this.userId, workspaceId: this.workspaceId }, agents);
+ private groupAgentOwnership = () =>
+ buildWorkspaceWhere({ userId: this.userId, workspaceId: this.workspaceId }, chatGroupsAgents);
private topicOwnership = () =>
buildWorkspaceWhere({ userId: this.userId, workspaceId: this.workspaceId }, topics);
private threadOwnership = () =>
@@ -538,12 +540,19 @@ export class AgentGroupRepository {
const { virtualAgents } = await this.checkAgentsBeforeRemoval(groupId, agentIds);
const virtualAgentIds = virtualAgents.map((a) => a.id);
- // 2. Remove all agents from the group (batch delete from junction table)
- await this.db
+ // 2. Remove all agents from the group (batch delete from junction table).
+ // Scope by the caller's ownership so a client-supplied groupId can only touch
+ // the caller's own junction rows — never another user's group membership (IDOR).
+ const removed = await this.db
.delete(chatGroupsAgents)
.where(
- and(eq(chatGroupsAgents.chatGroupId, groupId), inArray(chatGroupsAgents.agentId, agentIds)),
- );
+ and(
+ eq(chatGroupsAgents.chatGroupId, groupId),
+ inArray(chatGroupsAgents.agentId, agentIds),
+ this.groupAgentOwnership(),
+ ),
+ )
+ .returning({ agentId: chatGroupsAgents.agentId });
// 3. Delete virtual agents if requested
// Note: Virtual agents are standalone (no associated sessions), so we can delete them directly
@@ -556,7 +565,7 @@ export class AgentGroupRepository {
return {
deletedVirtualAgentIds: deleteVirtualAgents ? virtualAgentIds : [],
- removedFromGroup: agentIds.length,
+ removedFromGroup: removed.length,
};
}