Skip to content

Fix $-pattern corruption of user_config values in replaceVariables#259

Open
aosmcleod wants to merge 1 commit into
modelcontextprotocol:mainfrom
aosmcleod:fix/user-config-dollar-substitution
Open

Fix $-pattern corruption of user_config values in replaceVariables#259
aosmcleod wants to merge 1 commit into
modelcontextprotocol:mainfrom
aosmcleod:fix/user-config-dollar-substitution

Conversation

@aosmcleod

Copy link
Copy Markdown

Problem

replaceVariables substitutes ${user_config.*} placeholders using String.prototype.replace(pattern, replacement), where replacement is 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:

result = result.replace(pattern, () => replacement);

The array-expansion path already uses push(replacement) directly and was unaffected.

Test

Adds a regression test asserting that a value containing $&, $$ and $n is substituted literally. Verified that the test fails against the old replace(pattern, replacement) form and passes with the fix.

yarn lint and yarn test both pass.

@aosmcleod aosmcleod force-pushed the fix/user-config-dollar-substitution branch from 056828b to 00ed6ba Compare June 8, 2026 17:11
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.
@aosmcleod aosmcleod force-pushed the fix/user-config-dollar-substitution branch from 00ed6ba to 46e82aa Compare June 8, 2026 17:12
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.

replaceVariables corrupts user_config values containing $ (String.replace replacement-pattern footgun)

1 participant