Skip to content

Commit f3222de

Browse files
cliffhallclaude
andcommitted
fix(filesystem): close two read-only-dir bypasses found in review
move_file now validates both ends in a "move" mode that also refuses a path containing a read-only directory, so moving its parent cannot carry a protected subtree out from under the marking. A read-only directory that does not exist yet is resolved through its nearest existing ancestor, so a symlinked ancestor cannot leave it unprotected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent b766bc2 commit f3222de

7 files changed

Lines changed: 160 additions & 51 deletions

File tree

‎.changeset/filesystem-5348-read-only-dirs.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@modelcontextprotocol/server-filesystem": minor
33
---
44

5-
Add `--read-only-dir <path>`, repeatable, to make one allowed directory read-only while the others stay writable. Every write tool stays listed but refuses a target inside a read-only directory (`move_file` on either end, `copy_file` on its destination), checked on the resolved path so a symlink cannot route around it; a 2025-era client's Roots never lift the marking. `list_allowed_directories` names the read-only directories.
5+
Add `--read-only-dir <path>`, repeatable, to make one allowed directory read-only while the others stay writable. Every write tool stays listed but refuses a target inside a read-only directory (`move_file` on either end, including moving a directory that contains one, and `copy_file` on its destination), checked on the resolved path so a symlink cannot route around it; a 2025-era client's Roots never lift the marking. `list_allowed_directories` names the read-only directories.

‎src/filesystem/README.md‎

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ Published on npm as [`@modelcontextprotocol/server-filesystem`](https://www.npmj
1919
The server may touch only its **allowed directories**. Pass them on the command line. A 2025-era client that supports [Roots](https://modelcontextprotocol.io/docs/learn/client-concepts#roots) can also replace them at runtime, but Roots is [deprecated as of protocol version 2026-07-28](https://modelcontextprotocol.io/specification/2026-07-28/deprecated) and a 2026-07-28 connection cannot use it.
2020

2121
### Method 1: Command-line Arguments (Recommended)
22+
2223
Specify Allowed directories when starting the server:
24+
2325
```bash
2426
mcp-server-filesystem /path/to/dir1 /path/to/dir2
2527
```
@@ -29,7 +31,9 @@ Options may come before, between or after the directories: `--read-only`, `--rea
2931
This is the only method that works on every protocol version, so it is the one to use.
3032

3133
### Read-only Mode
34+
3235
Add `--read-only` to serve only the tools that cannot change the filesystem:
36+
3337
```bash
3438
mcp-server-filesystem --read-only /path/to/dir1 /path/to/dir2
3539
```
@@ -41,27 +45,32 @@ The flag may appear anywhere among the arguments. `--` ends the options, so a di
4145
Read-only mode covers every allowed directory. To make only some of them read-only, use `--read-only-dir` below.
4246

4347
#### Read-only directories
48+
4449
Add `--read-only-dir <path>` to make one directory read-only while the others stay writable. Repeat it for each directory to protect:
50+
4551
```bash
4652
mcp-server-filesystem --read-only-dir /path/to/reference /path/to/workspace
4753
```
4854

4955
- A read-only directory is an allowed directory too, so it need not also be listed on its own.
50-
- Every write tool (`write_file`, `append_file`, `edit_file`, `create_directory`, `move_file` and `copy_file`) stays listed, since the other directories are writable, but refuses a target inside a read-only directory with an error such as `Access denied - read-only directory: /path/to/reference/notes.md is in /path/to/reference, which the server was started with --read-only-dir`. `move_file` is refused when either its source or its destination is inside one; `copy_file` may copy out of one, never into it. `edit_file` is refused there even with `dryRun`.
51-
- The check runs on a path's resolved location, so a symlink in a writable directory cannot route a write into a read-only one.
56+
- Every write tool (`write_file`, `append_file`, `edit_file`, `create_directory`, `move_file` and `copy_file`) stays listed, since the other directories are writable, but refuses a target inside a read-only directory with an error such as `Access denied - read-only directory: /path/to/reference/notes.md is in /path/to/reference, which the server was started with --read-only-dir`. `move_file` is refused when either its source or its destination is inside one or contains one, so a protected directory cannot be carried away by moving its parent; `copy_file` may copy out of one, never into it. `edit_file` is refused there even with `dryRun`.
57+
- The check runs on a path's resolved location, so a symlink in a writable directory cannot route a write into a read-only one. A read-only directory that does not exist yet is still protected: it is resolved through its nearest existing ancestor, so it covers the path a write would create.
5258
- A path inside any read-only directory is refused, so a directory nested inside a read-only one is read-only too, even if it is also given as a writable directory.
5359
- The marking is fixed at startup and holds on both protocol eras. A 2025-era client's Roots can replace the allowed directories (see Method 2), but Roots say nothing about writing, so they never lift it: a write into a read-only directory is refused whichever directories are in force, with or without `--confine-roots`.
5460
- `list_allowed_directories` lists the read-only directories after the allowed ones, and the server logs them to stderr at startup.
5561

5662
The option may appear anywhere among the arguments, and takes the next argument as its directory, whatever it is. `--` ends the options, so after it `--read-only-dir` is a directory name. With `--read-only` as well, every write tool is withheld anyway. For defence in depth, make the protected directories read-only at the operating-system level as well, with file permissions or, with Docker, a bind mount's `ro` flag.
5763

5864
### Method 2: MCP Roots (Deprecated, 2025-era clients only)
65+
5966
A client that connects with a 2025-era protocol version (the `initialize` handshake) and supports [Roots](https://modelcontextprotocol.io/docs/learn/client-concepts#roots) can dynamically update the Allowed directories.
6067

6168
Roots notified by Client to Server, completely replace any server-side Allowed directories when provided.
6269

6370
#### Confining Roots to the command-line directories
71+
6472
To keep a client's Roots inside the directories you gave on the command line, add `--confine-roots`:
73+
6574
```bash
6675
mcp-server-filesystem --confine-roots /path/to/dir1 /path/to/dir2
6776
```
@@ -92,7 +101,6 @@ The server's directory access control follows this flow:
92101
- The client's first message picks the protocol era of the connection
93102
- A **2026-07-28** client sends `server/discover` (or any request carrying the per-request `_meta` envelope): the server uses the command-line directories, and steps 3 and 4 do not apply
94103
- A **2025-era** client sends `initialize` with its capabilities, and the server checks whether it supports roots (`capabilities.roots`)
95-
96104
3. **Roots Protocol Handling** (2025-era clients that support roots)
97105
- **On initialization**: Server requests roots from client via `roots/list`
98106
- Client responds with its configured roots
@@ -120,8 +128,6 @@ The server's directory access control follows this flow:
120128
121129
**Note**: The server will only allow operations within directories specified either via `args` or, for a 2025-era client, via Roots.
122130
123-
124-
125131
## API
126132
127133
### Resources
@@ -295,7 +301,6 @@ Files inside the allowed directories are also [resources](https://modelcontextpr
295301
entries below it do not count toward the 100,000-entry bound
296302
- Output is formatted with 2-space indentation for readability
297303
- Bounded: see [Slow and very large trees](#slow-and-very-large-trees)
298-
299304
- **get_file_info**
300305
- Get detailed file/directory metadata
301306
- Input: `path` (string)
@@ -326,11 +331,11 @@ request from stalling the server:
326331
- **Resolving a path (in any tool), and each directory read in `search_files`
327332
and `directory_tree`, waits at most 15 seconds.** A call that takes longer ends
328333
the request with an error naming the path (`Filesystem call timed out after
329-
15000 ms: ...`). A path whose real location cannot be read in time is
334+
15000 ms: ...`). A path whose real location cannot be read in time is
330335
refused, never admitted.
331336
- **`search_files` and `directory_tree` visit at most 100,000 entries.** A walk
332337
that reaches the cap ends with an error (`... stopped after visiting 100000
333-
entries ...`) rather than a partial result. Excluded entries do not count.
338+
entries ...`) rather than a partial result. Excluded entries do not count.
334339
335340
Either error means: narrow `path`, or name the slow or large folder in
336341
`excludePatterns`. A timed-out call cannot be cancelled and finishes in the
@@ -349,23 +354,23 @@ on each tool so clients can:
349354
350355
The mapping for filesystem tools is:
351356
352-
| Tool | readOnlyHint | idempotentHint | destructiveHint | Notes |
353-
|-----------------------------|--------------|----------------|-----------------|--------------------------------------------------|
354-
| `read_file` | `true` | `true` | `false` | Deprecated alias of `read_text_file` |
355-
| `read_text_file` | `true` | `true` | `false` | Pure read |
356-
| `read_media_file` | `true` | `true` | `false` | Pure read |
357-
| `read_multiple_files` | `true` | `true` | `false` | Pure read |
358-
| `list_directory` | `true` | `true` | `false` | Pure read |
359-
| `list_directory_with_sizes` | `true` | `true` | `false` | Pure read |
360-
| `directory_tree` | `true` | `true` | `false` | Pure read |
361-
| `search_files` | `true` | `true` | `false` | Pure read |
362-
| `get_file_info` | `true` | `true` | `false` | Pure read |
363-
| `list_allowed_directories` | `true` | `true` | `false` | Pure read |
364-
| `create_directory` | `false` | `true` | `false` | Re‑creating the same dir is a no‑op |
365-
| `write_file` | `false` | `true` | `true` | Overwrites existing files |
366-
| `append_file` | `false` | `false` | `false` | Adds to a file; repeating it appends again |
367-
| `edit_file` | `false` | `false` | `true` | Re‑applying edits can fail or double‑apply |
368-
| `move_file` | `false` | `false` | `true` | Deletes source file |
357+
| Tool | readOnlyHint | idempotentHint | destructiveHint | Notes |
358+
| --------------------------- | ------------ | -------------- | --------------- | -------------------------------------------------- |
359+
| `read_file` | `true` | `true` | `false` | Deprecated alias of `read_text_file` |
360+
| `read_text_file` | `true` | `true` | `false` | Pure read |
361+
| `read_media_file` | `true` | `true` | `false` | Pure read |
362+
| `read_multiple_files` | `true` | `true` | `false` | Pure read |
363+
| `list_directory` | `true` | `true` | `false` | Pure read |
364+
| `list_directory_with_sizes` | `true` | `true` | `false` | Pure read |
365+
| `directory_tree` | `true` | `true` | `false` | Pure read |
366+
| `search_files` | `true` | `true` | `false` | Pure read |
367+
| `get_file_info` | `true` | `true` | `false` | Pure read |
368+
| `list_allowed_directories` | `true` | `true` | `false` | Pure read |
369+
| `create_directory` | `false` | `true` | `false` | Re‑creating the same dir is a no‑op |
370+
| `write_file` | `false` | `true` | `true` | Overwrites existing files |
371+
| `append_file` | `false` | `false` | `false` | Adds to a file; repeating it appends again |
372+
| `edit_file` | `false` | `false` | `true` | Re‑applying edits can fail or double‑apply |
373+
| `move_file` | `false` | `false` | `true` | Deletes source file |
369374
| `copy_file` | `false` | `true` | `false` | Never overwrites; a repeat fails, changing nothing |
370375
371376
> Note: the MCP spec gives `idempotentHint` and `destructiveHint` meaning only when `readOnlyHint` is `false`, but every tool sets them anyway, so a client that reads them without checking `readOnlyHint` first still sees the truth: a read can be repeated safely and changes nothing. Every tool also sets `openWorldHint: false` — this server only accesses the local filesystem within its allowed directories, never an open or external world.
@@ -375,11 +380,13 @@ The mapping for filesystem tools is:
375380
Node.js 24 or later, when the server runs through `npx` or from source.
376381
377382
## Usage with Claude Desktop
383+
378384
Add this to your `claude_desktop_config.json`:
379385
380386
Note: you can provide sandboxed directories to the server by mounting them to `/projects`. Adding the `ro` flag will make the directory readonly by the server.
381387
382388
### Docker
389+
383390
Note: all directories must be mounted to `/projects` by default.
384391
385392
```json
@@ -391,9 +398,12 @@ Note: all directories must be mounted to `/projects` by default.
391398
"run",
392399
"-i",
393400
"--rm",
394-
"--mount", "type=bind,src=/Users/username/Desktop,dst=/projects/Desktop",
395-
"--mount", "type=bind,src=/path/to/other/allowed/dir,dst=/projects/other/allowed/dir,ro",
396-
"--mount", "type=bind,src=/path/to/file.txt,dst=/projects/path/to/file.txt",
401+
"--mount",
402+
"type=bind,src=/Users/username/Desktop,dst=/projects/Desktop",
403+
"--mount",
404+
"type=bind,src=/path/to/other/allowed/dir,dst=/projects/other/allowed/dir,ro",
405+
"--mount",
406+
"type=bind,src=/path/to/file.txt,dst=/projects/path/to/file.txt",
397407
"mcp/filesystem",
398408
"/projects"
399409
]
@@ -461,7 +471,8 @@ Alternatively, you can add the configuration to a file called `.vscode/mcp.json`
461471
You can provide sandboxed directories to the server by mounting them to `/projects`. Adding the `ro` flag will make the directory readonly by the server.
462472

463473
### Docker
464-
Note: all directories must be mounted to `/projects` by default.
474+
475+
Note: all directories must be mounted to `/projects` by default.
465476

466477
```json
467478
{
@@ -472,7 +483,8 @@ Note: all directories must be mounted to `/projects` by default.
472483
"run",
473484
"-i",
474485
"--rm",
475-
"--mount", "type=bind,src=${workspaceFolder},dst=/projects/workspace",
486+
"--mount",
487+
"type=bind,src=${workspaceFolder},dst=/projects/workspace",
476488
"mcp/filesystem",
477489
"/projects"
478490
]

‎src/filesystem/__tests__/read-only-dirs.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,37 @@ describe.each([
233233
expect(beside.isError).toBeFalsy();
234234
});
235235

236+
it("refuses to move a directory that contains a read-only one, and keeps protecting it", async () => {
237+
const vault = path.join(writable, "bundle", "vault");
238+
await fs.mkdir(vault, { recursive: true });
239+
const client = await connect(mode, [writable], [vault]);
240+
const moved = await call(client, "move_file", {
241+
source: path.join(writable, "bundle"),
242+
destination: path.join(writable, "renamed"),
243+
});
244+
expect(moved.isError).toBe(true);
245+
expect(textOf(moved)).toContain(`contains ${vault}`);
246+
await expect(fs.access(path.join(writable, "renamed"))).rejects.toThrow();
247+
await expectRefused(
248+
client,
249+
"write_file",
250+
{ path: path.join(vault, "x.txt"), content: "x" },
251+
vault,
252+
);
253+
});
254+
255+
it("refuses to move a directory to where a read-only one would be inside it", async () => {
256+
const later = path.join(writable, "dest", "vault");
257+
await fs.mkdir(path.join(writable, "src", "vault"), { recursive: true });
258+
const client = await connect(mode, [writable], [later]);
259+
const moved = await call(client, "move_file", {
260+
source: path.join(writable, "src"),
261+
destination: path.join(writable, "dest"),
262+
});
263+
expect(moved.isError).toBe(true);
264+
expect(textOf(moved)).toContain(`contains ${later}`);
265+
});
266+
236267
it("names the read-only directories in list_allowed_directories", async () => {
237268
const client = await connect(mode);
238269
const text = textOf(await call(client, "list_allowed_directories"));
@@ -327,6 +358,32 @@ describe("resolveReadOnlyDirectories", () => {
327358
const missing = path.join(writable, "later");
328359
expect(await resolveReadOnlyDirectories([missing])).toEqual([missing]);
329360
});
361+
362+
it("resolves a missing directory through a symlinked ancestor", async () => {
363+
const alias = path.join(protectedDir, "alias");
364+
await fs.symlink(writable, alias);
365+
expect(
366+
await resolveReadOnlyDirectories([path.join(alias, "later", "deep")]),
367+
).toEqual([
368+
path.join(alias, "later", "deep"),
369+
path.join(writable, "later", "deep"),
370+
]);
371+
});
372+
373+
it("protects a directory created later under a symlinked ancestor", async () => {
374+
const alias = path.join(protectedDir, "alias");
375+
await fs.symlink(writable, alias);
376+
const marked = await resolveReadOnlyDirectories([
377+
path.join(alias, "later"),
378+
]);
379+
const client = await connect("legacy", [writable], marked);
380+
const created = await call(client, "create_directory", {
381+
path: path.join(writable, "later"),
382+
});
383+
expect(created.isError).toBe(true);
384+
expect(textOf(created)).toContain("Access denied - read-only directory");
385+
await expect(fs.access(path.join(writable, "later"))).rejects.toThrow();
386+
});
330387
});
331388

332389
describe("parseArgs with --read-only-dir", () => {

‎src/filesystem/lib.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,8 @@ async function resolveUnicodeEquivalentPath(
476476
// checked against the allowed directories only. A "write" must also be handed
477477
// the read-only directories, and is refused when the resolved path falls in
478478
// one, so a write tool cannot skip the check: the mode is required, and the
479-
// "write" overload does not compile without the list.
479+
// "write" overload does not compile without the list. A "move" (move_file's
480+
// two ends) is a write that is also refused when the path contains one.
480481
export async function validatePath(
481482
requestedPath: string,
482483
allowedDirectories: readonly string[],
@@ -485,17 +486,19 @@ export async function validatePath(
485486
export async function validatePath(
486487
requestedPath: string,
487488
allowedDirectories: readonly string[],
488-
access: "write",
489+
access: "write" | "move",
489490
readOnlyDirectories: readonly string[],
490491
): Promise<string>;
491492
export async function validatePath(
492493
requestedPath: string,
493494
allowedDirectories: readonly string[],
494-
access: "read" | "write",
495+
access: "read" | "write" | "move",
495496
readOnlyDirectories: readonly string[] = [],
496497
): Promise<string> {
497498
const resolved = await resolveAllowedPath(requestedPath, allowedDirectories);
498-
if (access === "write") assertWritable(resolved, readOnlyDirectories);
499+
if (access !== "read") {
500+
assertWritable(resolved, readOnlyDirectories, access);
501+
}
499502
return resolved;
500503
}
501504

0 commit comments

Comments
 (0)