Skip to content

Commit fd4dff7

Browse files
cliffhallhe-yufengclaude
committed
fix(filesystem): admit paths under a UNC share root allowed directory
path.resolve keeps the trailing separator on a UNC share root (\\server\share\), as it does on a drive root, and the prefix check in isPathWithinAllowedDirectories appended another, comparing against \\server\share\\, which nothing matches. Every path below an allowed share root was refused. Append the separator only when it is missing. The #3527 pinning test in win32.test.ts now asserts the correct behavior, with cases for a share given without a trailing separator, sibling and other shares, another server, and .. clamped at the share. Fix as proposed in #4219 by @he-yufeng (closed unmerged); root-cause analysis in #3527 by @gdimas38-eng. Co-authored-by: Yufeng He <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 571e2b3 commit fd4dff7

3 files changed

Lines changed: 66 additions & 8 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, a UNC share root given as an allowed directory (`\\server\share` or `\\server\share\`) now admits the files and subdirectories under it. Before, only the share root itself was accessible and every path below it was refused. Sibling shares such as `\\server\share-evil` are still refused.

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

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Windows path handling, characterized on any host (#4854). The `path` module
22
// is replaced by `path.win32` and `process.platform` reads "win32", so the
33
// Windows-only branches of path-utils.ts and path-validation.ts run here:
4-
// drive roots, bare drive letters, backslash conversion and UNC shares. #3527
5-
// (a UNC share as the allowed root refuses its own subdirectories) is pinned
6-
// as it stands.
4+
// drive roots, bare drive letters, backslash conversion and UNC shares.
75

86
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
97

@@ -85,14 +83,61 @@ describe("isPathWithinAllowedDirectories on win32", () => {
8583
).toBe(true);
8684
});
8785

88-
// KNOWN BUG #3527: pins current (wrong) behavior; the fix changes this assertion.
89-
// #3527: path.resolve keeps a UNC root's trailing backslash, and the prefix
90-
// check appends another, so nothing below the share matches.
91-
it("refuses a subdirectory of a UNC share allowed directory (#3527)", () => {
86+
// #3527: path.resolve keeps a UNC share root's trailing backslash, so the
87+
// prefix check must not append a second one.
88+
it("accepts a subdirectory of a UNC share allowed directory (#3527)", () => {
9289
expect(
9390
isPathWithinAllowedDirectories("\\\\server\\share\\sub", [
9491
"\\\\server\\share\\",
9592
]),
93+
).toBe(true);
94+
});
95+
96+
it("accepts a deep path under a UNC share given without a trailing backslash", () => {
97+
expect(
98+
isPathWithinAllowedDirectories("\\\\server\\share\\a b\\.c\\f.txt", [
99+
"\\\\server\\share",
100+
]),
101+
).toBe(true);
102+
});
103+
104+
it("refuses a sibling share whose name extends the allowed share's", () => {
105+
expect(
106+
isPathWithinAllowedDirectories("\\\\server\\share-evil\\x", [
107+
"\\\\server\\share\\",
108+
]),
109+
).toBe(false);
110+
expect(
111+
isPathWithinAllowedDirectories("\\\\server\\share-evil", [
112+
"\\\\server\\share",
113+
]),
114+
).toBe(false);
115+
});
116+
117+
it("refuses another share, and the same share on another server", () => {
118+
expect(
119+
isPathWithinAllowedDirectories("\\\\server\\other\\x", [
120+
"\\\\server\\share\\",
121+
]),
122+
).toBe(false);
123+
expect(
124+
isPathWithinAllowedDirectories("\\\\server2\\share\\x", [
125+
"\\\\server\\share\\",
126+
]),
127+
).toBe(false);
128+
});
129+
130+
it("keeps a .. that climbs past a UNC share root inside that share", () => {
131+
// Node clamps .. at the share root, so this resolves to \\server\share\other.
132+
expect(
133+
isPathWithinAllowedDirectories("\\\\server\\share\\..\\other\\x", [
134+
"\\\\server\\share\\",
135+
]),
136+
).toBe(true);
137+
expect(
138+
isPathWithinAllowedDirectories("\\\\server\\share\\sub\\..\\..\\x", [
139+
"\\\\server\\share\\sub",
140+
]),
96141
).toBe(false);
97142
});
98143
});

‎src/filesystem/path-validation.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ export function isPathWithinAllowedDirectories(
9393
);
9494
}
9595

96-
return normalizedPath.startsWith(normalizedDir + path.sep);
96+
// path.resolve strips a trailing separator except from a root, and a UNC
97+
// share root (\\server\share\) keeps one, so appending another would give
98+
// \\server\share\\, a prefix nothing matches (#3527). Append one only when
99+
// it is missing; the boundary stays at a separator either way, so a
100+
// sibling such as \\server\share-evil still does not match.
101+
const dirWithSep = normalizedDir.endsWith(path.sep)
102+
? normalizedDir
103+
: normalizedDir + path.sep;
104+
return normalizedPath.startsWith(dirWithSep);
97105
});
98106
}

0 commit comments

Comments
 (0)