Skip to content
Merged
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: 4 additions & 2 deletions src/tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ export function keywordArrayReplace(input: string, mappings: KeywordMappings): s
// Matching against two sets of patterns because a developer may provide their array replacement keyword with or without wrapping quotes. It is not obvious to the developer which to do depending if they're operating in YAML or JSON.
const regex = keywordReplaceArrayRegExp(key);

input = input.replace(regex, JSON.stringify(mappings[key]));
// Use function-based replacement to prevent $ sequences (e.g., $', $`, $&) from being interpreted as special replacement patterns to fixes issue #1153.
input = input.replace(regex, () => JSON.stringify(mappings[key]));
});
return input;
}

export function keywordStringReplace(input: string, mappings: KeywordMappings): string {
Object.keys(mappings).forEach(function (key) {
const regex = keywordReplaceStringRegExp(key);
// Use function-based replacement to prevent $ sequences (e.g., $', $`, $&) from being interpreted as special replacement patterns to fixes issue #1153.
// @ts-ignore TODO: come back and distinguish strings vs array replacement.
input = input.replace(regex, mappings[key]);
input = input.replace(regex, () => mappings[key]);
});
return input;
}
Expand Down
19 changes: 19 additions & 0 deletions test/tools/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,15 @@ describe('#keywordReplacement', () => {
`{ "foo": ${mapping.STRING_REPLACEMENT}, "bar": "OTHER_REPLACEMENT" }`
);
});

// Issue #1153: Test cases for $ character handling
it('should handle dollar-apostrophe in replacement value', () => {
const input = '{ "secret": "##PASSWORD##" }';
const passwordMapping = { PASSWORD: "'mySecret$'" };
const output = utils.keywordStringReplace(input, passwordMapping);
expect(output).to.equal('{ "secret": "\'mySecret$\'" }');
expect(() => JSON.parse(output)).to.not.throw();
});
});

describe('#keywordArrayReplace', () => {
Expand Down Expand Up @@ -429,6 +438,16 @@ describe('#keywordReplacement', () => {
doubleQuotes: mapping.ARRAY_REPLACEMENT,
});
});

// Issue #1153: Test cases for $ character handling in arrays
it('should handle dollar-apostrophe in array values', () => {
const input = '{ "keys": @@KEYS@@ }';
const arrayMapping = { KEYS: ["api-key$'", "secret$'"] };
const output = utils.keywordArrayReplace(input, arrayMapping);
expect(() => JSON.parse(output)).to.not.throw();
const parsed = JSON.parse(output);
expect(parsed.keys).to.deep.equal(arrayMapping.KEYS);
});
});
});

Expand Down