src: fix escaping of single quotes in task runner

Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/64089
Refs: https://hackerone.com/reports/3817602
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
This commit is contained in:
Antoine du Hamel authored and GitHub committed 2026-07-04 08:56:09 +00:00
1 parent 387332fbf3
commit e76c573e45
5 files changed
+11 -11

No files matched your search

+2 -2
View File
@@ -171,10 +171,10 @@ std::string EscapeShell(const std::string_view input) {
escaped = std::regex_replace(escaped, leadingQuotePairs, "");
escaped = std::regex_replace(escaped, tripleSingleQuote, "\\\"");
#else
// Replace single quotes("'") with "\\'" and wrap the result
// Replace single quotes("'") with `'"'"'` and wrap the result
// in single quotes.
std::string escaped =
std::regex_replace(std::string(input), std::regex("'"), "\\'");
std::regex_replace(std::string(input), std::regex("'"), "'\"'\"'");
escaped = "'" + escaped + "'";
// Remove excessive quote pairs and handle edge cases
static const std::regex tripleSingleQuote("\\\\'''");
+3 -3
View File
@@ -28,12 +28,12 @@ TEST_F(TaskRunnerTest, EscapeShell) {
{"test words", "'test words'"},
{"$1", "'$1'"},
{"\"$1\"", "'\"$1\"'"},
{"'$1'", "'\\'$1\\''"},
{"'$1'", "\"'\"'$1'\"'\"''"},
{"\\$1", "'\\$1'"},
{"--arg=\"$1\"", "'--arg=\"$1\"'"},
{"--arg=node exec -c \"$1\"", "'--arg=node exec -c \"$1\"'"},
{"--arg=node exec -c '$1'", "'--arg=node exec -c \\'$1\\''"},
{"'--arg=node exec -c \"$1\"'", "'\\'--arg=node exec -c \"$1\"\\''"}
{"--arg=node exec -c '$1'", "'--arg=node exec -c '\"'\"'$1'\"'\"''"},
{"'--arg=node exec -c \"$1\"'", "\"'\"'--arg=node exec -c \"$1\"'\"'\"''"}
#endif
};
+1 -1
View File
@@ -1,3 +1,3 @@
#!/bin/bash
echo "Arguments: '$@'"
echo "The total number of arguments are: $#"
echo "The total number of arguments is: $#"
+1 -1
View File
@@ -12,4 +12,4 @@ for %%x in (%*) do (
)
@echo Raw '%*'
@echo Arguments: '%output%'
@echo The total number of arguments are: %argv%
@echo The total number of arguments is: %argv%
+4 -4
View File
@@ -143,15 +143,15 @@ describe('node --run [command]', () => {
it('appends positional arguments', async () => {
const child = await common.spawnPromisified(
process.execPath,
[ '--run', `positional-args${envSuffix}`, '--', '--help "hello world test"', 'A', 'B', 'C'],
[ '--run', `positional-args${envSuffix}`, '--', '--help "hello world test"', 'A', 'B', 'C', 'I think therefore I\'m'],
{ cwd: fixtures.path('run-script') },
);
if (common.isWindows) {
assert.match(child.stdout, /Arguments: '--help ""hello world test"" A B C'/);
assert.match(child.stdout, /Arguments: '--help ""hello world test"" A B C I think therefore I'm'/);
} else {
assert.match(child.stdout, /Arguments: '--help "hello world test" A B C'/);
assert.match(child.stdout, /Arguments: '--help "hello world test" A B C I think therefore I'm'/);
}
assert.match(child.stdout, /The total number of arguments are: 4/);
assert.match(child.stdout, /The total number of arguments is: 5/);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});