fix(filesystem): make move_file fail instead of silently overwriting the destination - #4630
Open
eeshsaxena wants to merge 1 commit into
Open
Conversation
…nation The move_file tool description and README both state the operation fails if the destination already exists, but the handler called fs.rename directly, which silently overwrites the destination. Since this server ships no delete tool, move_file effectively provided unadvertised, irreversible file deletion: moving any file onto an existing target destroyed the target. Add a moveFile helper in lib.ts that rejects when the destination already exists (checked with lstat, so an existing symlink is detected rather than followed) and only renames when the target is free, and use it from the move_file handler. Behaviour now matches the documented contract. Fixes modelcontextprotocol#4628
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.
Fixes #4628.
move_file's description and the README both say the move fails if the destination already exists:But the handler calls
fs.renamedirectly, which silently overwrites the destination and still reports success. Because this server ships no delete tool, an agent that reads the contract reasonably treatsmove_fileas non-clobbering, so this quietly turns into unadvertised, irreversible file deletion: move any file onto an existing target and the target's contents are gone.Fix
Added a
moveFilehelper inlib.tsthat checks the destination first and rejects if anything already occupies it, then renames only when the target is free:lstat(notstat) is used so an existing symlink at the destination is detected rather than followed. Themove_filehandler now calls this helper. Behaviour now matches the documented contract, and the successful-move path is unchanged.Tests
Added two cases to
__tests__/lib.test.ts:The second test fails against the old
fs.rename-only behaviour and passes with the fix. Full file suite green: