Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
src: escape Windows environment variables in task runner
Signed-off-by: Antoine du Hamel <[email protected]>
  • Loading branch information
aduh95 committed Aug 11, 2026
commit 38d3b15b8755fa4d3421f9f6fdbf170cb6beacba
3 changes: 2 additions & 1 deletion src/node_task_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
16 changes: 15 additions & 1 deletion test/parallel/test-node-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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,
Expand Down Expand Up @@ -286,4 +286,18 @@
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 ?

Check failure on line 297 in test/parallel/test-node-run.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 23 spaces but found 6
`Raw '"^%PAYLOAD^%" "$PAYLOAD"'\r\nArguments: '%PAYLOAD% $PAYLOAD'\r\nThe total number of arguments is: 2\r\n` :

Check failure on line 298 in test/parallel/test-node-run.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 25 spaces but found 8
"Arguments: '%PAYLOAD% $PAYLOAD'\nThe total number of arguments is: 2\n");

Check failure on line 299 in test/parallel/test-node-run.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 25 spaces but found 8
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
});
Loading