Skip to content

Commit 7b4bdaa

Browse files
authored
Merge pull request #5458 from modelcontextprotocol/v2/feat/5444-filesystem-refuse-windows-device-names
feat(filesystem): refuse Windows device names and trailing dots/spaces in paths
2 parents c1b6e19 + 56c79bd commit 7b4bdaa

6 files changed

Lines changed: 445 additions & 9 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+
On Windows, refuse paths with a component that is a reserved device name (`CON`, `PRN`, `AUX`, `NUL`, `COM1`-`COM9`, `LPT1`-`LPT9`, in any case and with or without an extension) or that ends in a dot or a space (`file.txt.`, `file.txt `), with "Access denied - Windows reserved device names ... are not supported" or "Access denied - Windows path components ending in a dot or a space are not supported". Windows opens the device instead of a file in the allowed directory, and strips trailing dots and spaces so the name refers to a different file. `.` and `..` components are not affected, and POSIX hosts are unaffected.

‎src/filesystem/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ The server's directory access control follows this flow:
101101
```
102102
- Server requires at least ONE allowed directory to operate
103103
- On Windows, a path naming an NTFS alternate data stream (`file.txt:stream`, `file.txt::$DATA`) is refused with "Access denied - NTFS alternate data stream paths are not supported", even inside an allowed directory: a colon is accepted only as a drive letter's (`C:\`, `\\?\C:\`). A stream holds data that no directory listing or diff of the file shows. On macOS and Linux a colon is an ordinary file-name character and is not refused
104+
- On Windows, a path any of whose components is a reserved device name (`CON`, `PRN`, `AUX`, `NUL`, `COM1`-`COM9`, `LPT1`-`LPT9`, in any case and with or without an extension, such as `NUL.txt`) is refused with "Access denied - Windows reserved device names ... are not supported", and one any of whose components ends in a dot or a space (`file.txt.`, `dir \file.txt`) with "Access denied - Windows path components ending in a dot or a space are not supported". Windows opens the device rather than a file in the directory, and strips trailing dots and spaces so the name refers to a different file. `.` and `..` are not affected. On macOS and Linux these are ordinary file names and are not refused
104105
105106
**Note**: The server will only allow operations within directories specified either via `args` or, for a 2025-era client, via Roots.
106107

‎src/filesystem/__tests__/path-validation.test.ts‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,16 @@ describe("Path Validation", () => {
479479
expect(
480480
isPathWithinAllowedDirectories("/home/user/project/..test", allowed),
481481
).toBe(true); // Not traversal
482-
expect(
483-
isPathWithinAllowedDirectories("/home/user/project/test..", allowed),
484-
).toBe(true); // Not traversal
482+
// On Windows a trailing dot is refused instead (#5444): Win32 strips it.
483+
if (path.sep === "\\") {
484+
expect(() =>
485+
isPathWithinAllowedDirectories("/home/user/project/test..", allowed),
486+
).toThrow(/ending in a dot or a space/);
487+
} else {
488+
expect(
489+
isPathWithinAllowedDirectories("/home/user/project/test..", allowed),
490+
).toBe(true); // Not traversal
491+
}
485492
expect(
486493
isPathWithinAllowedDirectories("/home/user/project/te..st", allowed),
487494
).toBe(true); // Not traversal
@@ -716,12 +723,18 @@ describe("Path Validation", () => {
716723
expect(
717724
isPathWithinAllowedDirectories("/home/user/project/..", allowed),
718725
).toBe(false); // This normalizes to parent
719-
expect(
720-
isPathWithinAllowedDirectories("/home/user/project/...", allowed),
721-
).toBe(true); // Three dots is a valid filename
722-
expect(
723-
isPathWithinAllowedDirectories("/home/user/project/....", allowed),
724-
).toBe(true); // Four dots is a valid filename
726+
// Three and four dots are valid filenames on POSIX; on Windows they end
727+
// in a dot, which Win32 strips, so they are refused (#5444).
728+
for (const name of ["...", "...."]) {
729+
const p = `/home/user/project/${name}`;
730+
if (path.sep === "\\") {
731+
expect(() => isPathWithinAllowedDirectories(p, allowed)).toThrow(
732+
/ending in a dot or a space/,
733+
);
734+
} else {
735+
expect(isPathWithinAllowedDirectories(p, allowed)).toBe(true);
736+
}
737+
}
725738
});
726739

727740
it("handles UNC paths on Windows", () => {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// The device-name and trailing dot/space refusals (#5444) on the real host,
2+
// with no path simulation. On Windows (the CI Windows leg) the tools refuse
3+
// a device name and a trailing-dot name and write nothing. On POSIX those are
4+
// ordinary, distinct file names, so they must keep working: the refusal is
5+
// Windows-only.
6+
7+
import fs from "fs/promises";
8+
import path from "path";
9+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
10+
import {
11+
call,
12+
connect,
13+
makeTempDir,
14+
quietStderr,
15+
textOf,
16+
type Connected,
17+
} from "./helpers.js";
18+
19+
const DEVICE = /Windows reserved device names .* are not supported/;
20+
const TRAILING = /ending in a dot or a space are not supported/;
21+
22+
let dir: string;
23+
let conn: Connected;
24+
25+
beforeEach(async () => {
26+
quietStderr();
27+
dir = await makeTempDir("mcp-fs-devnames-");
28+
conn = await connect([dir]);
29+
});
30+
31+
afterEach(async () => {
32+
vi.restoreAllMocks();
33+
await conn.close();
34+
await fs.rm(dir, { recursive: true, force: true });
35+
});
36+
37+
describe.runIf(process.platform === "win32")("on Windows", () => {
38+
it("refuses a device name and a trailing dot or space", async () => {
39+
const write = await call(conn.client, "write_file", {
40+
path: path.join(dir, "NUL.txt"),
41+
content: "payload",
42+
});
43+
expect(write.isError).toBe(true);
44+
expect(textOf(write)).toMatch(DEVICE);
45+
46+
await fs.writeFile(path.join(dir, "notes.txt"), "visible");
47+
const dotted = await call(conn.client, "write_file", {
48+
path: `${path.join(dir, "notes.txt")}.`,
49+
content: "overwritten",
50+
});
51+
expect(dotted.isError).toBe(true);
52+
expect(textOf(dotted)).toMatch(TRAILING);
53+
expect(await fs.readFile(path.join(dir, "notes.txt"), "utf8")).toBe(
54+
"visible",
55+
);
56+
57+
const spaced = await call(conn.client, "read_text_file", {
58+
path: `${path.join(dir, "notes.txt")} `,
59+
});
60+
expect(spaced.isError).toBe(true);
61+
expect(textOf(spaced)).toMatch(TRAILING);
62+
});
63+
});
64+
65+
describe.skipIf(process.platform === "win32")("on POSIX", () => {
66+
it.each(["NUL", "con.txt", "COM1", "trailing.", "trailing "])(
67+
"still writes and reads a file named %j",
68+
async (name) => {
69+
const file = path.join(dir, name);
70+
const write = await call(conn.client, "write_file", {
71+
path: file,
72+
content: `plain ${name}`,
73+
});
74+
expect(write.isError).toBeFalsy();
75+
expect(await fs.readFile(file, "utf8")).toBe(`plain ${name}`);
76+
77+
const read = await call(conn.client, "read_text_file", { path: file });
78+
expect(read.isError).toBeFalsy();
79+
expect(textOf(read)).toBe(`plain ${name}`);
80+
},
81+
);
82+
});

0 commit comments

Comments
 (0)