feat(chat): enforce team-scoped authorization and session creator check

- Update chat API routes and service methods to include and verify teamId context
- Validate that the requesting user is the creator of the chat session during continuation
- Update frontend chatbot store and components to pass teamId parameter
- Update backend API/core and frontend store tests to verify team scoping and creator checks
This commit is contained in:
yiling
2026-07-13 11:35:55 +08:00
parent 61c49b4d7f
commit e080167ac5
7 changed files with 158 additions and 91 deletions
+16 -9
View File
@@ -43,7 +43,7 @@ describe('Chat API', () => {
vi.mocked(authzService.hasPermission).mockResolvedValue(undefined)
})
describe('GET /chat/sessions', () => {
describe('GET /teams/:teamId/chat/sessions', () => {
it('returns list of sessions', async () => {
const mockSessions = {
data: [
@@ -62,15 +62,16 @@ describe('Chat API', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(chatService.listSessions).mockResolvedValue(mockSessions as any)
const res = await app.request('/chat/sessions?first=10')
const res = await app.request('/teams/team1/chat/sessions?first=10')
expect(res.status).toBe(200)
const data = await res.json()
expect(data.data).toHaveLength(1)
expect(data.data[0].id).toBe('session1')
expect(chatService.listSessions).toHaveBeenCalledWith('user1', 'team1', expect.any(Object))
})
})
describe('GET /chat/sessions/:sessionId/messages', () => {
describe('GET /teams/:teamId/chat/sessions/:sessionId/messages', () => {
it('returns list of messages', async () => {
const mockMessages = [
{
@@ -84,27 +85,28 @@ describe('Chat API', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(chatService.listMessages).mockResolvedValue(mockMessages as any)
const res = await app.request('/chat/sessions/session1/messages')
const res = await app.request('/teams/team1/chat/sessions/session1/messages')
expect(res.status).toBe(200)
const data = await res.json()
expect(data).toHaveLength(1)
expect(data[0].content).toBe('hello')
expect(chatService.listMessages).toHaveBeenCalledWith('user1', 'team1', 'session1')
})
})
describe('DELETE /chat/sessions/:sessionId', () => {
describe('DELETE /teams/:teamId/chat/sessions/:sessionId', () => {
it('deletes a session', async () => {
vi.mocked(chatService.deleteSession).mockResolvedValue(undefined)
const res = await app.request('/chat/sessions/session1', {
const res = await app.request('/teams/team1/chat/sessions/session1', {
method: 'DELETE',
})
expect(res.status).toBe(204)
expect(chatService.deleteSession).toHaveBeenCalledWith('user1', 'session1')
expect(chatService.deleteSession).toHaveBeenCalledWith('user1', 'team1', 'session1')
})
})
describe('POST /chat', () => {
describe('POST /teams/:teamId/chat', () => {
it('starts or continues chat and returns stream', async () => {
vi.mocked(chatService.startOrContinueChat).mockResolvedValue({
sessionId: 'session1',
@@ -135,7 +137,7 @@ describe('Chat API', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(prisma.workflowTask.findUnique).mockResolvedValue(mockTask as any)
const res = await app.request('/chat', {
const res = await app.request('/teams/team1/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
@@ -150,6 +152,11 @@ describe('Chat API', () => {
const text = await res.text()
expect(text).toContain('data:')
expect(text).toContain('entry1')
expect(chatService.startOrContinueChat).toHaveBeenCalledWith(
expect.objectContaining({ id: 'user1' }),
'team1',
expect.any(Object),
)
})
})
})
+41 -8
View File
@@ -3,38 +3,71 @@ import { zValidator } from '@hono/zod-validator'
import { streamSSE } from 'hono/streaming'
import { prisma } from '@shumai/db'
import { chatService, mapEntryToMessage } from '@shumai/core/src/chat/chat'
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
import { chatRequestSchema, paginationParamsSchema } from '@shumai/dtos'
import type { Prisma } from '@shumai/db'
type User = Prisma.UserGetPayload<Record<string, never>>
const route = new Hono<{ Variables: { user: User } }>()
.get('/chat/sessions', zValidator('query', paginationParamsSchema), async (c) => {
.get('/teams/:teamId/chat/sessions', zValidator('query', paginationParamsSchema), async (c) => {
const user = c.get('user')
const teamId = c.req.param('teamId')
const query = c.req.valid('query')
const res = await chatService.listSessions(user.id, query)
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Team,
id: teamId,
})
const res = await chatService.listSessions(user.id, teamId, query)
return c.json(res, 200)
})
.get('/chat/sessions/:sessionId/messages', async (c) => {
.get('/teams/:teamId/chat/sessions/:sessionId/messages', async (c) => {
const user = c.get('user')
const teamId = c.req.param('teamId')
const sessionId = c.req.param('sessionId')
const res = await chatService.listMessages(user.id, sessionId)
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Team,
id: teamId,
})
const res = await chatService.listMessages(user.id, teamId, sessionId)
return c.json(res, 200)
})
.delete('/chat/sessions/:sessionId', async (c) => {
.delete('/teams/:teamId/chat/sessions/:sessionId', async (c) => {
const user = c.get('user')
const teamId = c.req.param('teamId')
const sessionId = c.req.param('sessionId')
await chatService.deleteSession(user.id, sessionId)
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Team,
id: teamId,
})
await chatService.deleteSession(user.id, teamId, sessionId)
return new Response(null, { status: 204 })
})
.post('/chat', zValidator('json', chatRequestSchema), async (c) => {
.post('/teams/:teamId/chat', zValidator('json', chatRequestSchema), async (c) => {
const user = c.get('user')
const teamId = c.req.param('teamId')
const req = c.req.valid('json')
const { sessionId, taskId } = await chatService.startOrContinueChat(user, req)
await authzService.hasPermission({
user,
permission: Permission.Read,
type: ResourceType.Team,
id: teamId,
})
const { sessionId, taskId } = await chatService.startOrContinueChat(user, teamId, req)
c.header('x-session-id', sessionId)