mirror of
https://github.com/margrop/wrong-notebook.git
synced 2026-12-06 12:59:58 +08:00
fix: 修复 MCP_USER_EMAIL 非唯一匹配导致的目标账号错配
- 邮箱配置按规范化小写精确查找,不再回退匹配其他用户的 name - 用户名命中多个启用账号时拒绝认证并要求改用邮箱 - 补充 MCP 账号匹配集成测试 - Refs: https://github.com/margrop/wrong-notebook/issues/174
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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<McpAuthResult> {
|
||||
@@ -60,12 +64,26 @@ export async function authenticateMcpRequest(req: Request): Promise<McpAuthResul
|
||||
return { ok: false, status: 401, message: "Missing or invalid MCP API key" };
|
||||
}
|
||||
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
OR: [{ email: config.userEmail }, { name: config.userEmail }],
|
||||
isActive: true,
|
||||
},
|
||||
});
|
||||
let user: User | null;
|
||||
if (config.userEmail.includes('@')) {
|
||||
user = await prisma.user.findUnique({ where: { email: config.userEmail.toLowerCase() } });
|
||||
if (!user || !user.isActive) user = null;
|
||||
} else {
|
||||
const matches = await prisma.user.findMany({
|
||||
where: { name: config.userEmail, isActive: true },
|
||||
take: 2,
|
||||
});
|
||||
if (matches.length > 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" };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user