mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-29 20:34:28 +00:00
feat(authz): enforce project-aware roles for agent/skill/mcp permissions (#314)
* feat(authz): enforce project-aware roles for agent/skill/mcp permissions Introduce a shared resolveEffectiveRole helper that mirrors AuthzService precedence rules (projectMember.role override -> team role for team-scoped members -> restricted), and route all agent/skill/mcp runtime permission checks through it. Previously these checks used the raw teamMember.role, which let project-only members execute team-level agents/skills/MCP tools and ignored per-project role overrides. Changes: - authz: add resolveEffectiveRole(teamId, projectId, userId, db) and refactor hasPermission to use it (behavior-preserving) - chat/asset/agent-activity: gate agent usage by the effective project role - read_skill: enforce skill permission via effective role (projectId threaded from the agent session) - mcp-service: checkPermission and buildAgentTools filter by effective role; un-permitted servers' tools are no longer advertised to the model - agent activity: only advertise skills the user's effective role permits - listProjectMembers: filter included agent bots by the requester's effective project role - new GET /projects/:projectId/chat-agents API returning chat agents scoped to the project and the user's effective role; WebUI chat selector now uses it (settings keeps the team-level API) - tests: effective-role resolver, chat/mention/MCP/skill project-role matrices, listProjectChatAgents, listProjectMembers filter, API route * fix(agent): forward projectId to getAgentChatContextActivity in agentChat workflow The agentChat workflow called getAgentChatContextActivity without passing projectId from the task payload, so project-scoped members (scope: 'project') resolved a null effective role in the activity and were denied with 'Insufficient role to use agent' even though the chat selector and chat.ts both authorized them. Forward payload.projectId so the activity enforces the user's effective project role, and assert the forwarding in the workflow unit test (fails without the fix).
This commit is contained in:
@@ -196,6 +196,53 @@ describe('ProjectService', () => {
|
||||
expect(allMembers).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('filters agent bots by the requester effective project role', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Team Bot Filter' } })
|
||||
const project = await prisma.project.create({ data: { name: 'P2', teamId: team.id } })
|
||||
|
||||
const createChatAgent = async (name: string, permission: 'owner' | 'editor' | 'reviewer') => {
|
||||
const bot = await prisma.user.create({
|
||||
data: { name, email: `${name}-${Date.now()}@example.com`, password: 'pw', type: 'agent' },
|
||||
})
|
||||
await prisma.teamMember.create({
|
||||
data: { teamId: team.id, userId: bot.id, role: 'editor', scope: 'team' },
|
||||
})
|
||||
await prisma.agent.create({
|
||||
data: {
|
||||
id: bot.id,
|
||||
teamId: team.id,
|
||||
type: 'chat',
|
||||
enabled: true,
|
||||
permission,
|
||||
config: { provider: 'google', model: 'gemini' },
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
await createChatAgent('Reviewer Bot', 'reviewer')
|
||||
await createChatAgent('Editor Bot', 'editor')
|
||||
|
||||
// Project-scoped requester invited as reviewer: sees only reviewer bots
|
||||
const requester = await prisma.user.create({
|
||||
data: { name: 'Req', email: `req-${Date.now()}@example.com`, password: 'pw' },
|
||||
})
|
||||
const requesterTm = await prisma.teamMember.create({
|
||||
data: { teamId: team.id, userId: requester.id, role: 'reviewer', scope: 'project' },
|
||||
})
|
||||
await prisma.projectMember.create({
|
||||
data: { projectId: project.id, teamMemberId: requesterTm.id, role: 'reviewer' },
|
||||
})
|
||||
|
||||
const members = await projectService.listProjectMembers({
|
||||
projectId: project.id,
|
||||
includeAgents: true,
|
||||
requesterUserId: requester.id,
|
||||
})
|
||||
const agentNames = members.filter((m) => m.id !== requester.id).map((m) => m.name)
|
||||
expect(agentNames).toContain('Reviewer Bot')
|
||||
expect(agentNames).not.toContain('Editor Bot')
|
||||
})
|
||||
|
||||
it('should allow upserting custom role override for team-scoped members via updateMemberRole', async () => {
|
||||
const team = await prisma.team.create({ data: { name: 'Team Scope Test' } })
|
||||
const project = await prisma.project.create({ data: { name: 'Project A', teamId: team.id } })
|
||||
|
||||
@@ -3,6 +3,8 @@ import { Prisma } from '@shumai/db'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { paginateQuery, PaginatedData } from '@shumai/core/src/pagination'
|
||||
import { getAvatarUrl } from '@shumai/core/src/user/avatar'
|
||||
import { getAllowedAgentRoles } from '@shumai/core/src/agent/permissions'
|
||||
import { resolveEffectiveRole } from '@shumai/core/src/authz/authz'
|
||||
import {
|
||||
ServiceCreateProjectRequest,
|
||||
ServiceUpdateProjectRequest,
|
||||
@@ -226,17 +228,35 @@ export class ProjectService {
|
||||
})
|
||||
if (!project) throw new Error('Project not found')
|
||||
|
||||
// When agents are requested, only include the chat agents the requester's
|
||||
// effective project role permits (project override wins; restricted
|
||||
// requesters see no agents at all). Without a requester context, fall back
|
||||
// to including all enabled chat agents (legacy behavior).
|
||||
let agentFilter: Prisma.AgentWhereInput | undefined
|
||||
if (req.includeAgents) {
|
||||
if (req.requesterUserId) {
|
||||
const effectiveRole = await resolveEffectiveRole(
|
||||
project.teamId,
|
||||
req.projectId,
|
||||
req.requesterUserId,
|
||||
)
|
||||
if (effectiveRole) {
|
||||
agentFilter = {
|
||||
type: 'chat' as const,
|
||||
enabled: true,
|
||||
permission: { in: getAllowedAgentRoles(effectiveRole) },
|
||||
}
|
||||
}
|
||||
} else {
|
||||
agentFilter = { type: 'chat' as const, enabled: true }
|
||||
}
|
||||
}
|
||||
|
||||
const userTypeFilter = req.includeAgents
|
||||
? {
|
||||
OR: [
|
||||
{ type: 'human' as const },
|
||||
{
|
||||
type: 'agent' as const,
|
||||
agent: {
|
||||
type: 'chat' as const,
|
||||
enabled: true,
|
||||
},
|
||||
},
|
||||
...(agentFilter ? [{ type: 'agent' as const, agent: agentFilter }] : []),
|
||||
],
|
||||
}
|
||||
: { type: { not: 'agent' as const } }
|
||||
|
||||
Reference in New Issue
Block a user