Skip to content

Commit 9b7b641

Browse files
committed
fix(filesystem): name a broken symlink instead of blaming its parent
validatePath on a symlink with nothing at the other end reported "Parent directory does not exist: <dir>" for a directory that is plainly there. resolveUnicodeEquivalentPath lets the ENOENT from fs.realpath escape bare, and validatePath's catch reads any ENOENT as a missing parent. Naming the symlink needs the catch inside the equivalent-path walk, where the failing entry is known. Only ENOENT is renamed; the realpath error is attached as the cause, the way the Parent directory error below it already does. The characterization test for this case asserted the old message. Its name and assertion now follow the fix; the security assertion in it, that nothing is created outside the allowed directory, is unchanged. Added a guard that a symlink cycle still reports ELOOP, so a later, broader catch cannot rename every realpath failure into a broken symlink. Signed-off-by: Dmitry Voropaev <[email protected]>
1 parent 6d613ec commit 9b7b641

4 files changed

Lines changed: 77 additions & 5 deletions

File tree

‎.changeset/brave-pianos-listen.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-filesystem": patch
3+
---
4+
5+
Name a broken symlink in the error message instead of reporting its parent directory as missing.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import fs from "fs/promises";
2+
import os from "os";
3+
import path from "path";
4+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
5+
import { validatePath } from "../lib.js";
6+
7+
describe("a symlink with no target", () => {
8+
let testDirectory: string;
9+
let allowedDirs: string[];
10+
11+
beforeEach(async () => {
12+
testDirectory = await fs.mkdtemp(
13+
path.join(os.tmpdir(), "mcp-broken-symlink-"),
14+
);
15+
allowedDirs = [testDirectory];
16+
});
17+
18+
afterEach(async () => {
19+
await fs.rm(testDirectory, { recursive: true, force: true });
20+
});
21+
22+
it("is named as such, not as a missing parent directory", async () => {
23+
const link = path.join(testDirectory, "link.txt");
24+
await fs.symlink(path.join(testDirectory, "gone.txt"), link);
25+
26+
await expect(validatePath(link, allowedDirs)).rejects.toThrow(
27+
/Broken symlink/,
28+
);
29+
// the parent is right there, so the old message pointed at the wrong thing
30+
await expect(fs.stat(testDirectory)).resolves.toBeDefined();
31+
});
32+
33+
// Windows needs a privilege to create a symlink at all, and a self-referencing
34+
// one is not reachable there the same way.
35+
it.skipIf(process.platform === "win32")(
36+
"leaves a realpath failure other than ENOENT alone",
37+
async () => {
38+
const loop = path.join(testDirectory, "loop");
39+
await fs.symlink(loop, loop);
40+
41+
// A cycle fails realpath with ELOOP, not ENOENT, so validatePath throws
42+
// before the equivalent-path walk runs at all. The guard is against a
43+
// broader catch there that would rename every realpath failure.
44+
await expect(validatePath(loop, allowedDirs)).rejects.toThrow(/^ELOOP/);
45+
},
46+
);
47+
48+
it("does not change what a genuinely missing path reports", async () => {
49+
const missing = path.join(testDirectory, "nope", "file.txt");
50+
51+
// resolveUnicodeEquivalentPath returns the tail so create_directory can
52+
// mkdir -p it; that is unrelated to the broken-symlink case.
53+
await expect(validatePath(missing, allowedDirs)).resolves.toBe(
54+
path.join(await fs.realpath(testDirectory), "nope", "file.txt"),
55+
);
56+
});
57+
});

‎src/filesystem/__tests__/server-paths.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ describe("symlinks", () => {
199199
expect(await fs.readdir(outside)).toEqual(["secret.txt"]);
200200
});
201201

202-
it("refuses to write through a dangling symlink, reporting a missing parent", async () => {
202+
it("refuses to write through a dangling symlink, naming the symlink", async () => {
203203
const link = path.join(dir, "dangling");
204204
await fs.symlink(path.join(outside, "not-yet"), link);
205205
const result = await call(client, "write_file", {
206206
path: link,
207207
content: "x",
208208
});
209-
expect(textOf(result)).toBe(`Parent directory does not exist: ${dir}`);
209+
expect(textOf(result)).toBe(`Broken symlink: ${link}`);
210210
await expect(fs.stat(path.join(outside, "not-yet"))).rejects.toThrow();
211211
});
212212

‎src/filesystem/lib.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,19 @@ async function resolveUnicodeEquivalentPath(
175175
return path.join(currentPath, ...relativeParts.slice(index));
176176
}
177177

178-
currentPath = await fs.realpath(
179-
path.join(currentPath, equivalentMatches[0]),
180-
);
178+
const matchedPath = path.join(currentPath, equivalentMatches[0]);
179+
try {
180+
currentPath = await fs.realpath(matchedPath);
181+
} catch (error) {
182+
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
183+
// The entry is in the directory listing but realpath cannot follow it,
184+
// so it is a symlink with nothing at the other end. Name that. Left as
185+
// a bare ENOENT it reaches validatePath's catch, which reports a
186+
// missing parent directory for a directory that is plainly there.
187+
throw new Error(`Broken symlink: ${matchedPath}`, { cause: error });
188+
}
189+
throw error;
190+
}
181191
if (
182192
!isPathWithinAllowedDirectories(
183193
normalizePath(currentPath),

0 commit comments

Comments
 (0)