fix(filesystem): prevent $ replacement patterns in edit_file newText#4158
Open
Christian-Sidak wants to merge 1 commit into
Open
fix(filesystem): prevent $ replacement patterns in edit_file newText#4158Christian-Sidak wants to merge 1 commit into
Christian-Sidak wants to merge 1 commit into
Conversation
String.prototype.replace interprets special patterns like $$, $&, $`, and $' in the replacement string. Use a callback instead so that the return value is always treated as a literal replacement. Fixes modelcontextprotocol#4157
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.
Summary
applyFileEditscalledString.prototype.replace(searchStr, replacementStr), where JS interprets special patterns in the replacement argument ($$,$&,$`,$'), silently corrupting content that contains literal$charactersreplace(searchStr, () => replacement)— the return value of a callback is always treated as a literal string, disabling pattern interpretation while preserving "replace first occurrence only" semanticsTest plan
$$in newText preserved as$$(not collapsed to$)$&in newText preserved as$&(not expanded to matched substring)$`in newText preserved as$`(not expanded to preceding substring)$'in newText preserved as$'(not expanded to following substring)Fixes #4157