Skip to content

Commit 056828b

Browse files
committed
Fix $-pattern corruption of user_config values in replaceVariables
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.
1 parent 70fe3b3 commit 056828b

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/shared/config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ export function replaceVariables(
3434
{ key, replacement },
3535
);
3636
} else {
37-
result = result.replace(pattern, replacement);
37+
// Use a replacement function so that `$` sequences in the value
38+
// (e.g. `$&`, `$$`, `$1`) are inserted literally rather than being
39+
// interpreted as String.prototype.replace special patterns. This
40+
// matters for secrets such as passwords that may contain `$`.
41+
result = result.replace(pattern, () => replacement);
3842
}
3943
}
4044
}

test/config.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ describe("replaceVariables", () => {
2929
consoleWarnSpy.mockRestore();
3030
});
3131

32+
it("should insert values containing $ patterns literally", () => {
33+
// `$&`, `$$`, `$1` etc. are special in String.prototype.replace's
34+
// replacement string — secrets containing them must be inserted verbatim.
35+
const result = replaceVariables("${user_config.password}", {
36+
"user_config.password": "pa$&ss$$wo$1rd",
37+
});
38+
expect(result).toBe("pa$&ss$$wo$1rd");
39+
});
40+
3241
it("should replace variables in objects", () => {
3342
const result = replaceVariables(
3443
{ message: "Hello ${name}!" },

0 commit comments

Comments
 (0)