Skip to content

Commit f3f636d

Browse files
cliffhallclaude
andcommitted
fix(filesystem): address GHSA-p9cv-8g75-4xcg
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent b71b3da commit f3f636d

11 files changed

Lines changed: 614 additions & 115 deletions
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+
Anchor read tools to the validated file, as write tools are. A FIFO, socket or device is refused rather than read.

‎src/filesystem/README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,14 @@ Files inside the allowed directories are also [resources](https://modelcontextpr
153153
- `tail` (non-negative integer, optional): Last N lines; `0` returns no lines
154154
- Always treats the file as UTF-8 text regardless of extension
155155
- Cannot specify both `head` and `tail` simultaneously
156+
- Reads only a regular file: a FIFO, socket or device is refused rather
157+
than read, as it is by every tool and by `resources/read`
156158
157159
- **read_media_file**
158160
- Read a file and return it as a base64-encoded content block with its MIME type
159161
- Inputs:
160162
- `path` (string)
161-
- Streams the file and returns base64 data with the corresponding MIME type. Image and
163+
- Reads the file and returns base64 data with the corresponding MIME type. Image and
162164
audio files are returned as `image`/`audio` content; any other file type is returned as
163165
an embedded `resource` (a valid MCP content block for arbitrary binary data)
164166

‎src/filesystem/__tests__/file-resources.test.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// the roots barrier on 2025-11-25, the no-directories error on 2026-07-28).
66

77
import fs from "fs/promises";
8+
import fsSync from "fs";
89
import path from "path";
910
import { pathToFileURL } from "url";
1011
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -216,9 +217,14 @@ describe("resources/read", () => {
216217
const file = path.join(allowed, "locked.txt");
217218
await fs.writeFile(file, "x");
218219
const client = await connectTo([allowed]);
219-
vi.spyOn(fs, "readFile").mockRejectedValueOnce(
220-
Object.assign(new Error("permission denied"), { code: "EACCES" }),
221-
);
220+
// The read goes through the descriptor the anchored open returned.
221+
vi.spyOn(fsSync, "readFile").mockImplementationOnce(((
222+
_fd: number,
223+
callback: (error: Error) => void,
224+
) =>
225+
callback(
226+
Object.assign(new Error("permission denied"), { code: "EACCES" }),
227+
)) as typeof fsSync.readFile);
222228
const error = await readError(client, uriOf(file));
223229
expect(error.code).toBe(-32603);
224230
expect(error.message).toContain("permission denied");

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

Lines changed: 67 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
vi,
88
type Mock,
99
} from "vitest";
10-
import fs, { type FileHandle } from "fs/promises";
10+
import fs from "fs/promises";
1111
import type { BigIntStats, Dirent, PathLike, Stats } from "fs";
1212
import path from "path";
1313
import {
@@ -32,13 +32,17 @@ import {
3232
headFile,
3333
} from "../lib.js";
3434
import * as pinned from "../pinned-directory.js";
35+
import type { ReadableFile } from "../pinned-directory.js";
3536

3637
// Mock fs module
3738
vi.mock("fs/promises");
3839
// The pinned-directory writes act on the real filesystem; their own suite
3940
// covers them, so here only what lib.ts hands them is checked.
4041
vi.mock("../pinned-directory.js");
4142
const mockPinned = vi.mocked(pinned);
43+
// What the anchored read (readFileInDirectory) returns for the file's text.
44+
const mockReadFile = (text: string) =>
45+
mockPinned.readFileInDirectory.mockResolvedValue(Buffer.from(text));
4246
const mockFs = vi.mocked(fs);
4347
// fs.readdir is overloaded and vi.mocked types the mock against its last
4448
// (Buffer) overload; the code under test calls the withFileTypes overload.
@@ -68,12 +72,10 @@ interface MockFileHandle {
6872
close: Mock;
6973
}
7074

71-
function asFileHandle(handle: MockFileHandle): FileHandle {
72-
// Double cast is safe here: tailFile/headFile only ever call read() and
73-
// close() on the handle, which this test double provides; FileHandle's
74-
// overloaded read() signatures cannot be matched by a vi.fn() mock, so no
75-
// single cast or structural type relates the two.
76-
return handle as unknown as FileHandle;
75+
function asFileHandle(handle: MockFileHandle, size: number = 0): ReadableFile {
76+
// What openFileForReading hands tailFile/headFile: the opened file's size,
77+
// and read() and close() on its descriptor.
78+
return { size, read: handle.read, close: handle.close };
7779
}
7880

7981
function createMockFileHandle(content: Buffer) {
@@ -566,7 +568,7 @@ describe("Lib Functions", () => {
566568
nlink: 2,
567569
};
568570

569-
mockFs.stat.mockResolvedValueOnce(mockStats as Stats);
571+
mockPinned.statInDirectory.mockReturnValueOnce(mockStats as Stats);
570572

571573
const result = await getFileStats("/test/file.txt");
572574

@@ -593,7 +595,7 @@ describe("Lib Functions", () => {
593595
mode: 0o755,
594596
};
595597

596-
mockFs.stat.mockResolvedValueOnce(mockStats as Stats);
598+
mockPinned.statInDirectory.mockReturnValueOnce(mockStats as Stats);
597599

598600
const result = await getFileStats("/test/dir");
599601

@@ -605,21 +607,26 @@ describe("Lib Functions", () => {
605607

606608
describe("readFileContent", () => {
607609
it("reads file with default encoding", async () => {
608-
mockFs.readFile.mockResolvedValueOnce("file content");
610+
mockPinned.readFileInDirectory.mockResolvedValueOnce(
611+
Buffer.from("file content"),
612+
);
609613

610614
const result = await readFileContent("/test/file.txt");
611615

612616
expect(result).toBe("file content");
613-
expect(mockFs.readFile).toHaveBeenCalledWith("/test/file.txt", "utf-8");
617+
expect(mockPinned.readFileInDirectory).toHaveBeenCalledWith(
618+
"/test/file.txt",
619+
);
614620
});
615621

616622
it("reads file with custom encoding", async () => {
617-
mockFs.readFile.mockResolvedValueOnce("file content");
623+
mockPinned.readFileInDirectory.mockResolvedValueOnce(
624+
Buffer.from("file content"),
625+
);
618626

619-
const result = await readFileContent("/test/file.txt", "ascii");
627+
const result = await readFileContent("/test/file.txt", "base64");
620628

621-
expect(result).toBe("file content");
622-
expect(mockFs.readFile).toHaveBeenCalledWith("/test/file.txt", "ascii");
629+
expect(result).toBe(Buffer.from("file content").toString("base64"));
623630
});
624631
});
625632

@@ -873,7 +880,7 @@ describe("Lib Functions", () => {
873880
describe("File Editing Functions", () => {
874881
describe("applyFileEdits", () => {
875882
beforeEach(() => {
876-
mockFs.readFile.mockResolvedValue("line1\nline2\nline3\n");
883+
mockReadFile("line1\nline2\nline3\n");
877884
mockFs.lstat.mockResolvedValue(REGULAR_FILE);
878885
});
879886

@@ -937,7 +944,7 @@ describe("Lib Functions", () => {
937944
});
938945

939946
it("handles whitespace-flexible matching", async () => {
940-
mockFs.readFile.mockResolvedValue(" line1\n line2\n line3\n");
947+
mockReadFile(" line1\n line2\n line3\n");
941948

942949
const edits = [{ oldText: "line2", newText: "modified line2" }];
943950

@@ -982,7 +989,7 @@ describe("Lib Functions", () => {
982989
});
983990

984991
it("handles complex multi-line edits with indentation", async () => {
985-
mockFs.readFile.mockResolvedValue(
992+
mockReadFile(
986993
'function test() {\n console.log("hello");\n return true;\n}',
987994
);
988995

@@ -1004,9 +1011,7 @@ describe("Lib Functions", () => {
10041011
});
10051012

10061013
it("handles edits with different indentation patterns", async () => {
1007-
mockFs.readFile.mockResolvedValue(
1008-
" if (condition) {\n doSomething();\n }",
1009-
);
1014+
mockReadFile(" if (condition) {\n doSomething();\n }");
10101015

10111016
const edits = [
10121017
{
@@ -1025,7 +1030,7 @@ describe("Lib Functions", () => {
10251030
});
10261031

10271032
it("handles CRLF line endings in file content", async () => {
1028-
mockFs.readFile.mockResolvedValue("line1\r\nline2\r\nline3\r\n");
1033+
mockReadFile("line1\r\nline2\r\nline3\r\n");
10291034

10301035
const edits = [{ oldText: "line2", newText: "modified line2" }];
10311036

@@ -1041,17 +1046,19 @@ describe("Lib Functions", () => {
10411046

10421047
describe("tailFile", () => {
10431048
it("handles empty files", async () => {
1044-
mockFs.stat.mockResolvedValue({ size: 0 } as Stats);
1049+
const mockFileHandle = createMockFileHandle(Buffer.alloc(0));
1050+
mockPinned.openFileForReading.mockResolvedValue(
1051+
asFileHandle(mockFileHandle, 0),
1052+
);
10451053

10461054
const result = await tailFile("/test/empty.txt", 5);
10471055

10481056
expect(result).toBe("");
1049-
expect(mockFs.open).not.toHaveBeenCalled();
1057+
expect(mockFileHandle.read).not.toHaveBeenCalled();
1058+
expect(mockFileHandle.close).toHaveBeenCalled();
10501059
});
10511060

1052-
it("calls stat to check file size", async () => {
1053-
mockFs.stat.mockResolvedValue({ size: 100 } as Stats);
1054-
1061+
it("opens the file through its pinned directory", async () => {
10551062
// Mock file handle with proper typing
10561063
const mockFileHandle = {
10571064
read: vi.fn(),
@@ -1061,17 +1068,18 @@ describe("Lib Functions", () => {
10611068
mockFileHandle.read.mockResolvedValue({ bytesRead: 0 });
10621069
mockFileHandle.close.mockResolvedValue(undefined);
10631070

1064-
mockFs.open.mockResolvedValue(asFileHandle(mockFileHandle));
1071+
mockPinned.openFileForReading.mockResolvedValue(
1072+
asFileHandle(mockFileHandle, 100),
1073+
);
10651074

10661075
await tailFile("/test/file.txt", 2);
10671076

1068-
expect(mockFs.stat).toHaveBeenCalledWith("/test/file.txt");
1069-
expect(mockFs.open).toHaveBeenCalledWith("/test/file.txt", "r");
1077+
expect(mockPinned.openFileForReading).toHaveBeenCalledWith(
1078+
"/test/file.txt",
1079+
);
10701080
});
10711081

10721082
it("handles files with content and returns last lines", async () => {
1073-
mockFs.stat.mockResolvedValue({ size: 50 } as Stats);
1074-
10751083
const mockFileHandle = {
10761084
read: vi.fn(),
10771085
close: vi.fn(),
@@ -1086,7 +1094,9 @@ describe("Lib Functions", () => {
10861094
.mockResolvedValueOnce({ bytesRead: 0 });
10871095
mockFileHandle.close.mockResolvedValue(undefined);
10881096

1089-
mockFs.open.mockResolvedValue(asFileHandle(mockFileHandle));
1097+
mockPinned.openFileForReading.mockResolvedValue(
1098+
asFileHandle(mockFileHandle, 50),
1099+
);
10901100

10911101
await tailFile("/test/file.txt", 2);
10921102

@@ -1101,18 +1111,16 @@ describe("Lib Functions", () => {
11011111
Buffer.from("\nlast"),
11021112
]);
11031113
const mockFileHandle = createMockFileHandle(content);
1104-
1105-
mockFs.stat.mockResolvedValue({ size: content.length } as Stats);
1106-
mockFs.open.mockResolvedValue(asFileHandle(mockFileHandle));
1114+
mockPinned.openFileForReading.mockResolvedValue(
1115+
asFileHandle(mockFileHandle, content.length),
1116+
);
11071117

11081118
const result = await tailFile("/test/file.txt", 2);
11091119

11101120
expect(result).toBe(`界${"a".repeat(1017)}\nlast`);
11111121
});
11121122

11131123
it("handles read errors gracefully", async () => {
1114-
mockFs.stat.mockResolvedValue({ size: 100 } as Stats);
1115-
11161124
const mockFileHandle = {
11171125
read: vi.fn(),
11181126
close: vi.fn(),
@@ -1121,7 +1129,9 @@ describe("Lib Functions", () => {
11211129
mockFileHandle.read.mockResolvedValue({ bytesRead: 0 });
11221130
mockFileHandle.close.mockResolvedValue(undefined);
11231131

1124-
mockFs.open.mockResolvedValue(asFileHandle(mockFileHandle));
1132+
mockPinned.openFileForReading.mockResolvedValue(
1133+
asFileHandle(mockFileHandle, 100),
1134+
);
11251135

11261136
await tailFile("/test/file.txt", 5);
11271137

@@ -1140,11 +1150,15 @@ describe("Lib Functions", () => {
11401150
mockFileHandle.read.mockResolvedValue({ bytesRead: 0 });
11411151
mockFileHandle.close.mockResolvedValue(undefined);
11421152

1143-
mockFs.open.mockResolvedValue(asFileHandle(mockFileHandle));
1153+
mockPinned.openFileForReading.mockResolvedValue(
1154+
asFileHandle(mockFileHandle),
1155+
);
11441156

11451157
await headFile("/test/file.txt", 2);
11461158

1147-
expect(mockFs.open).toHaveBeenCalledWith("/test/file.txt", "r");
1159+
expect(mockPinned.openFileForReading).toHaveBeenCalledWith(
1160+
"/test/file.txt",
1161+
);
11481162
});
11491163

11501164
it("handles files with content and returns first lines", async () => {
@@ -1162,7 +1176,9 @@ describe("Lib Functions", () => {
11621176
.mockResolvedValueOnce({ bytesRead: 0 });
11631177
mockFileHandle.close.mockResolvedValue(undefined);
11641178

1165-
mockFs.open.mockResolvedValue(asFileHandle(mockFileHandle));
1179+
mockPinned.openFileForReading.mockResolvedValue(
1180+
asFileHandle(mockFileHandle),
1181+
);
11661182

11671183
await headFile("/test/file.txt", 2);
11681184

@@ -1176,7 +1192,9 @@ describe("Lib Functions", () => {
11761192
]);
11771193
const mockFileHandle = createMockFileHandle(content);
11781194

1179-
mockFs.open.mockResolvedValue(asFileHandle(mockFileHandle));
1195+
mockPinned.openFileForReading.mockResolvedValue(
1196+
asFileHandle(mockFileHandle),
1197+
);
11801198

11811199
const result = await headFile("/test/file.txt", 1);
11821200

@@ -1198,7 +1216,9 @@ describe("Lib Functions", () => {
11981216
.mockResolvedValueOnce({ bytesRead: 0 });
11991217
mockFileHandle.close.mockResolvedValue(undefined);
12001218

1201-
mockFs.open.mockResolvedValue(asFileHandle(mockFileHandle));
1219+
mockPinned.openFileForReading.mockResolvedValue(
1220+
asFileHandle(mockFileHandle),
1221+
);
12021222

12031223
await headFile("/test/file.txt", 5);
12041224

@@ -1220,7 +1240,9 @@ describe("Lib Functions", () => {
12201240
.mockResolvedValueOnce({ bytesRead: 0 });
12211241
mockFileHandle.close.mockResolvedValue(undefined);
12221242

1223-
mockFs.open.mockResolvedValue(asFileHandle(mockFileHandle));
1243+
mockPinned.openFileForReading.mockResolvedValue(
1244+
asFileHandle(mockFileHandle),
1245+
);
12241246

12251247
await headFile("/test/file.txt", 2);
12261248

0 commit comments

Comments
 (0)