{"draft":null,"from":20,"to":21,"files":[{"path":"client/src/mcp.ts","added":51,"removed":3,"diff":"--- a/client/src/mcp.ts\n+++ b/client/src/mcp.ts\n@@ -9,7 +9,8 @@\n // The protocol is JSON-RPC 2.0, one message per line, with no library: the\n // client uses nothing outside Node.\n \n-import { resolve } from \"node:path\";\n+import { realpathSync } from \"node:fs\";\n+import { isAbsolute, relative, resolve, sep } from \"node:path\";\n import { clientVersion } from \"./client-version.ts\";\n import { commandTable, main } from \"./main.ts\";\n \n@@ -92,7 +93,8 @@\n \t\t\t\t\t},\n \t\t\t\t\tfolder: {\n \t\t\t\t\t\ttype: \"string\",\n-\t\t\t\t\t\tdescription: \"The abso folder to run in (default: the folder this server started in).\",\n+\t\t\t\t\t\tdescription:\n+\t\t\t\t\t\t\t\"The abso folder to run in: the folder this server started in (the default), or one inside it.\",\n \t\t\t\t\t},\n \t\t\t\t\tjson: { type: \"boolean\", description: \"Answer with the command's JSON (as --json) instead of its text.\" },\n \t\t\t\t\t...flags,\n@@ -103,6 +105,50 @@\n \t});\n }\n \n+/** A path as it really is (through links), or as given when it doesn't exist. */\n+function real(path: string): string {\n+\ttry {\n+\t\treturn realpathSync.native(path);\n+\t} catch {\n+\t\treturn path;\n+\t}\n+}\n+\n+/** Whether `path` is `start` or inside it, after links are followed. */\n+function inside(start: string, path: string): boolean {\n+\tconst rel = relative(real(start), real(path));\n+\treturn rel === \"\" || (rel !== \"..\" && !rel.startsWith(`..${sep}`) && !isAbsolute(rel));\n+}\n+\n+/**\n+ * Where a tool call runs, and what it may read (Security on 233): its folder and\n+ * every --attach file stay inside the folder the server started in. A relative\n+ * path that climbs out (..), an absolute one elsewhere, or a link out is\n+ * refused, so no tool reads a file outside the project. Answers the folder, or\n+ * why the call is refused.\n+ */\n+export function confineCall(\n+\tstart: string,\n+\tinput: Readonly<Record<string, unknown>>,\n+): { folder: string } | { refused: string } {\n+\tconst named = typeof input.folder === \"string\" && input.folder !== \"\" ? input.folder : undefined;\n+\tconst folder = named === undefined ? start : resolve(start, named);\n+\tif (!inside(start, folder))\n+\t\treturn {\n+\t\t\trefused: `folder ${JSON.stringify(named)} is outside ${start}, the folder this server started in; tools run there or below`,\n+\t\t};\n+\tif (typeof input.attach === \"string\")\n+\t\tfor (const p of input.attach\n+\t\t\t.split(\",\")\n+\t\t\t.map((x) => x.trim())\n+\t\t\t.filter((x) => x !== \"\"))\n+\t\t\tif (!inside(start, resolve(folder, p)))\n+\t\t\t\treturn {\n+\t\t\t\t\trefused: `--attach ${p} is outside ${start}, the folder this server started in; a tool attaches files from there or below`,\n+\t\t\t\t};\n+\treturn { folder };\n+}\n+\n /** The command line a tool call stands for: flags as --name=value, then the words after --. */\n export function argvFor(command: string, input: Readonly<Record<string, unknown>>): string[] {\n \tconst def = commandTable()[command]!;\n@@ -158,7 +204,9 @@\n \t\t\t\tconst command = MCP_COMMANDS.find((c) => toolName(c) === name);\n \t\t\t\tif (command === undefined) return fail(id, -32602, `no tool ${JSON.stringify(name)}; tools/list names them`);\n \t\t\t\tconst args = (params.arguments ?? {}) as Record<string, unknown>;\n-\t\t\t\tconst folder = typeof args.folder === \"string\" && args.folder !== \"\" ? resolve(cwd, args.folder) : cwd;\n+\t\t\t\tconst where = confineCall(cwd, args);\n+\t\t\t\tif (\"refused\" in where) return reply(id, { content: [{ type: \"text\", text: where.refused }], isError: true });\n+\t\t\t\tconst folder = where.folder;\n \t\t\t\tlet streamed = \"\";\n \t\t\t\tconst result = await main(argvFor(command, args), {\n \t\t\t\t\tcwd: folder,"}],"unmatched":[]}