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

CommandBuilder: Use more robust quoting #1540

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -297,7 +299,14 @@ private static String escape(String str, char... escapeChars) {
}

public static String toBatchStringLiteral(String s) {
return containsEscape(s, " \t\"^&<>|") ? '"' + escape(s, '\\', '"') + '"' : s;
String escape = " \t\"^&<>|?*";
if (containsEscape(s, escape))
// The argument has not been quoted, add quotes.
// See explanation at https://github.com/Artoria2e5/node/blob/fix!/child-process-args/lib/child_process.js
// about making the string "inert to CMD", and associated unit tests
return '"' + s.replaceAll("(\\\\*)($|\")\"", "$1$1$2").replace("\"", "\"\"") + '"';
else
return s;
}

public static String toShellStringLiteral(String s) {
Expand Down