mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 14:34:40 +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:
@@ -1296,6 +1296,90 @@ describe('AssetService', () => {
|
||||
expect(tasks.length).toBe(0)
|
||||
})
|
||||
|
||||
it('uses the effective project role for bot mention permission checks', async () => {
|
||||
const { project } = await setupBasicAssets()
|
||||
const file = await prisma.asset.create({
|
||||
data: {
|
||||
name: 'perm-file-2',
|
||||
type: AssetType.file,
|
||||
projectId: project.id,
|
||||
status: 'uploaded',
|
||||
},
|
||||
})
|
||||
|
||||
// Editor-permission chat agent
|
||||
const editorBotUser = await prisma.user.create({
|
||||
data: {
|
||||
name: 'Editor Bot Agent',
|
||||
type: 'agent',
|
||||
email: `editor-bot-${Date.now()}@shumai.ai`,
|
||||
},
|
||||
})
|
||||
const editorAgent = await prisma.agent.create({
|
||||
data: {
|
||||
id: editorBotUser.id,
|
||||
teamId: project.teamId,
|
||||
type: 'chat',
|
||||
permission: 'editor',
|
||||
config: { provider: 'p', model: 'm' },
|
||||
},
|
||||
})
|
||||
|
||||
// Team editor, demoted to project reviewer in this project
|
||||
const demoted = await prisma.user.create({
|
||||
data: { name: 'Demoted Member', email: `dem-${Date.now()}@shumai.ai`, password: 'pw' },
|
||||
})
|
||||
const demotedTm = await prisma.teamMember.create({
|
||||
data: { teamId: project.teamId, userId: demoted.id, role: 'editor', scope: 'team' },
|
||||
})
|
||||
await prisma.projectMember.create({
|
||||
data: { projectId: project.id, teamMemberId: demotedTm.id, role: 'reviewer' },
|
||||
})
|
||||
|
||||
// Project-scoped member invited as editor
|
||||
const projEditor = await prisma.user.create({
|
||||
data: { name: 'Proj Editor', email: `pe-${Date.now()}@shumai.ai`, password: 'pw' },
|
||||
})
|
||||
const projEditorTm = await prisma.teamMember.create({
|
||||
data: { teamId: project.teamId, userId: projEditor.id, role: 'reviewer', scope: 'project' },
|
||||
})
|
||||
await prisma.projectMember.create({
|
||||
data: { projectId: project.id, teamMemberId: projEditorTm.id, role: 'editor' },
|
||||
})
|
||||
|
||||
// Demoted team editor (project reviewer) mentioning the editor agent: no workflow
|
||||
const demotedComment = await assetService.createComment({
|
||||
assetId: file.id,
|
||||
userId: demoted.id,
|
||||
message: `Hello <@${editorAgent.id}>`,
|
||||
attachmentIds: [],
|
||||
})
|
||||
const demotedTasks = await prisma.workflowTask.findMany({
|
||||
where: {
|
||||
assetId: file.id,
|
||||
payload: { path: ['agent', 'userCommentId'], equals: demotedComment.id },
|
||||
},
|
||||
})
|
||||
expect(demotedTasks.length).toBe(0)
|
||||
|
||||
// Project-scoped editor mentioning the editor agent: workflow created
|
||||
const peComment = await assetService.createComment({
|
||||
assetId: file.id,
|
||||
userId: projEditor.id,
|
||||
message: `Hello <@${editorAgent.id}>`,
|
||||
attachmentIds: [],
|
||||
})
|
||||
const peTasks = await prisma.workflowTask.findMany({
|
||||
where: {
|
||||
assetId: file.id,
|
||||
payload: { path: ['agent', 'userCommentId'], equals: peComment.id },
|
||||
},
|
||||
})
|
||||
expect(peTasks.length).toBe(1)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
expect((peTasks[0].payload as any)?.agent?.userId).toBe(projEditor.id)
|
||||
})
|
||||
|
||||
it('updates asset order correctly', async () => {
|
||||
const { user, project } = await setupBasicAssets()
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ import { watermarkService } from '@shumai/core/src/watermark/watermark'
|
||||
import { generateKeyBetween } from 'jittered-fractional-indexing'
|
||||
import { getAvatarUrl } from '@shumai/core/src/user/avatar'
|
||||
import { getAgentRequiredLevel, getRoleLevel } from '@shumai/core/src/agent/permissions'
|
||||
import { resolveEffectiveRole } from '@shumai/core/src/authz/authz'
|
||||
|
||||
type AssetWithIncludes = Prisma.AssetGetPayload<{
|
||||
include: {
|
||||
@@ -1378,19 +1379,13 @@ export class AssetService {
|
||||
const mentionedAgentIds = new Set(botMentionMatches.map((match) => match[1]))
|
||||
const handledAgentIds = new Set<string>()
|
||||
|
||||
const member = a.project?.team
|
||||
? await tx.teamMember.findUnique({
|
||||
where: {
|
||||
teamIdUserId: {
|
||||
teamId: a.project.team.id,
|
||||
userId: req.userId,
|
||||
},
|
||||
},
|
||||
select: { role: true },
|
||||
})
|
||||
// Effective role for the project context (project override wins; project-
|
||||
// scoped members without project access resolve to null and are denied).
|
||||
const effectiveRole = a.project?.team
|
||||
? await resolveEffectiveRole(a.project.team.id, a.project.id, req.userId, tx)
|
||||
: null
|
||||
|
||||
const userLevel = getRoleLevel(member?.role)
|
||||
const userLevel = getRoleLevel(effectiveRole)
|
||||
|
||||
if (parentComment && a.project) {
|
||||
const rootSessionId = parentComment.sessionId
|
||||
|
||||
Reference in New Issue
Block a user