fix(filesystem): preserve file permissions during write and edit operations#4115
Open
drudiger wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix(filesystem): preserve file permissions during write and edit operations#4115drudiger wants to merge 1 commit intomodelcontextprotocol:mainfrom
drudiger wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
…ations The atomic write pattern (write temp file + rename) replaces the original inode, causing the new file to have default 0644 permissions regardless of what the original file had. This breaks executable scripts and other files with non-default permissions. Fix: capture stat.mode before writing and restore it with chmod after rename. Fixes both writeFileContent() and applyFileEdits().
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.
Description
The filesystem server's atomic write pattern (write to temp file +
fs.rename()) replaces the original file's inode, causing the new file to inherit default0644permissions from the temp file. This silently strips execute bits and any other non-default permissions from the original file.This fix captures
stat.modebefore the write and restores it withfs.chmod()after the rename, in bothwriteFileContent()andapplyFileEdits().Server Details
write_file,edit_file)Motivation and Context
When using
edit_fileorwrite_fileon an executable script (e.g., a shell script withchmod +x), the file loses its execute permission after the edit. This causes downstream failures when the script is invoked -- for example, a CI build calling./pip_install.shfails with "Permission denied" after the MCP server edits the file.The root cause is the atomic write security pattern:
fs.writeFile(tempPath)creates a new file with the process's default umask (typically0644), thenfs.rename(tempPath, filePath)atomically replaces the original inode. Since the rename replaces the inode entirely, the original file's permission bits are lost.How Has This Been Tested?
Unit tests (4 new tests added, all 149 pass):
writeFileContentpreserves 755 permissions when overwriting existing fileapplyFileEditspreserves 755 permissions after applying editsapplyFileEditsdoes NOT call chmod in dry run modeapplyFileEditstests updated withstat/chmodmocks inbeforeEachManual testing with MCP client:
chmod 755, edited it via theedit_fileMCP tool, confirmed permissions remained755write_fileMCP tool, confirmed permissions preservedBreaking Changes
None. This is a bug fix that restores expected behavior. No configuration changes needed.
Types of changes
Checklist
Note: No README update is needed because this is a bug fix, not a new feature. No new environment variables or configuration options were introduced.
Additional context
The fix adds
fs.stat()before the atomic write andfs.chmod()afterfs.rename()in two locations:writeFileContent()(line ~170 in lib.ts) -- used by thewrite_filetoolapplyFileEdits()(line ~272 in lib.ts) -- used by theedit_filetoolThere is an inherent TOCTOU window between
statandchmodwhere another process could change the file's permissions, but this is the same class of race that exists for the content read inapplyFileEditsand is not practically exploitable in this context.