Skip to content

Commit 9192ea3

Browse files
cliffhallclaude
andcommitted
fix(filesystem): show an unstattable entry's size as unknown
list_directory_with_sizes substituted a size of 0 (and an epoch mtime, never shown) for an entry it could not stat, so an unreadable entry, a dangling symlink or a symlink leaving the allowed directories was listed as an empty file and counted in the combined size. Show such an entry's size as "unknown", sort it last by size, leave it out of the combined size, and say how many files the total excludes. Closes #5190 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 446be48 commit 9192ea3

4 files changed

Lines changed: 64 additions & 13 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+
`list_directory_with_sizes` no longer reports an entry it could not stat (unreadable, a dangling symlink, removed mid-listing, or a symlink leaving the allowed directories) as `0 B`. Such an entry is shown with size `unknown`, sorts last by size, and is left out of the combined size, which notes how many files it excludes.

‎src/filesystem/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ The server's directory access control follows this flow:
154154
- `sortBy` (string, optional): Sort entries by "name" or "size" (default: "name")
155155
- Returns detailed listing with file sizes and summary statistics
156156
- Shows total files, directories, and combined size
157+
- An entry that cannot be stat'd (unreadable, a dangling symlink, or a symlink leaving the allowed directories) is listed with size `unknown` and left out of the combined size
157158

158159
- **move_file**
159160
- Move or rename files and directories

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,15 +1174,38 @@ describe("list_directory_with_sizes", () => {
11741174
expect(files).toEqual(["b.txt", "a.txt"]);
11751175
});
11761176

1177-
it("lists an entry it cannot stat (a dangling symlink) as size 0", async () => {
1177+
it("lists an entry it cannot stat (a dangling symlink) as unknown size, left out of the total (#5190)", async () => {
11781178
await fs.symlink(path.join(dir, "gone"), path.join(dir, "dangling"));
11791179
const result = await call(client, "list_directory_with_sizes", {
11801180
path: dir,
11811181
});
11821182
expect(textOf(result)).toContain(
1183-
`[FILE] ${"dangling".padEnd(30)} ${"0 B".padStart(10)}`,
1183+
`[FILE] ${"dangling".padEnd(30)} ${"unknown".padStart(10)}`,
11841184
);
1185+
expect(textOf(result)).not.toContain(" 0 B");
11851186
expect(textOf(result)).toContain("Total: 3 files, 1 directories");
1187+
expect(textOf(result)).toContain(
1188+
"Combined size: 2.00 KB (excludes 1 file of unknown size)",
1189+
);
1190+
});
1191+
1192+
it("sorts entries of unknown size last when sorting by size (#5190)", async () => {
1193+
await fs.symlink(path.join(dir, "gone"), path.join(dir, "dangling"));
1194+
await fs.symlink(path.join(dir, "gone2"), path.join(dir, "dangling2"));
1195+
await fs.writeFile(path.join(dir, "empty.txt"), "");
1196+
const result = await call(client, "list_directory_with_sizes", {
1197+
path: dir,
1198+
sortBy: "size",
1199+
});
1200+
const files = textOf(result)
1201+
.split("\n")
1202+
.filter((line) => line.startsWith("[FILE]"))
1203+
.map((line) => line.split(/\s+/)[1]);
1204+
expect(files.slice(0, 3)).toEqual(["b.txt", "a.txt", "empty.txt"]);
1205+
expect(files.slice(3).sort()).toEqual(["dangling", "dangling2"]);
1206+
expect(textOf(result)).toContain(
1207+
"Combined size: 2.00 KB (excludes 2 files of unknown size)",
1208+
);
11861209
});
11871210

11881211
it("does not report the size of a symlink target outside the allowed directories", async () => {
@@ -1197,10 +1220,12 @@ describe("list_directory_with_sizes", () => {
11971220
path: dir,
11981221
});
11991222
expect(textOf(result)).toContain(
1200-
`[FILE] ${"link-out".padEnd(30)} ${"0 B".padStart(10)}`,
1223+
`[FILE] ${"link-out".padEnd(30)} ${"unknown".padStart(10)}`,
12011224
);
12021225
expect(textOf(result)).not.toContain("1.50 KB");
1203-
expect(textOf(result)).toContain("Combined size: 2.00 KB");
1226+
expect(textOf(result)).toContain(
1227+
"Combined size: 2.00 KB (excludes 1 file of unknown size)",
1228+
);
12041229
} finally {
12051230
await fs.rm(outside, { recursive: true, force: true });
12061231
}

‎src/filesystem/server.ts‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,15 @@ export function createServer(
561561
const validPath = await validatePath(args.path, allowedDirectories);
562562
const entries = await fs.readdir(validPath, { withFileTypes: true });
563563

564-
// Get detailed information for each entry
564+
// Get detailed information for each entry; size is undefined when the
565+
// entry could not be stat'd
566+
type SizedEntry = {
567+
name: string;
568+
isDirectory: boolean;
569+
size: number | undefined;
570+
};
565571
const detailedEntries = await Promise.all(
566-
entries.map(async (entry) => {
572+
entries.map(async (entry): Promise<SizedEntry> => {
567573
const entryPath = path.join(validPath, entry.name);
568574
try {
569575
// fs.stat follows symlinks, so validate each entry first: a
@@ -578,14 +584,16 @@ export function createServer(
578584
name: entry.name,
579585
isDirectory: entry.isDirectory(),
580586
size: stats.size,
581-
mtime: stats.mtime,
582587
};
583588
} catch {
589+
// The entry could not be stat'd (unreadable, a dangling symlink,
590+
// removed since readdir, or outside the allowed directories).
591+
// Its size is unknown, not 0: inventing one would list it as an
592+
// empty file and count it in the total (#5190).
584593
return {
585594
name: entry.name,
586595
isDirectory: entry.isDirectory(),
587-
size: 0,
588-
mtime: new Date(0),
596+
size: undefined,
589597
};
590598
}
591599
}),
@@ -594,7 +602,8 @@ export function createServer(
594602
// Sort entries based on sortBy parameter
595603
const sortedEntries = [...detailedEntries].sort((a, b) => {
596604
if (args.sortBy === "size") {
597-
return b.size - a.size; // Descending by size
605+
// Descending by size; an entry of unknown size sorts last
606+
return (b.size ?? -1) - (a.size ?? -1);
598607
}
599608
// Default sort by name
600609
return a.name.localeCompare(b.name);
@@ -604,22 +613,33 @@ export function createServer(
604613
const formattedEntries = sortedEntries.map(
605614
(entry) =>
606615
`${entry.isDirectory ? "[DIR]" : "[FILE]"} ${entry.name.padEnd(30)} ${
607-
entry.isDirectory ? "" : formatSize(entry.size).padStart(10)
616+
entry.isDirectory
617+
? ""
618+
: (entry.size === undefined
619+
? "unknown"
620+
: formatSize(entry.size)
621+
).padStart(10)
608622
}`,
609623
);
610624

611625
// Add summary
612626
const totalFiles = detailedEntries.filter((e) => !e.isDirectory).length;
613627
const totalDirs = detailedEntries.filter((e) => e.isDirectory).length;
614628
const totalSize = detailedEntries.reduce(
615-
(sum, entry) => sum + (entry.isDirectory ? 0 : entry.size),
629+
(sum, entry) => sum + (entry.isDirectory ? 0 : (entry.size ?? 0)),
616630
0,
617631
);
632+
const unknownFiles = detailedEntries.filter(
633+
(e) => !e.isDirectory && e.size === undefined,
634+
).length;
618635

619636
const summary = [
620637
"",
621638
`Total: ${totalFiles} files, ${totalDirs} directories`,
622-
`Combined size: ${formatSize(totalSize)}`,
639+
`Combined size: ${formatSize(totalSize)}` +
640+
(unknownFiles > 0
641+
? ` (excludes ${unknownFiles} ${unknownFiles === 1 ? "file" : "files"} of unknown size)`
642+
: ""),
623643
];
624644

625645
const text = [...formattedEntries, ...summary].join("\n");

0 commit comments

Comments
 (0)