Skip to content

Commit 5ea217b

Browse files
cliffhallclaude
andcommitted
fix(filesystem): revoke access when a roots update leaves no valid root
Once the client's roots are in force, a roots/list_changed update with no valid roots now clears the allow-list instead of keeping the previous directories. Before any root has been in force, such an update keeps the command-line directories, as before. An empty allow-list now says so in its access-denied error. Closes #5094 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 207fa99 commit 5ea217b

6 files changed

Lines changed: 115 additions & 2 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+
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's roots expose none (${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's roots expose none/,
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's roots expose none (${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
@@ -169,6 +169,11 @@ export function createServer(
169169
options: ServerOptions = {},
170170
): McpServer {
171171
let allowedDirectories = [...initialAllowedDirectories];
172+
// True once the client's roots have replaced the command-line directories.
173+
// From then on a roots update replaces them even with none, so a client that
174+
// withdraws its roots withdraws the server's access too (#5094). Until then an
175+
// update with no valid roots keeps the command-line directories.
176+
let rootsInForce = false;
172177

173178
const server = new McpServer({
174179
name: "secure-filesystem-server",
@@ -760,9 +765,15 @@ export function createServer(
760765
const validatedRootDirs = await getValidRootDirectories(requestedRoots);
761766
if (validatedRootDirs.length > 0) {
762767
allowedDirectories = [...validatedRootDirs];
768+
rootsInForce = true;
763769
console.error(
764770
`Updated allowed directories from MCP roots: ${validatedRootDirs.length} valid directories`,
765771
);
772+
} else if (rootsInForce) {
773+
allowedDirectories = [];
774+
console.error(
775+
"No valid root directories provided by client; access revoked until it exposes a root again",
776+
);
766777
} else {
767778
console.error("No valid root directories provided by client");
768779
}

0 commit comments

Comments
 (0)