From a2776f68ae6dca19349f833a52b232dcc7f3149e Mon Sep 17 00:00:00 2001 From: WuYongJie Date: Thu, 28 May 2026 16:08:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B6=E7=B4=A7AI=E6=94=B9=E5=9B=BE=E7=B4=A0?= =?UTF-8?q?=E6=9D=90=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/ai/edit/__tests__/route.test.ts | 174 ++++++++++++++++++++++++ app/api/ai/edit/route.ts | 26 +++- docs/商业化地基排查问题清单_20260524.md | 21 +++ 3 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 app/api/ai/edit/__tests__/route.test.ts diff --git a/app/api/ai/edit/__tests__/route.test.ts b/app/api/ai/edit/__tests__/route.test.ts new file mode 100644 index 0000000..00cc9fc --- /dev/null +++ b/app/api/ai/edit/__tests__/route.test.ts @@ -0,0 +1,174 @@ +import { NextRequest } from 'next/server' + +jest.mock('@/lib/auth', () => ({ + authOptions: {}, +})) + +jest.mock('next-auth', () => ({ + getServerSession: jest.fn(), +})) + +jest.mock('@/lib/config', () => ({ + featureFlags: { + aiEditEnabled: false, + }, +})) + +jest.mock('@/lib/prisma', () => ({ + prisma: { + systemSettings: { + findFirst: jest.fn(), + }, + material: { + findFirst: jest.fn(), + }, + user: { + findUnique: jest.fn(), + }, + department: { + findMany: jest.fn(), + }, + }, +})) + +import { POST } from '../route' + +const { getServerSession } = require('next-auth') +const { prisma } = require('@/lib/prisma') + +function buildRequest(body: Record = {}) { + return new NextRequest('http://localhost:3000/api/ai/edit', { + method: 'POST', + body: JSON.stringify({ + materialId: 'material-1', + operation: 'remove-background', + ...body, + }), + }) +} + +function createMaterial(overrides: Record = {}) { + return { + id: 'material-1', + userId: 'owner-1', + ownerType: 'TEAM', + ownerId: null, + deletedAt: null, + user: { + id: 'owner-1', + department: '设计部', + }, + ...overrides, + } +} + +describe('AI 改图接口权限边界', () => { + beforeEach(() => { + jest.resetAllMocks() + + getServerSession.mockResolvedValue({ + user: { + id: 'owner-1', + role: 'USER', + }, + }) + + prisma.systemSettings.findFirst.mockResolvedValue({ + enableAiImageEdit: true, + }) + + prisma.material.findFirst.mockResolvedValue(createMaterial()) + prisma.user.findUnique.mockImplementation(async ({ where }: any) => { + if (where.id === 'owner-1') { + return { + id: 'owner-1', + role: 'USER', + isSuperAdmin: false, + department: '设计部', + managedDepartment: null, + } + } + + if (where.id === 'other-user') { + return { + id: 'other-user', + role: 'USER', + isSuperAdmin: false, + department: '运营部', + managedDepartment: null, + } + } + + return null + }) + prisma.department.findMany.mockResolvedValue([]) + }) + + test('未登录不能创建 AI 改图任务', async () => { + getServerSession.mockResolvedValue(null) + + const response = await POST(buildRequest()) + const data = await response.json() + + expect(response.status).toBe(401) + expect(data.error).toBe('未登录') + expect(prisma.material.findFirst).not.toHaveBeenCalled() + }) + + test('系统未启用 AI 改图时不能创建任务', async () => { + prisma.systemSettings.findFirst.mockResolvedValue({ + enableAiImageEdit: false, + }) + + const response = await POST(buildRequest()) + const data = await response.json() + + expect(response.status).toBe(501) + expect(data.error).toBe('AI改图功能未启用') + expect(prisma.material.findFirst).not.toHaveBeenCalled() + }) + + test('素材不存在或已删除时不能创建任务', async () => { + prisma.material.findFirst.mockResolvedValue(null) + + const response = await POST(buildRequest()) + const data = await response.json() + + expect(response.status).toBe(404) + expect(data.error).toBe('素材不存在') + expect(prisma.material.findFirst).toHaveBeenCalledWith( + expect.objectContaining({ + where: { + id: 'material-1', + deletedAt: null, + }, + }) + ) + }) + + test('无权管理该素材的普通用户不能创建任务', async () => { + getServerSession.mockResolvedValue({ + user: { + id: 'other-user', + role: 'USER', + }, + }) + + const response = await POST(buildRequest()) + const data = await response.json() + + expect(response.status).toBe(403) + expect(data.error).toBe('您没有权限操作此素材') + }) + + test('素材本人可以创建占位任务', async () => { + const response = await POST(buildRequest({ params: { prompt: '调整亮度' } })) + const data = await response.json() + + expect(response.status).toBe(200) + expect(data.status).toBe('queued') + expect(data.materialId).toBe('material-1') + expect(data.operation).toBe('remove-background') + expect(data.params).toEqual({ prompt: '调整亮度' }) + }) +}) diff --git a/app/api/ai/edit/route.ts b/app/api/ai/edit/route.ts index 0275533..eebd727 100644 --- a/app/api/ai/edit/route.ts +++ b/app/api/ai/edit/route.ts @@ -3,6 +3,7 @@ import { getServerSession } from 'next-auth' import { authOptions } from '@/lib/auth' import { featureFlags } from '@/lib/config' import { prisma } from '@/lib/prisma' +import { canManageMaterial, getMaterialPermissionActor } from '@/lib/material-permissions' async function ensureAiEditEnabled() { if (featureFlags.aiEditEnabled) return true @@ -31,7 +32,30 @@ export async function POST(req: NextRequest) { return NextResponse.json({ error: '缺少必要参数(materialId/operation)' }, { status: 400 }) } - // TODO: 校验用户是否有权限访问该素材(上传者或其上级) + const material = await prisma.material.findFirst({ + where: { + id: materialId, + deletedAt: null, + }, + include: { + user: { + select: { + id: true, + department: true, + }, + }, + }, + }) + + if (!material) { + return NextResponse.json({ error: '素材不存在' }, { status: 404 }) + } + + const actor = await getMaterialPermissionActor(session) + if (!(await canManageMaterial(material, actor))) { + return NextResponse.json({ error: '您没有权限操作此素材' }, { status: 403 }) + } + // TODO: 接入外部AI服务(基于你提供的API),并以任务形式异步执行 return NextResponse.json({ diff --git a/docs/商业化地基排查问题清单_20260524.md b/docs/商业化地基排查问题清单_20260524.md index e122619..b5225a9 100644 --- a/docs/商业化地基排查问题清单_20260524.md +++ b/docs/商业化地基排查问题清单_20260524.md @@ -808,6 +808,27 @@ NAS 断电、容器重启、升级重启后,正在处理的视频可能永久 - 已完成:转码查询和触发接口不再匿名开放;转码恢复维护也已收紧到最高权限账号。 - P1-10 已完成第一轮持久化恢复;后续继续增强失败原因展示和管理端重试入口。 +### P1-20 AI 改图占位接口缺少素材权限校验(已修复) + +客户影响: + +AI 改图以后会消耗外部额度、CPU、存储和客户素材数据。即使当前接口还是占位实现,只要系统开关打开,未收口的接口就可能让普通登录用户拿任意素材 ID 创建任务,后续接入真实 AI 时容易把权限洞一起带进生产。 + +证据: + +- `app/api/ai/edit/route.ts`:现在会先确认 AI 改图已启用,再读取未删除素材,并复用统一素材管理范围;素材本人、个人归属人、最高权限账号和管理部门范围内管理员可操作,其他用户返回 `403`。 +- `app/api/ai/edit/route.ts`:素材不存在或已软删除时返回 `404`,不会继续创建占位任务。 +- `app/api/ai/edit/__tests__/route.test.ts`:新增测试覆盖未登录、功能未启用、素材不存在、普通用户无权操作他人素材、素材本人可创建占位任务。 + +验证: + +- `npm test -- --runTestsByPath "app/api/ai/edit/__tests__/route.test.ts" --runInBand` + +建议动作: + +- 已完成:AI 改图占位入口先按素材管理权限收口。 +- 后续接入真实 AI 服务前,还要增加许可证开关、任务持久化、外部调用失败重试、额度/并发限制和素材出域审计。 + ## 5. P2 问题 ### P2-01 监控页普通登录用户可进入(已修复)