Skip to content

Commit 7dc802f

Browse files
cliffhall0ywfeayushbunny11claude
committed
fix(filesystem): overwrite existing files in place (#4512, #3199)
write_file and edit_file replaced an existing file by renaming a temp file over it. That gave the file a new inode every time, so it lost its creation time, hard links and inode identity (#4512), and on Windows the rename fails with EPERM when another process holds the file open (#3199). Both now go through overwriteInPlace in lib.ts, which truncates and writes through a single handle on the existing file. Symlink safety is kept by three checks: lstat refuses anything that is not a regular file, the open uses O_NOFOLLOW where the platform has it (POSIX), and the opened handle must have the device and inode lstat saw, which catches a swap between the lstat and the open on every platform, Windows included. Permission bits now survive by construction, so the chmod repair after the rename is gone. The trade-off is crash-atomicity: a crash mid-write can leave the file partly written. Documented in the README and the changeset. The pinning tests for #4512 and #3199 now assert the correct behavior and lose their KNOWN BUG markers; new tests stage a symlink swap and a file swap inside the TOCTOU window against the real filesystem. Approach follows #4516 (O_NOFOLLOW open, fstat, truncate and write) and the Windows O_NOFOLLOW caveat raised on #4512. Part of #5004 (Wave 2). Co-authored-by: 0ywfe <[email protected]> Co-authored-by: Ayush Padhy <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 571e2b3 commit 7dc802f

6 files changed

Lines changed: 348 additions & 193 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+
`write_file` and `edit_file` now overwrite an existing file in place instead of renaming a temp file over it, so the file keeps its inode, creation time (birthtime), hard links and permission bits (#4512), and an overwrite no longer fails with `EPERM` on Windows when another process holds the file open (#3199). A symlink swapped in after the path was validated is still refused rather than written through, now by an `O_NOFOLLOW` open (on POSIX) and a check that the opened file is the one validated (on every platform). The overwrite is no longer crash-atomic.

‎src/filesystem/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ The server's directory access control follows this flow:
9595
- Inputs:
9696
- `path` (string): File location
9797
- `content` (string): File content
98+
- An existing file is overwritten in place, so it keeps its inode, creation
99+
time, hard links and permissions (`edit_file` writes the same way). The
100+
overwrite is not crash-atomic: a crash mid-write can leave the file partly
101+
written
98102

99103
- **edit_file**
100104
- Make selective edits using advanced pattern matching and formatting

0 commit comments

Comments
 (0)