diff --git a/packages/manager/.changeset/pr-10887-tech-stories-1725467686328.md b/packages/manager/.changeset/pr-10887-tech-stories-1725467686328.md new file mode 100644 index 00000000000..48bf6c14c7f --- /dev/null +++ b/packages/manager/.changeset/pr-10887-tech-stories-1725467686328.md @@ -0,0 +1,5 @@ +--- +"@linode/manager": Tech Stories +--- + +Resolve "Incomplete string escape or encoding" codeQL alert in `generate-ansibleConfig.ts` ([#10887](https://github.com/linode/manager/pull/10887)) diff --git a/packages/manager/src/utilities/codesnippets/generate-ansibleConfig.test.ts b/packages/manager/src/utilities/codesnippets/generate-ansibleConfig.test.ts index 018500f605d..6b147ec6c8a 100644 --- a/packages/manager/src/utilities/codesnippets/generate-ansibleConfig.test.ts +++ b/packages/manager/src/utilities/codesnippets/generate-ansibleConfig.test.ts @@ -43,4 +43,17 @@ describe('generateAnsibleConfig', () => { expect(generateAnsibleConfig(config)).toEqual(expectedOutput); }); + + it('should safely escape extra backslash characters in YAML strings', () => { + const config = { + label: 'Linode with ] and also \\[, }, and \\{', + region: 'us-central', + root_pass: 'securePass123', + type: 'g6-standard-1', + }; + + const expectedOutput = `- name: Create a new Linode instance.\n linode.cloud.instance:\n state: "present"\n label: "Linode with \\] and also \\\\\\[, \\}, and \\\\\\{"\n type: "g6-standard-1"\n region: "us-central"\n root_pass: "securePass123"\n`; + + expect(generateAnsibleConfig(config)).toEqual(expectedOutput); + }); }); diff --git a/packages/manager/src/utilities/codesnippets/generate-ansibleConfig.ts b/packages/manager/src/utilities/codesnippets/generate-ansibleConfig.ts index d04cf80cdd7..968bbed7d12 100644 --- a/packages/manager/src/utilities/codesnippets/generate-ansibleConfig.ts +++ b/packages/manager/src/utilities/codesnippets/generate-ansibleConfig.ts @@ -6,7 +6,7 @@ import type { CreateLinodeRequest } from '@linode/api-v4/lib/linodes'; * @returns {string} - The safely escaped string. */ function escapeYAMLString(str: string) { - return str.replace(/(["':\[\]\{\}])/g, '\\$1').replace(/\n/g, '\\n'); + return str.replace(/(["':\\\[\\\]\\\{\\\}])/g, '\\$1').replace(/\n/g, '\\n'); } /**