Skip to content

fix(filesystem): prevent $ replacement patterns in edit_file newText#4158

Open
Christian-Sidak wants to merge 1 commit into
modelcontextprotocol:mainfrom
Christian-Sidak:fix/issue-4157
Open

fix(filesystem): prevent $ replacement patterns in edit_file newText#4158
Christian-Sidak wants to merge 1 commit into
modelcontextprotocol:mainfrom
Christian-Sidak:fix/issue-4157

Conversation

@Christian-Sidak
Copy link
Copy Markdown

Summary

  • applyFileEdits called String.prototype.replace(searchStr, replacementStr), where JS interprets special patterns in the replacement argument ($$, $&, $`, $'), silently corrupting content that contains literal $ characters
  • Fix uses the callback form replace(searchStr, () => replacement) — the return value of a callback is always treated as a literal string, disabling pattern interpretation while preserving "replace first occurrence only" semantics
  • Added four parametric tests covering each affected replacement pattern

Test 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)
  • All existing lib.test.ts tests (49 total) continue to pass

Fixes #4157

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

filesystem: edit_file newText interpreted as String.prototype.replace replacement-pattern (literal $ corrupted)

1 participant