Skip to content

Commit cebc963

Browse files
authored
Merge pull request #5096 from modelcontextprotocol/v2/fix/5094-release-1.0.0-cherry-pick
v1.0.0: cherry-pick the #5094 roots revocation fix onto the release branch
2 parents 8e4c8c0 + 2b74b00 commit cebc963

6 files changed

Lines changed: 112 additions & 2 deletions

File tree

‎src/filesystem/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ The first semantic version, resuming semver after the date-stamped releases (las
2121
- [#5010](https://github.com/modelcontextprotocol/servers/pull/5010) [`fd4dff7`](https://github.com/modelcontextprotocol/servers/commit/fd4dff7da877068734bee9419fa935213d66e359) [@cliffhall](https://github.com/cliffhall) - 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.
2222

2323
- [#5009](https://github.com/modelcontextprotocol/servers/pull/5009) [`f621c75`](https://github.com/modelcontextprotocol/servers/commit/f621c75a92347738f28f26c9461286764beba3c8) [@cliffhall](https://github.com/cliffhall) - Paths that differ from the names on disk only in Unicode form now resolve ([#1970](https://github.com/modelcontextprotocol/servers/issues/1970)). An NFC spelling of an allowed directory whose name is stored NFD (macOS "Capture d’écran", Japanese dakuten) is no longer refused as outside the allowed directories, and a request that spells a U+202F or U+00A0 in a name as a plain space (the macOS screenshot "Screenshot … at 2.40.40 PM.png") finds the file instead of failing with ENOENT. The exact spelling still wins when it exists, and a name matching two entries is refused as ambiguous.
24+
25+
- [#5094](https://github.com/modelcontextprotocol/servers/issues/5094) [`bea1c9a`](https://github.com/modelcontextprotocol/servers/commit/bea1c9a1bd623aea093a4408b0f71e95cd0e1d03) [@cliffhall](https://github.com/cliffhall) - A `roots/list_changed` update that leaves no valid root now revokes access once the client's roots are in force, instead of keeping the previous allowed directories, so a client that withdraws its roots withdraws the server's access too (#5094). Every path is then refused with "Access denied - no allowed directories" until the client exposes a root again. Before any root has been in force, such an update still keeps the command-line directories.

‎src/filesystem/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ The server's directory access control follows this flow:
5151
- Tool calls that arrive before the initial roots are loaded wait for them, so they are checked against the client's roots
5252
- **On runtime updates**: Client can send `notifications/roots/list_changed`
5353
- Server requests updated roots and replaces allowed directories again
54+
- Once the client's roots are in force, an update with no valid roots revokes access: every path is refused ("Access denied - no allowed directories") until the client exposes a root again. Before any root has been in force, such an update keeps the command-line directories
5455

5556
4. **Fallback Behavior** (if client doesn't support roots)
5657
- Server continues using command-line directories only

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ describe("relative paths", () => {
164164
path: "anything.txt",
165165
});
166166
expect(textOf(result)).toBe(
167-
`Access denied - path outside allowed directories: ${path.resolve(process.cwd(), "anything.txt")} not in `,
167+
`Access denied - no allowed directories: the client exposes no valid roots (${path.resolve(process.cwd(), "anything.txt")})`,
168168
);
169169
} finally {
170170
await empty.close();

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,100 @@ describe("roots/list_changed", () => {
251251
);
252252
});
253253

254+
// #5094: once the client's roots are in force, an update that leaves no
255+
// valid root revokes access instead of keeping the previous directories.
256+
it("revokes access when the client withdraws its last root (#5094)", async () => {
257+
let roots = [rootOf(rootDir)];
258+
const { client } = await connectTracked([cliDir], {
259+
capabilities: ROOTS,
260+
listRoots: () => roots,
261+
});
262+
await vi.waitFor(async () =>
263+
expect(await allowedDirectoriesOf(client)).toEqual([rootDir]),
264+
);
265+
266+
roots = [];
267+
await client.sendRootsListChanged();
268+
await vi.waitFor(async () =>
269+
expect(await allowedDirectoriesOf(client)).toEqual([]),
270+
);
271+
expect(stderr).toHaveBeenCalledWith(
272+
"No valid root directories provided by client; access revoked until it exposes a root again",
273+
);
274+
const result = await call(client, "list_directory", { path: rootDir });
275+
expect(result.isError).toBe(true);
276+
expect(textOf(result)).toMatch(
277+
/^Access denied - no allowed directories: the client exposes no valid roots/,
278+
);
279+
// The command-line directories do not come back either.
280+
expect(
281+
textOf(await call(client, "list_directory", { path: cliDir })),
282+
).toMatch(/^Access denied/);
283+
});
284+
285+
it("revokes access when every remaining root is invalid (#5094)", async () => {
286+
let roots = [rootOf(rootDir)];
287+
const { client } = await connectTracked([], {
288+
capabilities: ROOTS,
289+
listRoots: () => roots,
290+
});
291+
await vi.waitFor(async () =>
292+
expect(await allowedDirectoriesOf(client)).toEqual([rootDir]),
293+
);
294+
295+
roots = [rootOf(path.join(rootDir, "gone"))];
296+
await client.sendRootsListChanged();
297+
await vi.waitFor(async () =>
298+
expect(await allowedDirectoriesOf(client)).toEqual([]),
299+
);
300+
});
301+
302+
it("restores access when the client exposes a root again (#5094)", async () => {
303+
let roots = [rootOf(rootDir)];
304+
const { client } = await connectTracked([], {
305+
capabilities: ROOTS,
306+
listRoots: () => roots,
307+
});
308+
await vi.waitFor(async () =>
309+
expect(await allowedDirectoriesOf(client)).toEqual([rootDir]),
310+
);
311+
roots = [];
312+
await client.sendRootsListChanged();
313+
await vi.waitFor(async () =>
314+
expect(await allowedDirectoriesOf(client)).toEqual([]),
315+
);
316+
317+
roots = [rootOf(rootDir)];
318+
await client.sendRootsListChanged();
319+
await vi.waitFor(async () =>
320+
expect(await allowedDirectoriesOf(client)).toEqual([rootDir]),
321+
);
322+
});
323+
324+
it("keeps the command-line directories on an empty update before any root was in force", async () => {
325+
let roots: Root[] = [];
326+
const { client } = await connectTracked([cliDir], {
327+
capabilities: ROOTS,
328+
listRoots: () => roots,
329+
});
330+
await vi.waitFor(() =>
331+
expect(stderr).toHaveBeenCalledWith(
332+
"No valid root directories provided by client",
333+
),
334+
);
335+
336+
roots = [];
337+
await client.sendRootsListChanged();
338+
await vi.waitFor(() =>
339+
expect(
340+
stderr.mock.calls.filter(
341+
(c) => c[0] === "No valid root directories provided by client",
342+
),
343+
).toHaveLength(2),
344+
);
345+
expect(await allowedDirectoriesOf(client)).toEqual([cliDir]);
346+
});
347+
254348
it("logs and keeps the current directories when the re-fetch fails", async () => {
255349
let fail = false;
256350
const { client } = await connectTracked([], {

‎src/filesystem/lib.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,9 @@ export async function validatePath(
233233
);
234234
if (!isAllowed) {
235235
throw new Error(
236-
`Access denied - path outside allowed directories: ${absolute} not in ${allowedDirectories.join(", ")}`,
236+
allowedDirectories.length === 0
237+
? `Access denied - no allowed directories: the client exposes no valid roots (${absolute})`
238+
: `Access denied - path outside allowed directories: ${absolute} not in ${allowedDirectories.join(", ")}`,
237239
);
238240
}
239241

‎src/filesystem/server.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ export function createServer(
172172
options: ServerOptions = {},
173173
): McpServer {
174174
let allowedDirectories = [...initialAllowedDirectories];
175+
// True once the client's roots have replaced the command-line directories.
176+
// From then on a roots update replaces them even with none, so a client that
177+
// withdraws its roots withdraws the server's access too (#5094). Until then an
178+
// update with no valid roots keeps the command-line directories.
179+
let rootsInForce = false;
175180

176181
const server = new McpServer({
177182
name: "secure-filesystem-server",
@@ -763,9 +768,15 @@ export function createServer(
763768
const validatedRootDirs = await getValidRootDirectories(requestedRoots);
764769
if (validatedRootDirs.length > 0) {
765770
allowedDirectories = [...validatedRootDirs];
771+
rootsInForce = true;
766772
console.error(
767773
`Updated allowed directories from MCP roots: ${validatedRootDirs.length} valid directories`,
768774
);
775+
} else if (rootsInForce) {
776+
allowedDirectories = [];
777+
console.error(
778+
"No valid root directories provided by client; access revoked until it exposes a root again",
779+
);
769780
} else {
770781
console.error("No valid root directories provided by client");
771782
}

0 commit comments

Comments
 (0)