mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-25 19:19:28 +00:00
* 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).
94 lines
4.2 KiB
TypeScript
94 lines
4.2 KiB
TypeScript
import { setupTestDbHooks } from '@shumai/db/test'
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
import { prisma } from '@shumai/db'
|
|
import { resolveEffectiveRole } from '@shumai/core/src/authz/authz'
|
|
|
|
describe('resolveEffectiveRole', () => {
|
|
setupTestDbHooks()
|
|
|
|
async function setupData() {
|
|
const team = await prisma.team.create({ data: { name: 'Role Team' } })
|
|
const projectA = await prisma.project.create({
|
|
data: { name: 'Project A', teamId: team.id },
|
|
})
|
|
const projectB = await prisma.project.create({
|
|
data: { name: 'Project B', teamId: team.id },
|
|
})
|
|
|
|
// Team-scoped owner
|
|
const owner = await prisma.user.create({ data: { name: 'Owner', email: 'o@x.com' } })
|
|
await prisma.teamMember.create({
|
|
data: { teamId: team.id, userId: owner.id, role: 'owner', scope: 'team' },
|
|
})
|
|
|
|
// Team-scoped reviewer, promoted to project owner in Project A
|
|
const promoted = await prisma.user.create({ data: { name: 'Promoted', email: 'p@x.com' } })
|
|
const promotedTm = await prisma.teamMember.create({
|
|
data: { teamId: team.id, userId: promoted.id, role: 'reviewer', scope: 'team' },
|
|
})
|
|
await prisma.projectMember.create({
|
|
data: { projectId: projectA.id, teamMemberId: promotedTm.id, role: 'owner' },
|
|
})
|
|
|
|
// Team-scoped editor, demoted to project reviewer in Project A
|
|
const demoted = await prisma.user.create({ data: { name: 'Demoted', email: 'd@x.com' } })
|
|
const demotedTm = await prisma.teamMember.create({
|
|
data: { teamId: team.id, userId: demoted.id, role: 'editor', scope: 'team' },
|
|
})
|
|
await prisma.projectMember.create({
|
|
data: { projectId: projectA.id, teamMemberId: demotedTm.id, role: 'reviewer' },
|
|
})
|
|
|
|
// Project-scoped member (invited to Project A only, as editor)
|
|
const guest = await prisma.user.create({ data: { name: 'Guest', email: 'g@x.com' } })
|
|
const guestTm = await prisma.teamMember.create({
|
|
data: { teamId: team.id, userId: guest.id, role: 'reviewer', scope: 'project' },
|
|
})
|
|
await prisma.projectMember.create({
|
|
data: { projectId: projectA.id, teamMemberId: guestTm.id, role: 'editor' },
|
|
})
|
|
|
|
// Non-member
|
|
const outsider = await prisma.user.create({ data: { name: 'Outsider', email: 'x@x.com' } })
|
|
|
|
return { team, projectA, projectB, owner, promoted, demoted, guest, outsider }
|
|
}
|
|
|
|
it('returns the team role for team-scoped members without a project override', async () => {
|
|
const { team, projectA, owner } = await setupData()
|
|
await expect(resolveEffectiveRole(team.id, projectA.id, owner.id)).resolves.toBe('owner')
|
|
await expect(resolveEffectiveRole(team.id, undefined, owner.id)).resolves.toBe('owner')
|
|
})
|
|
|
|
it('prefers the project override over the team role', async () => {
|
|
const { team, projectA, projectB, promoted, demoted } = await setupData()
|
|
|
|
// Promoted: team reviewer -> project owner
|
|
await expect(resolveEffectiveRole(team.id, projectA.id, promoted.id)).resolves.toBe('owner')
|
|
// Outside the overridden project, the team role applies
|
|
await expect(resolveEffectiveRole(team.id, projectB.id, promoted.id)).resolves.toBe('reviewer')
|
|
|
|
// Demoted: team editor -> project reviewer
|
|
await expect(resolveEffectiveRole(team.id, projectA.id, demoted.id)).resolves.toBe('reviewer')
|
|
await expect(resolveEffectiveRole(team.id, projectB.id, demoted.id)).resolves.toBe('editor')
|
|
})
|
|
|
|
it('resolves the project role for project-scoped members inside their project', async () => {
|
|
const { team, projectA, guest } = await setupData()
|
|
await expect(resolveEffectiveRole(team.id, projectA.id, guest.id)).resolves.toBe('editor')
|
|
})
|
|
|
|
it('restricts project-scoped members without a matching project record', async () => {
|
|
const { team, projectB, guest } = await setupData()
|
|
await expect(resolveEffectiveRole(team.id, projectB.id, guest.id)).resolves.toBeNull()
|
|
await expect(resolveEffectiveRole(team.id, undefined, guest.id)).resolves.toBeNull()
|
|
})
|
|
|
|
it('returns null for non-members', async () => {
|
|
const { team, projectA, outsider } = await setupData()
|
|
await expect(resolveEffectiveRole(team.id, projectA.id, outsider.id)).resolves.toBeNull()
|
|
await expect(resolveEffectiveRole(team.id, undefined, outsider.id)).resolves.toBeNull()
|
|
})
|
|
})
|