Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/shared/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export function replaceVariables(
{ key, replacement },
);
} else {
result = result.replace(pattern, replacement);
// Use a replacement function so that `$` sequences in the value
// (e.g. `$&`, `$$`, `$1`) are inserted literally rather than being
// interpreted as String.prototype.replace special patterns. This
// matters for secrets such as passwords that may contain `$`.
result = result.replace(pattern, () => replacement);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ describe("replaceVariables", () => {
consoleWarnSpy.mockRestore();
});

it("should insert values containing $ patterns literally", () => {
// `$&`, `$$`, `$1` etc. are special in String.prototype.replace's
// replacement string — secrets containing them must be inserted verbatim.
const result = replaceVariables("${user_config.password}", {
"user_config.password": "pa$&ss$$wo$1rd",
});
expect(result).toBe("pa$&ss$$wo$1rd");
});

it("should replace variables in objects", () => {
const result = replaceVariables(
{ message: "Hello ${name}!" },
Expand Down