src: escape Windows environment variables in task runner

Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/65217
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Tierney Cyren <hello@bnb.im>
This commit is contained in:
Antoine du Hamel committed 2026-08-26 00:56:03 +02:00
1 parent 6ca9c1e078
commit 922d47cbd7
2 files changed
+18 -2

No files matched your search

+2 -1
View File
@@ -154,7 +154,7 @@ std::string EscapeShell(const std::string_view input) {
}
static constexpr std::string_view forbidden_characters =
"[\t\n\r \"#$&'()*;<>?\\\\`|~]";
"[\t\n\r \"#$&'()*;<>%?\\\\`|~]";
// Check if input contains any forbidden characters
// If it doesn't, return the input as is.
@@ -174,6 +174,7 @@ std::string EscapeShell(const std::string_view input) {
static const std::regex tripleSingleQuote("\\\\\"\"\"");
escaped = std::regex_replace(escaped, leadingQuotePairs, "");
escaped = std::regex_replace(escaped, tripleSingleQuote, "\\\"");
escaped = std::regex_replace(escaped, std::regex("%"), "^%");
#else
// Replace single quotes("'") with `'"'"'` and wrap the result
// in single quotes.
+16 -1
View File
@@ -12,7 +12,7 @@ const fixtures = require('../common/fixtures');
const tmpdir = require('../common/tmpdir');
const envSuffix = common.isWindows ? '-windows' : '';
describe('node --run [command]', () => {
describe('node --run [command]', { concurrency: !process.env.TEST_PARALLEL }, () => {
it('returns error on non-existent file', async () => {
const child = await common.spawnPromisified(
process.execPath,
@@ -286,4 +286,19 @@ describe('node --run [command]', () => {
assert.strictEqual(child.stdout, '');
assert.strictEqual(child.code, 1);
});
it('escapes shell characters', async () => {
const child = await common.spawnPromisified(
process.execPath,
[ '--run', `positional-args${envSuffix}`, '--', '%PAYLOAD%', '$PAYLOAD'],
{ cwd: fixtures.path('run-script'), env: { ...process.env, PAYLOAD: 'env value' } },
);
assert.strictEqual(
child.stdout,
common.isWindows ?
`Raw '"^%PAYLOAD^%" "$PAYLOAD"'\r\nArguments: '%PAYLOAD% $PAYLOAD'\r\nThe total number of arguments is: 2\r\n` :
"Arguments: '%PAYLOAD% $PAYLOAD'\nThe total number of arguments is: 2\n");
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
});