fix(agent): fail closed when user role cannot be resolved

A userId that is not a team member previously resolved to isOwner=true
(role === undefined || role === 'owner'), bypassing bash restrictions.
Treat only flows without a user context (autofill etc.) and explicit
owner memberships as unrestricted; unknown users are now restricted.
This commit is contained in:
yiling
2026-08-04 16:26:16 +08:00
parent c938e84ccc
commit dd2f8e68d9
2 changed files with 21 additions and 4 deletions
+17 -2
View File
@@ -585,7 +585,7 @@ describe('createAgentSession bash restriction for non-owner users', () => {
type TestHarness = Awaited<ReturnType<typeof createAgentSession>>['harness']
async function setupSession(opts: {
role: 'owner' | 'editor' | 'reviewer' | 'none'
role: 'owner' | 'editor' | 'reviewer' | 'none' | 'non-member'
deniedTools?: string[]
sessionId?: string
}): Promise<{ team: { id: string }; userId?: string; harness: TestHarness }> {
@@ -613,7 +613,12 @@ describe('createAgentSession bash restriction for non-owner users', () => {
})
let userId: string | undefined
if (opts.role !== 'none') {
if (opts.role === 'non-member') {
const user = await prisma.user.create({
data: { name: 'Non Member', email: 'non-member@shumai.ai' },
})
userId = user.id // no teamMember record created
} else if (opts.role !== 'none') {
const user = await prisma.user.create({
data: { name: 'Bash User', email: 'bash-user@shumai.ai' },
})
@@ -653,6 +658,16 @@ describe('createAgentSession bash restriction for non-owner users', () => {
expect(harness.getTools().some((t) => t.name === 'bash')).toBe(true)
})
it('should treat a user who is not a team member as restricted (fail-closed)', async () => {
const { harness } = await setupSession({ role: 'non-member' })
const tools = harness.getTools()
expect(tools.some((t) => t.name === 'bash')).toBe(false)
expect(tools.some((t) => t.name === 'read_skill')).toBe(true)
// @ts-expect-error accessing private property for verification
const prompt = await harness.systemPrompt()
expect(prompt).toContain('# Restricted User Context')
})
it.each(['editor', 'reviewer'] as const)(
'should exclude the bash tool initially for %s users',
async (role) => {
+4 -2
View File
@@ -234,9 +234,11 @@ export async function createAgentSession(params: CreateAgentSessionParams) {
Object.assign(skillEnvs, envs)
}
// Resolve the requesting user's team role to enforce owner-only bash privileges
// Resolve the requesting user's team role to enforce owner-only bash privileges.
// A user who is not a member of the team is treated as restricted (fail-closed),
// while flows without a user context (e.g. autofill) remain unrestricted.
const role = await resolveTeamMemberRole(teamId, userId)
const isOwner = role === undefined || role === 'owner'
const isOwner = !userId || role === 'owner'
const restricted = !isOwner
// Restore env variables from previously loaded skills in this session