Skip to content

Commit 40a28a8

Browse files
cliffhallclaude
andcommitted
fix(filesystem): keep a Root's advertised form verbatim, untrimmed
normalizePath trims whitespace and quotes, which are legal POSIX filename characters, so a Root advertised as 'alias ' would have allowed an unrelated sibling 'alias'. Format the advertised form with normalizePathFormat instead, and pin it with a regression test. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 1216a93 commit 40a28a8

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,32 @@ describe("initial roots", () => {
132132
}
133133
});
134134

135+
// #5183: the advertised form is kept as the client spelled it. Trimming
136+
// whitespace or quotes from it would name a different directory, here an
137+
// unrelated sibling, and allow it. POSIX only: Windows filenames cannot end
138+
// in a space.
139+
it.skipIf(process.platform === "win32")(
140+
"keep a root's advertised form verbatim, not trimmed to a sibling's name (#5183)",
141+
async () => {
142+
const link = path.join(cliDir, "alias ");
143+
await fs.symlink(rootDir, link);
144+
const sibling = path.join(cliDir, "alias");
145+
await fs.mkdir(sibling);
146+
await fs.writeFile(path.join(sibling, "secret.txt"), "not in the root");
147+
const { client } = await connectTracked([], {
148+
capabilities: ROOTS,
149+
listRoots: () => [rootOf(link)],
150+
});
151+
await vi.waitFor(async () =>
152+
expect(await allowedDirectoriesOf(client)).toEqual([link, rootDir]),
153+
);
154+
const result = await call(client, "read_text_file", {
155+
path: path.join(sibling, "secret.txt"),
156+
});
157+
expect(textOf(result)).toMatch(/^Access denied/);
158+
},
159+
);
160+
135161
// #5183 as reported: on macOS /tmp is a symlink to /private/tmp, so a Root
136162
// of file:///tmp/... was allowed only as /private/tmp/... and a request
137163
// naming /tmp/... was refused.

‎src/filesystem/roots-utils.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { promises as fs, type Stats } from "fs";
22
import path from "path";
33
import os from "os";
4-
import { normalizePath } from "./path-utils.js";
4+
import { normalizePath, normalizePathFormat } from "./path-utils.js";
55
import type { Root } from "@modelcontextprotocol/server";
66
import { fileURLToPath } from "url";
77

@@ -32,7 +32,9 @@ async function parseRootUri(rootUri: string): Promise<RootPaths | null> {
3232
const absolutePath = path.resolve(expandedPath);
3333
const resolvedPath = await fs.realpath(absolutePath);
3434
return {
35-
advertised: normalizePath(absolutePath),
35+
// normalizePathFormat, not normalizePath: trimming whitespace or quotes
36+
// from the client's spelling would name a different directory.
37+
advertised: normalizePathFormat(absolutePath),
3638
resolved: normalizePath(resolvedPath),
3739
};
3840
} catch {

0 commit comments

Comments
 (0)