找出 MCP 源码读取器里的 bug

来自 智能体指令与 MCP
Node 24 进阶 6分钟 找出 3处问题

找出这个生成式源码读取工具中的边界故障。

JavaScript
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";

const SOURCE_ROOT = "/srv/project/src";

export async function readSource(args) {
  const fullPath = resolve(SOURCE_ROOT, args.path);
  if (!fullPath.startsWith(SOURCE_ROOT)) {
    return { isError: true, message: "outside source root" };
  }
  const text = await readFile(fullPath, "utf8");
  return {
    isError: false,
    content: [{ type: "text", text }],
  };
}
在试验场中打开
报告错误