Skip to content

Commit d665e43

Browse files
cliffhallclaude
andcommitted
fix(filesystem): report a dangling symlink as broken, not as a missing parent
When a requested path is, or passes through, a symlink whose target does not exist, the realpath of that entry in resolveUnicodeEquivalentPath failed with ENOENT, and validatePath reported "Parent directory does not exist" for a directory that does exist. The entry is known to exist at that point (lstat found it), so an ENOENT from its realpath means its target is missing: say so, naming the symlink. The request is still refused; any other realpath failure is reported as it is. Closes #5191 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 446be48 commit d665e43

4 files changed

Lines changed: 69 additions & 5 deletions

File tree

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+
Report a dangling symlink as a broken symlink. A request through a symlink whose target does not exist is still refused, but the error now names the symlink instead of saying its parent directory, which exists, does not.

‎src/filesystem/__tests__/lib.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,24 @@ describe("Lib Functions", () => {
402402
).rejects.toThrow("EACCES lstat");
403403
});
404404

405+
// #5191: only an ENOENT from an existing entry's realpath means a
406+
// broken symlink; any other failure is reported as it is.
407+
it("reports a realpath failure other than ENOENT on an existing component as is", async () => {
408+
const base =
409+
process.platform === "win32" ? "C:\\Users\\test" : "/home/user";
410+
const enoentError = new Error("ENOENT") as NodeJS.ErrnoException;
411+
enoentError.code = "ENOENT";
412+
mockFs.realpath
413+
.mockRejectedValueOnce(enoentError)
414+
.mockResolvedValueOnce(base)
415+
.mockRejectedValueOnce(new Error("EACCES realpath"));
416+
mockReaddirNames.mockResolvedValueOnce(["link"]);
417+
418+
await expect(
419+
validatePath(path.join(base, "link", "a"), allowedDirs),
420+
).rejects.toThrow("EACCES realpath");
421+
});
422+
405423
it("rejects when no ancestor directory exists at all", async () => {
406424
const newFilePath =
407425
process.platform === "win32"

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ describe("symlinks", () => {
314314
).toBe("x");
315315
});
316316

317-
it("refuses to write under a respelled dangling symlink, reporting a missing parent", async () => {
317+
it("refuses to write under a respelled dangling symlink, naming it as broken (#5191)", async () => {
318318
await fs.symlink(
319319
path.join(outside, "not-yet"),
320320
path.join(dir, "dangling"),
@@ -324,24 +324,51 @@ describe("symlinks", () => {
324324
content: "x",
325325
});
326326
expect(textOf(result)).toBe(
327-
`Parent directory does not exist: ${path.join(dir, "DANGLING")}`,
327+
`Broken symlink: ${path.join(dir, "DANGLING")} points to a target that does not exist`,
328328
);
329329
await expect(fs.stat(path.join(outside, "not-yet"))).rejects.toThrow();
330330
});
331331
},
332332
);
333333

334-
it("refuses to write through a dangling symlink, reporting a missing parent", async () => {
334+
// #5191: the refusal is right, and the message names the symlink rather
335+
// than reporting its parent, which exists, as missing.
336+
it("refuses to write through a dangling symlink, naming it as broken (#5191)", async () => {
335337
const link = path.join(dir, "dangling");
336338
await fs.symlink(path.join(outside, "not-yet"), link);
337339
const result = await call(client, "write_file", {
338340
path: link,
339341
content: "x",
340342
});
341-
expect(textOf(result)).toBe(`Parent directory does not exist: ${dir}`);
343+
expect(textOf(result)).toBe(
344+
`Broken symlink: ${link} points to a target that does not exist`,
345+
);
342346
await expect(fs.stat(path.join(outside, "not-yet"))).rejects.toThrow();
343347
});
344348

349+
it("refuses to read a dangling symlink, naming it as broken (#5191)", async () => {
350+
const link = path.join(dir, "dangling");
351+
await fs.symlink(path.join(dir, "gone.txt"), link);
352+
const result = await read(link);
353+
expect(result.isError).toBe(true);
354+
expect(textOf(result)).toBe(
355+
`Broken symlink: ${link} points to a target that does not exist`,
356+
);
357+
});
358+
359+
it("names a dangling symlink in the middle of a path as broken (#5191)", async () => {
360+
const link = path.join(dir, "dangling-dir");
361+
await fs.symlink(path.join(dir, "gone"), link);
362+
const result = await call(client, "write_file", {
363+
path: path.join(link, "f.txt"),
364+
content: "x",
365+
});
366+
expect(textOf(result)).toBe(
367+
`Broken symlink: ${link} points to a target that does not exist`,
368+
);
369+
await expect(fs.stat(path.join(dir, "gone"))).rejects.toThrow();
370+
});
371+
345372
it("reports a realpath failure other than ENOENT as is", async () => {
346373
await fs.writeFile(path.join(dir, "file.txt"), "");
347374
const result = await read(path.join(dir, "file.txt", "child"));

‎src/filesystem/lib.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,21 @@ async function resolveUnicodeEquivalentPath(
429429
return path.join(currentPath, ...relativeParts.slice(index));
430430
}
431431

432-
currentPath = await realpathWithTimeout(candidate);
432+
try {
433+
currentPath = await realpathWithTimeout(candidate);
434+
} catch (error) {
435+
// The entry exists (lstat found it just above) but realpath cannot
436+
// follow it, so it is a symlink whose target is missing. Name it: a bare
437+
// ENOENT reaches validatePath's catch, which would report its parent,
438+
// a directory that plainly exists, as missing (#5191).
439+
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
440+
throw new Error(
441+
`Broken symlink: ${candidate} points to a target that does not exist`,
442+
{ cause: error },
443+
);
444+
}
445+
throw error;
446+
}
433447
if (
434448
!isPathWithinAllowedDirectories(
435449
normalizePathFormat(currentPath),

0 commit comments

Comments
 (0)