Skip to content

Escape regex metacharacters in the variable key during substitution#269

Open
aosmcleod wants to merge 1 commit into
modelcontextprotocol:mainfrom
aosmcleod:fix/escape-variable-key-regex
Open

Escape regex metacharacters in the variable key during substitution#269
aosmcleod wants to merge 1 commit into
modelcontextprotocol:mainfrom
aosmcleod:fix/escape-variable-key-regex

Conversation

@aosmcleod

Copy link
Copy Markdown

Problem

replaceVariables builds its match pattern with new RegExp(\\$\{${key}\}`, "g")`, interpolating the raw key. Two consequences:

  • The . in keys like user_config.cs_password is an unescaped metacharacter, so it matches any character — ${user_config_cs_password} (underscore) is wrongly substituted.
  • A user_config key containing a metacharacter ([, (a+)+, …) throws a SyntaxError / is a ReDoS vector, aborting getMcpConfigForManifest for an otherwise valid manifest. The schema allows arbitrary string keys.

Distinct from #258, which concerns $ sequences in the replacement value. Fixes #262.

Fix

Escape regex metacharacters in the key before constructing the pattern (src/shared/config.ts).

Test

Adds two cases to test/config.test.ts: . matches only a literal dot, and a metacharacter key does not throw and matches its own literal placeholder. Both fail against the old code and pass with the fix. yarn lint + yarn test green.

replaceVariables built its match pattern with `new RegExp(\`\\$\\{${key}\\}\`)`,
interpolating the raw key. Keys contain "." (e.g. "user_config.cs_password")
and the manifest schema allows arbitrary user_config key names, so the pattern
over-matched ("." matched any character) and a key with a metacharacter (e.g.
"[", "(a+)+") threw a SyntaxError / ReDoS, aborting config generation for an
otherwise valid manifest. Escape the key before building the regex.

Distinct from modelcontextprotocol#258 (which concerns `$` in the replacement value). Adds tests
for literal-dot matching and metacharacter-key safety.

Fixes modelcontextprotocol#262
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 interpolates the variable key into a RegExp unescaped (over-match + crash/ReDoS)

1 participant