Fix $-pattern corruption of user_config values in replaceVariables#259
Open
aosmcleod wants to merge 1 commit into
Open
Fix $-pattern corruption of user_config values in replaceVariables#259aosmcleod wants to merge 1 commit into
aosmcleod wants to merge 1 commit into
Conversation
056828b to
00ed6ba
Compare
String.prototype.replace treats $ sequences ($&, $$, $`, $', $n) in the replacement string as special patterns, so any user_config value containing a $ — e.g. a password with $& in it — was silently corrupted before being injected into mcp_config. The server then received wrong credentials. Pass a replacement function so the value is inserted verbatim. Adds a regression test covering $&, $$ and $n in a substituted value.
00ed6ba to
46e82aa
Compare
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.
Problem
replaceVariablessubstitutes${user_config.*}placeholders usingString.prototype.replace(pattern, replacement), wherereplacementis the raw user_config value. JavaScript treats$sequences in the replacement string as special patterns ($$,$&,$`,$',$n), so any user_config value containing a$is silently corrupted before it reaches the MCP server's environment.In practice this caused an MCP server to receive a mangled password and fail authentication with a generic 401, even though the user's credentials were correct. The password contained
$&, which expands to the entire matched placeholder.Fixes #258.
Fix
Pass a replacement function to
replace, which disables special-pattern interpretation and inserts the value verbatim:The array-expansion path already uses
push(replacement)directly and was unaffected.Test
Adds a regression test asserting that a value containing
$&,$$and$nis substituted literally. Verified that the test fails against the oldreplace(pattern, replacement)form and passes with the fix.yarn lintandyarn testboth pass.