fix(filesystem): preserve literal $ in edit_file newText (#4157)#4172
Open
dhruba-datta wants to merge 1 commit into
Open
fix(filesystem): preserve literal $ in edit_file newText (#4157)#4172dhruba-datta wants to merge 1 commit into
dhruba-datta wants to merge 1 commit into
Conversation
Fixes modelcontextprotocol#4157. `applyFileEdits` (in src/filesystem/lib.ts) invoked `String.prototype.replace(searchString, replacementString)` where the second argument is interpreted as a replacement-string pattern, not a literal string. This silently corrupted content containing literal $ characters: | Pattern in newText | Replaced by | | ------------------ | --------------------------------- | | $$ | a single $ | | $& | the matched substring | | $` | the substring preceding the match | | $' | the substring following the match | | $<name> | (regex named-group syntax) | Particularly hit JS/TS code with $variables, currency, regex source, and shell scripts. Fix: pass the replacement via the callback form `replace(needle, () => normalizedNew)`. The function's return value is used verbatim, bypassing the pattern interpreter, while preserving "replace first occurrence only" semantics. The fallback line-by-line path was not affected. Added regression test in lib.test.ts covering all five pattern cases.
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 #4157.
Root cause
applyFileEdits(insrc/filesystem/lib.ts) invokedString.prototype.replace(searchString, replacementString)where thesecond argument is interpreted as a replacement-string pattern,
not a literal string. This silently corrupted content containing
literal
$characters:newText$$$$&$`$'$<name>Particularly hit JS/TS code with
$variables, currency amounts, regexsource, and shell scripts. The fallback line-by-line path (via
splice/
join) was not affected.Fix
Pass the replacement via the callback form:
The function's return value is used verbatim, which bypasses the
pattern interpreter while preserving "replace first occurrence only"
semantics. One-line change to the production code.
Test coverage
Added a regression test in
__tests__/lib.test.tscovering all fivepattern cases (
$$,$&,$`,$',$<name>). The new test runsalongside the existing
applyFileEditstest block and matches thefile's existing style.
Full vitest suite: 147 passed, 0 failed locally after the change.
Impact
@modelcontextprotocol/server-filesystemhas ~297k weekly downloadsper the issue, and the corruption is silent —
edit_filereturnssuccess and a clean-looking diff while the on-disk content differs
from intent.