From f8a4bebb43dc3a81f284a3f5b8d28c7b64c70f96 Mon Sep 17 00:00:00 2001 From: margrop <1731827+margrop@users.noreply.github.com> Date: Fri, 4 Dec 2026 10:32:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20MCP=5FUSER=5FEMAIL?= =?UTF-8?q?=20=E9=9D=9E=E5=94=AF=E4=B8=80=E5=8C=B9=E9=85=8D=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84=E7=9B=AE=E6=A0=87=E8=B4=A6=E5=8F=B7=E9=94=99?= =?UTF-8?q?=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 邮箱配置按规范化小写精确查找,不再回退匹配其他用户的 name - 用户名命中多个启用账号时拒绝认证并要求改用邮箱 - 补充 MCP 账号匹配集成测试 - Refs: https://github.com/margrop/wrong-notebook/issues/174 --- src/__tests__/integration/mcp.test.ts | 33 +++++++++++++++++++++++++++ src/lib/mcp/auth.ts | 30 +++++++++++++++++++----- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/__tests__/integration/mcp.test.ts b/src/__tests__/integration/mcp.test.ts index c264897e..fd7e9b7c 100644 --- a/src/__tests__/integration/mcp.test.ts +++ b/src/__tests__/integration/mcp.test.ts @@ -255,6 +255,39 @@ describe('MCP 服务', () => { expect(response.status).toBe(503); }); + it('MCP_USER_EMAIL 为邮箱时规范化大小写且不回退匹配其他用户的 name', async () => { + await database.user.create({ + data: { id: 'user-2', email: 'other@example.com', name: 'MCP@EXAMPLE.COM', password: 'test-only' }, + }); + vi.stubEnv('MCP_USER_EMAIL', 'MCP@EXAMPLE.COM'); + + const init = await MCP_POST(mcpRequest( + { jsonrpc: '2.0', id: 1, method: 'initialize', params: { protocolVersion: '2025-06-18' } }, + )); + expect(init.status).toBe(200); + + const notebooks = toolJson((await callTool('list_notebooks')).result!) as { + notebooks: { id: string; role: string }[]; + }; + expect(notebooks.notebooks).toContainEqual(expect.objectContaining({ id: 'math', role: 'owner' })); + }); + + it('MCP_USER_EMAIL 按用户名命中多个启用账号时拒绝认证', async () => { + await database.user.createMany({ data: [ + { id: 'dup-1', email: 'dup1@example.com', name: 'DupName', password: 'test-only' }, + { id: 'dup-2', email: 'dup2@example.com', name: 'DupName', password: 'test-only' }, + ] }); + vi.stubEnv('MCP_USER_EMAIL', 'DupName'); + + const response = await MCP_POST(mcpRequest( + { jsonrpc: '2.0', id: 1, method: 'initialize', params: { protocolVersion: '2025-06-18' } }, + )); + + expect(response.status).toBe(401); + const body = await response.json() as { error: { message: string } }; + expect(body.error.message).toContain('multiple active usernames'); + }); + it('同一 IP 连续鉴权失败 20 次后返回 429 与 Retry-After', async () => { const ip = '203.0.113.20'; mcpAuthFailureIpLimiter.reset(ip); diff --git a/src/lib/mcp/auth.ts b/src/lib/mcp/auth.ts index 93ef5d3b..80826988 100644 --- a/src/lib/mcp/auth.ts +++ b/src/lib/mcp/auth.ts @@ -2,8 +2,11 @@ import "server-only"; import { timingSafeEqual } from "node:crypto"; import { prisma } from "@/lib/prisma"; +import { createLogger } from "@/lib/logger"; import type { User } from "@prisma/client"; +const logger = createLogger('mcp:auth'); + /** MCP 服务配置:集成密钥与固定目标账号 */ export interface McpConfig { apiKey: string; @@ -43,6 +46,7 @@ export type McpAuthResult = /** * 校验集成密钥并解析 MCP_USER_EMAIL(邮箱或用户名)对应的启用账号。 + * 邮箱按规范化小写后精确查找,不回退匹配其他用户的 name;用户名不唯一时拒绝认证。 * 未配置时返回 503;密钥错误或账号不存在/已停用时返回 401。 */ export async function authenticateMcpRequest(req: Request): Promise { @@ -60,12 +64,26 @@ export async function authenticateMcpRequest(req: Request): Promise 1) { + logger.warn({ userEmail: config.userEmail }, 'MCP_USER_EMAIL matches multiple active usernames'); + return { + ok: false, + status: 401, + message: "MCP user matches multiple active usernames; set MCP_USER_EMAIL to the account email", + }; + } + user = matches[0] ?? null; + } + if (!user) { return { ok: false, status: 401, message: "MCP user not found or disabled" }; }