|
| 1 | +// A write whose parent directory is missing reports the documented |
| 2 | +// "Parent directory does not exist: <path>" error (#5432), never the raw |
| 3 | +// chdir error of the pinned write, which named chdir (an implementation |
| 4 | +// detail) and the server's own working directory (wherever it was launched, |
| 5 | +// possibly outside the allowed directories). The protocol-level tests drive |
| 6 | +// each pinned write through a client; the unit tests below them cover the |
| 7 | +// error mapping's other branches in pinned-directory.ts directly. |
| 8 | + |
| 9 | +import fs from "fs/promises"; |
| 10 | +import fsSync from "fs"; |
| 11 | +import path from "path"; |
| 12 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 13 | +import type { Client } from "@modelcontextprotocol/client"; |
| 14 | +import { createFileInDirectory } from "../pinned-directory.js"; |
| 15 | +import { |
| 16 | + call, |
| 17 | + connect, |
| 18 | + makeTempDir, |
| 19 | + quietStderr, |
| 20 | + textOf, |
| 21 | + type Connected, |
| 22 | +} from "./helpers.js"; |
| 23 | + |
| 24 | +let dir: string; |
| 25 | +let cwd: string; |
| 26 | +let conn: Connected; |
| 27 | +let client: Client; |
| 28 | + |
| 29 | +beforeEach(async () => { |
| 30 | + quietStderr(); |
| 31 | + cwd = process.cwd(); |
| 32 | + dir = await makeTempDir("mcp-fs-missing-parent-"); |
| 33 | + conn = await connect([dir]); |
| 34 | + client = conn.client; |
| 35 | +}); |
| 36 | + |
| 37 | +afterEach(async () => { |
| 38 | + vi.restoreAllMocks(); |
| 39 | + process.chdir(cwd); |
| 40 | + await conn.close(); |
| 41 | + await fs.rm(dir, { recursive: true, force: true }); |
| 42 | +}); |
| 43 | + |
| 44 | +/** The error text must not leak the server's working directory or chdir. */ |
| 45 | +function expectNoLeak(text: string): void { |
| 46 | + expect(text).not.toContain(cwd); |
| 47 | + expect(text).not.toMatch(/chdir/); |
| 48 | +} |
| 49 | + |
| 50 | +describe("a write into a missing directory (#5432)", () => { |
| 51 | + it("write_file reports the missing parent", async () => { |
| 52 | + const missing = path.join(dir, "nope"); |
| 53 | + const result = await call(client, "write_file", { |
| 54 | + path: path.join(missing, "x.txt"), |
| 55 | + content: "z", |
| 56 | + }); |
| 57 | + expect(result.isError).toBe(true); |
| 58 | + expect(textOf(result)).toBe(`Parent directory does not exist: ${missing}`); |
| 59 | + expect(fsSync.existsSync(missing)).toBe(false); |
| 60 | + }); |
| 61 | + |
| 62 | + it("write_file reports the missing parent several levels down", async () => { |
| 63 | + const missing = path.join(dir, "no", "such"); |
| 64 | + const result = await call(client, "write_file", { |
| 65 | + path: path.join(missing, "file.txt"), |
| 66 | + content: "z", |
| 67 | + }); |
| 68 | + expect(result.isError).toBe(true); |
| 69 | + expect(textOf(result)).toBe(`Parent directory does not exist: ${missing}`); |
| 70 | + }); |
| 71 | + |
| 72 | + it("append_file reports the missing parent", async () => { |
| 73 | + const missing = path.join(dir, "nope"); |
| 74 | + const result = await call(client, "append_file", { |
| 75 | + path: path.join(missing, "x.txt"), |
| 76 | + content: "z", |
| 77 | + }); |
| 78 | + expect(result.isError).toBe(true); |
| 79 | + expect(textOf(result)).toBe(`Parent directory does not exist: ${missing}`); |
| 80 | + expect(fsSync.existsSync(missing)).toBe(false); |
| 81 | + }); |
| 82 | + |
| 83 | + it("move_file reports the destination's missing parent", async () => { |
| 84 | + const source = path.join(dir, "a.txt"); |
| 85 | + await fs.writeFile(source, "a"); |
| 86 | + const missing = path.join(dir, "nope"); |
| 87 | + const result = await call(client, "move_file", { |
| 88 | + source, |
| 89 | + destination: path.join(missing, "a.txt"), |
| 90 | + }); |
| 91 | + expect(result.isError).toBe(true); |
| 92 | + expect(textOf(result)).toBe(`Parent directory does not exist: ${missing}`); |
| 93 | + expect(await fs.readFile(source, "utf-8")).toBe("a"); |
| 94 | + }); |
| 95 | + |
| 96 | + it("copy_file reports the destination's missing parent", async () => { |
| 97 | + const source = path.join(dir, "a.txt"); |
| 98 | + await fs.writeFile(source, "a"); |
| 99 | + const missing = path.join(dir, "nope"); |
| 100 | + const result = await call(client, "copy_file", { |
| 101 | + source, |
| 102 | + destination: path.join(missing, "a.txt"), |
| 103 | + }); |
| 104 | + expect(result.isError).toBe(true); |
| 105 | + expect(textOf(result)).toBe(`Parent directory does not exist: ${missing}`); |
| 106 | + expect(fsSync.existsSync(missing)).toBe(false); |
| 107 | + }); |
| 108 | + |
| 109 | + it("edit_file on a file in a missing directory leaks nothing", async () => { |
| 110 | + const result = await call(client, "edit_file", { |
| 111 | + path: path.join(dir, "nope", "x.txt"), |
| 112 | + edits: [{ oldText: "a", newText: "b" }], |
| 113 | + }); |
| 114 | + expect(result.isError).toBe(true); |
| 115 | + expectNoLeak(textOf(result)); |
| 116 | + }); |
| 117 | + |
| 118 | + it("create_directory still creates missing parents, and leaks nothing on a file in the way", async () => { |
| 119 | + const created = await call(client, "create_directory", { |
| 120 | + path: path.join(dir, "a", "b"), |
| 121 | + }); |
| 122 | + expect(created.isError).toBeFalsy(); |
| 123 | + await fs.writeFile(path.join(dir, "file"), ""); |
| 124 | + const result = await call(client, "create_directory", { |
| 125 | + path: path.join(dir, "file", "below"), |
| 126 | + }); |
| 127 | + expect(result.isError).toBe(true); |
| 128 | + expectNoLeak(textOf(result)); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +describe("the pinned write's error for a directory it cannot enter", () => { |
| 133 | + it("reports a file in place of the parent as a missing parent", async () => { |
| 134 | + await fs.writeFile(path.join(dir, "file"), ""); |
| 135 | + const parent = path.join(dir, "file"); |
| 136 | + await expect( |
| 137 | + createFileInDirectory(path.join(parent, "x.txt"), "z"), |
| 138 | + ).rejects.toThrow( |
| 139 | + expect.objectContaining({ |
| 140 | + message: `Parent directory does not exist: ${parent}`, |
| 141 | + code: "ENOTDIR", |
| 142 | + }), |
| 143 | + ); |
| 144 | + }); |
| 145 | + |
| 146 | + it("keeps any other failure's code and names only the directory", async () => { |
| 147 | + const realChdir = process.chdir.bind(process); |
| 148 | + vi.spyOn(process, "chdir").mockImplementation((directory) => { |
| 149 | + if (directory === dir) { |
| 150 | + throw Object.assign( |
| 151 | + new Error(`EACCES: permission denied, chdir '${cwd}' -> '${dir}'`), |
| 152 | + { code: "EACCES" }, |
| 153 | + ); |
| 154 | + } |
| 155 | + realChdir(directory); |
| 156 | + }); |
| 157 | + await expect( |
| 158 | + createFileInDirectory(path.join(dir, "x.txt"), "z"), |
| 159 | + ).rejects.toThrow( |
| 160 | + expect.objectContaining({ |
| 161 | + message: `EACCES: cannot enter directory: ${dir}`, |
| 162 | + code: "EACCES", |
| 163 | + }), |
| 164 | + ); |
| 165 | + }); |
| 166 | + |
| 167 | + it("reports a failure with no code without leaking its message", async () => { |
| 168 | + const realChdir = process.chdir.bind(process); |
| 169 | + vi.spyOn(process, "chdir").mockImplementation((directory) => { |
| 170 | + if (directory === dir) { |
| 171 | + throw new Error(`chdir '${cwd}' -> '${dir}'`); |
| 172 | + } |
| 173 | + realChdir(directory); |
| 174 | + }); |
| 175 | + await expect( |
| 176 | + createFileInDirectory(path.join(dir, "x.txt"), "z"), |
| 177 | + ).rejects.toThrow(`Error: cannot enter directory: ${dir}`); |
| 178 | + }); |
| 179 | +}); |
0 commit comments