M7-11 P0 | deleteSession / updateSession / getMessages 缺少 userId 鉴权 #99

Closed
opened 2026-06-06 17:12:45 +08:00 by wangdl · 1 comment
Owner

发现位置

代码审查 rag-chat.service.ts 发现 3 个端点缺少用户身份校验。

deleteSession (L259)

async deleteSession(sessionId: string) {
  await this.prisma.chatSession.update({
    where: { id: sessionId },
    data: { isDeleted: true },
  });
}
  • 没有检查 session 是否属于当前用户
  • Controller 也没有传 userId
  • 任何人传任意 sessionId 就能软删除别人的会话

updateSession (L268)

async updateSession(sessionId: string, dto: {...}) {
  return this.prisma.chatSession.update({ where: { id: sessionId }, data });
}
  • 同上,无 userId 校验。可修改任何人的会话属性。

getMessages (L114)

async getMessages(sessionId: string) {
  return this.prisma.chatMessage.findMany({ where: { sessionId }, ... });
}
  • 同上,无 userId 校验。可读取任何人的消息。

修复方案

  1. deleteSession 增加 userId 参数 → findUnique({ id, userId }) 校验所有权 → 403 或 404
  2. updateSession 增加 userId 参数 → findUnique({ id, userId }) 校验所有权 → 403 或 404
  3. getMessages 增加 userId 参数 → 先查 session 校验所有权 → 403 或 404
  4. Controller 对应端点传入 String(user.id)

涉及文件

文件 变更
src/modules/rag-chat/rag-chat.service.ts deleteSession / updateSession / getMessages 加 userId
src/modules/rag-chat/rag-chat.controller.ts DELETE/PATCH/GET messages 传入 user
## 发现位置 代码审查 `rag-chat.service.ts` 发现 3 个端点缺少用户身份校验。 ### deleteSession (L259) ```typescript async deleteSession(sessionId: string) { await this.prisma.chatSession.update({ where: { id: sessionId }, data: { isDeleted: true }, }); } ``` - ❌ 没有检查 session 是否属于当前用户 - ❌ Controller 也没有传 userId - 任何人传任意 sessionId 就能软删除别人的会话 ### updateSession (L268) ```typescript async updateSession(sessionId: string, dto: {...}) { return this.prisma.chatSession.update({ where: { id: sessionId }, data }); } ``` - ❌ 同上,无 userId 校验。可修改任何人的会话属性。 ### getMessages (L114) ```typescript async getMessages(sessionId: string) { return this.prisma.chatMessage.findMany({ where: { sessionId }, ... }); } ``` - ❌ 同上,无 userId 校验。可读取任何人的消息。 ## 修复方案 1. `deleteSession` 增加 userId 参数 → `findUnique({ id, userId })` 校验所有权 → 403 或 404 2. `updateSession` 增加 userId 参数 → `findUnique({ id, userId })` 校验所有权 → 403 或 404 3. `getMessages` 增加 userId 参数 → 先查 session 校验所有权 → 403 或 404 4. Controller 对应端点传入 `String(user.id)` ## 涉及文件 | 文件 | 变更 | |------|------| | src/modules/rag-chat/rag-chat.service.ts | deleteSession / updateSession / getMessages 加 userId | | src/modules/rag-chat/rag-chat.controller.ts | DELETE/PATCH/GET messages 传入 user
wangdl added this to the M7:ChatScope 会话系统 — 学习对象绑定的上下文会话 milestone 2026-06-06 17:12:45 +08:00
Author
Owner

修复

  • getMessages(userId, sessionId) — 先查 session 校验 userId
  • deleteSession(userId, sessionId) — 同上
  • updateSession(userId, sessionId, dto) — 同上
  • Controller 对应端点注入 @CurrentUser 并传 userId
  • 所有权不匹配 → 404(对话不存在)

涉及: rag-chat.service.ts + rag-chat.controller.ts

## 修复 - getMessages(userId, sessionId) — 先查 session 校验 userId - deleteSession(userId, sessionId) — 同上 - updateSession(userId, sessionId, dto) — 同上 - Controller 对应端点注入 @CurrentUser 并传 userId - 所有权不匹配 → 404(对话不存在) 涉及: rag-chat.service.ts + rag-chat.controller.ts
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: wangdl/api-server#99
No description provided.