Skip to content

Commit 44f611e

Browse files
committed
improve quoteStringIfNecessary to escape special characters
1 parent 48fc434 commit 44f611e

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/features/execution/execUtils.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
export function quoteStringIfNecessary(arg: string): string {
2-
if (arg.indexOf(' ') >= 0 && !(arg.startsWith('"') && arg.endsWith('"'))) {
3-
return `"${arg}"`;
2+
// Always return if already quoted to avoid double-quoting
3+
if (arg.startsWith('"') && arg.endsWith('"')) {
4+
return arg;
45
}
5-
return arg;
6+
7+
// Quote if contains common shell special characters that are problematic across multiple shells
8+
// Includes: space, &, |, <, >, ;, ', ", `, (, ), [, ], {, }, $
9+
const needsQuoting = /[\s&|<>;'"`()\[\]{}$]/.test(arg);
10+
11+
return needsQuoting ? `"${arg}"` : arg;
612
}
713

814
export function quoteArgs(args: string[]): string[] {

0 commit comments

Comments
 (0)