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
+21 -14
View File
@@ -4,17 +4,22 @@ import { client } from '@/ui/api/client'
import type { ChatMessage } from '@shumai/dtos'
vi.mock('@/ui/api/client', () => {
const chatMock = {
$post: vi.fn(),
sessions: {
$get: vi.fn(() => Promise.resolve({ ok: true, json: async () => ({ data: [] }) } as any)),
$delete: vi.fn(),
messages: {
$get: vi.fn(),
},
},
}
return {
client: {
api: {
chat: {
$post: vi.fn(),
sessions: {
$get: vi.fn(),
$delete: vi.fn(),
messages: {
$get: vi.fn(),
},
teams: {
':teamId': {
chat: chatMock,
},
},
},
@@ -99,9 +104,9 @@ describe('useChatbotStore', () => {
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(client.api.chat.$post).mockResolvedValue(mockResponse as any)
vi.mocked(client.api.teams[':teamId'].chat.$post).mockResolvedValue(mockResponse as any)
const sendPromise = useChatbotStore.getState().sendMessage('hello', 'proj-123')
const sendPromise = useChatbotStore.getState().sendMessage('team-123', 'hello', 'proj-123')
// Optimistic message should be present immediately
const stateWithOptimistic = useChatbotStore.getState()
@@ -166,9 +171,11 @@ describe('useChatbotStore', () => {
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(client.api.chat.$post).mockResolvedValue(mockResponse as any)
vi.mocked(client.api.teams[':teamId'].chat.$post).mockResolvedValue(mockResponse as any)
const sendPromise = useChatbotStore.getState().sendMessage('new message', 'proj-123')
const sendPromise = useChatbotStore
.getState()
.sendMessage('team-123', 'new message', 'proj-123')
// Optimistic message should be appended immediately
const stateWithOptimistic = useChatbotStore.getState()
@@ -221,9 +228,9 @@ describe('useChatbotStore', () => {
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(client.api.chat.$post).mockResolvedValue(mockResponse as any)
vi.mocked(client.api.teams[':teamId'].chat.$post).mockResolvedValue(mockResponse as any)
const sendPromise = useChatbotStore.getState().sendMessage('hello', 'proj-123')
const sendPromise = useChatbotStore.getState().sendMessage('team-123', 'hello', 'proj-123')
// 3. Verify that both the optimistic context message and user message are present immediately
const stateWithOptimistic = useChatbotStore.getState()
+22 -15
View File
@@ -29,11 +29,16 @@ interface ChatbotState {
selectedAgentId: string | null
setSelectedAgentId: (id: string | null) => void
fetchHistorySessions: () => Promise<void>
loadSession: (sessionId: string) => Promise<void>
deleteSession: (sessionId: string) => Promise<void>
fetchHistorySessions: (teamId: string) => Promise<void>
loadSession: (teamId: string, sessionId: string) => Promise<void>
deleteSession: (teamId: string, sessionId: string) => Promise<void>
startNewSession: () => void
sendMessage: (text: string, projectId: string, contextAssetId?: string) => Promise<void>
sendMessage: (
teamId: string,
text: string,
projectId: string,
contextAssetId?: string,
) => Promise<void>
}
export const useChatbotStore = create<ChatbotState>((set) => ({
@@ -64,9 +69,10 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
selectedAgentId: null,
setSelectedAgentId: (id) => set({ selectedAgentId: id }),
fetchHistorySessions: async () => {
fetchHistorySessions: async (teamId) => {
try {
const res = await client.api.chat.sessions.$get({
const res = await client.api.teams[':teamId'].chat.sessions.$get({
param: { teamId },
query: { first: '50' },
})
if (res.ok) {
@@ -78,10 +84,10 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
}
},
loadSession: async (sessionId) => {
loadSession: async (teamId, sessionId) => {
try {
const res = await client.api.chat.sessions[':sessionId'].messages.$get({
param: { sessionId },
const res = await client.api.teams[':teamId'].chat.sessions[':sessionId'].messages.$get({
param: { teamId, sessionId },
})
if (res.ok) {
const messages = await res.json()
@@ -104,10 +110,10 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
}
},
deleteSession: async (sessionId) => {
deleteSession: async (teamId, sessionId) => {
try {
const res = await client.api.chat.sessions[':sessionId'].$delete({
param: { sessionId },
const res = await client.api.teams[':teamId'].chat.sessions[':sessionId'].$delete({
param: { teamId, sessionId },
})
if (res.ok) {
toast.success('Session deleted successfully')
@@ -133,7 +139,7 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
})
},
sendMessage: async (text, projectId, contextAssetId) => {
sendMessage: async (teamId, text, projectId, contextAssetId) => {
const state = useChatbotStore.getState()
if (state.isStreaming) return
@@ -180,7 +186,8 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
}
try {
const res = await client.api.chat.$post({
const res = await client.api.teams[':teamId'].chat.$post({
param: { teamId },
json: {
agentId,
textPrompt: text,
@@ -272,7 +279,7 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
// If we finished successfully, trigger a history list refresh
const finalState = useChatbotStore.getState()
finalState.fetchHistorySessions()
finalState.fetchHistorySessions(teamId)
} catch (err) {
const errMsg = err instanceof Error ? err.message : 'Failed to send message'
toast.error(errMsg)