Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tech story: [M3-8423] - Resolve "Incomplete string escape or encoding" in generate-ansibleConfig.test.ts #10887

Merged
merged 4 commits into from
Sep 10, 2024
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
Original file line number Diff line number Diff line change
@@ -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))
Original file line number Diff line number Diff line change
Expand Up @@ -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 \\{',
Copy link
Contributor Author

@coliu-akamai coliu-akamai Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Originally I'd had \] and \} in this label, but eslint corrected them to just ] and { πŸ₯²... They still resulted in \\] and \\}, same as the current expected output (which is good/what we'd want, I think!).

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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down
Loading