fix(filesystem): prevent move_file from overwriting existing destination (#4628) - #4709
Closed
LuckTerence wants to merge 1 commit into
Closed
Conversation
…ion (modelcontextprotocol#4628) Signed-off-by: LuckTerence <xihuan1127@gmail.com>
Member
Author
|
Superseded by #4630, which was merged earlier today — closing this rather than leaving a conflicting duplicate in the queue. Worth noting #4630 landed the guard inside |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4628
Problem
The
move_filetool handler in@modelcontextprotocol/server-filesystempreviously calledfs.rename(validSourcePath, validDestPath)directly without verifying whether the destination path already existed. Under POSIX and Node.js semantics,fs.renamesilently overwrites existing target files, leading to unexpected data loss. This contradicted the tool specification and documentation ("If the destination exists, the operation will fail").Fix
validDestPathusingawait fs.stat(validDestPath)before invokingfs.rename.Error(Destination already exists: ${args.destination}).fs.statthrowsENOENT, execution proceeds tofs.renameas expected. Any unexpected I/O errors are rethrown.Tests
src/filesystem/__tests__/move-file.test.ts:move_filesucceeds when moving a file to a non-existent destination.move_filefails with an explicit error when destination file exists, leaving both source and destination file contents intact (data loss prevention).move_filefails when destination directory exists, leaving source and destination intact.move_filesucceeds when moving an entire directory to a non-existent destination.