Skip to content

Commit

Permalink
refactor: escape quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
topliceanurazvan committed Jan 5, 2024
1 parent ebc2539 commit c6da331
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/web/src/utils/escapeArguments.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,13 @@ describe('utils', () => {
it('should handle array with multiple arguments with spaces', () => {
expect(escapeArguments(['ab c', 'de f'])).toEqual(['"ab c"', '"de f"']);
});

it('should handle array with multiple arguments with quotes', () => {
expect(escapeArguments(['ab"c', 'de"f'])).toEqual(['ab"c', 'de"f']);
});

it('should handle array with multiple arguments with spaces and quotes', () => {
expect(escapeArguments(['ab"c', 'de f'])).toEqual(['ab"c', '"de f"']);
});
});
});
6 changes: 5 additions & 1 deletion packages/web/src/utils/escapeArguments.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const escapeArguments = (args: string[]) => {
return args.map(arg => (arg.includes(' ') ? `"${arg}"` : arg));
return args.map(arg => {
const escapedArg = arg.replace(/(")/g, '\\$1');

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.

return /\s/.test(escapedArg) ? `"${escapedArg}"` : escapedArg;
});
};

0 comments on commit c6da331

Please sign in to comment.