Skip to content

Commit 1b86a1d

Browse files
Copilotbaronfel
andcommitted
Address code review feedback: improve escaping implementation
Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
1 parent aaa8246 commit 1b86a1d

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/Tasks/Exec.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,15 @@ private static string EscapeArgumentForWindows(string argument)
214214
return "\"\"";
215215
}
216216

217+
// Characters that require quoting in Windows cmd.exe
218+
// Based on cmd.exe special characters
219+
char[] specialChars = { ' ', '\t', '"', '&', '|', '<', '>', '^', '%', '(', ')', '!', '=', ';', ',' };
220+
217221
// Check if the argument contains special characters that need quoting
218222
bool needsQuoting = false;
219223
foreach (char c in argument)
220224
{
221-
if (c == ' ' || c == '\t' || c == '"' || c == '&' || c == '|' || c == '<' || c == '>' ||
222-
c == '^' || c == '%' || c == '(' || c == ')' || c == '!' || c == '=' || c == ';' || c == ',')
225+
if (Array.IndexOf(specialChars, c) >= 0)
223226
{
224227
needsQuoting = true;
225228
break;
@@ -388,8 +391,15 @@ private void CreateTemporaryBatchFile()
388391
else if (CommandArguments != null && CommandArguments.Length > 0)
389392
{
390393
// If no Command but we have CommandArguments, treat the first argument as the command
391-
// and the rest as arguments
392-
sw.Write(CommandArguments[0]);
394+
// and the rest as arguments. Escape the command if it contains special characters.
395+
if (NativeMethodsShared.IsUnixLike)
396+
{
397+
sw.Write(EscapeArgumentForUnix(CommandArguments[0]));
398+
}
399+
else
400+
{
401+
sw.Write(EscapeArgumentForWindows(CommandArguments[0]));
402+
}
393403

394404
for (int i = 1; i < CommandArguments.Length; i++)
395405
{

0 commit comments

Comments
 (0)