|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Node.js on Windows should not be able to spawn batch files directly, |
| 4 | +// only when the 'shell' option is set. An undocumented feature of the |
| 5 | +// Win32 CreateProcess API allows spawning .bat and .cmd files directly |
| 6 | +// but it does not sanitize arguments. We cannot do that automatically |
| 7 | +// because it's sometimes impossible to escape arguments unambiguously. |
| 8 | +// |
| 9 | +// Expectation: spawn() and spawnSync() raise EINVAL if and only if: |
| 10 | +// |
| 11 | +// 1. 'shell' option is unset |
| 12 | +// 2. Platform is Windows |
| 13 | +// 3. Filename ends in .bat or .cmd, case-insensitive |
| 14 | +// |
| 15 | +// exec() and execSync() are unchanged. |
| 16 | + |
| 17 | +const common = require('../common'); |
| 18 | +const cp = require('child_process'); |
| 19 | +const assert = require('assert'); |
| 20 | +const { isWindows } = common; |
| 21 | + |
| 22 | +const arg = '--security-revert=CVE-2024-27980'; |
| 23 | +const isRevert = process.execArgv.includes(arg); |
| 24 | + |
| 25 | +const expectedCode = isWindows && !isRevert ? 'EINVAL' : 'ENOENT'; |
| 26 | +const expectedStatus = isWindows ? 1 : 127; |
| 27 | + |
| 28 | +const suffixes = |
| 29 | + 'BAT bAT BaT baT BAt bAt Bat bat CMD cMD CmD cmD CMd cMd Cmd cmd' |
| 30 | + .split(' '); |
| 31 | + |
| 32 | +if (process.argv[2] === undefined) { |
| 33 | + const a = cp.spawnSync(process.execPath, [__filename, 'child']); |
| 34 | + const b = cp.spawnSync(process.execPath, [arg, __filename, 'child']); |
| 35 | + assert.strictEqual(a.status, 0); |
| 36 | + assert.strictEqual(b.status, 0); |
| 37 | + return; |
| 38 | +} |
| 39 | + |
| 40 | +function testExec(filename) { |
| 41 | + return new Promise((resolve) => { |
| 42 | + cp.exec(filename).once('exit', common.mustCall(function(status) { |
| 43 | + assert.strictEqual(status, expectedStatus); |
| 44 | + resolve(); |
| 45 | + })); |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +function testExecSync(filename) { |
| 50 | + let e; |
| 51 | + try { |
| 52 | + cp.execSync(filename); |
| 53 | + } catch (_e) { |
| 54 | + e = _e; |
| 55 | + } |
| 56 | + if (!e) throw new Error(`Exception expected for ${filename}`); |
| 57 | + assert.strictEqual(e.status, expectedStatus); |
| 58 | +} |
| 59 | + |
| 60 | +function testSpawn(filename, code) { |
| 61 | + // Batch file case is a synchronous error, file-not-found is asynchronous. |
| 62 | + if (code === 'EINVAL') { |
| 63 | + let e; |
| 64 | + try { |
| 65 | + cp.spawn(filename); |
| 66 | + } catch (_e) { |
| 67 | + e = _e; |
| 68 | + } |
| 69 | + if (!e) throw new Error(`Exception expected for ${filename}`); |
| 70 | + assert.strictEqual(e.code, code); |
| 71 | + } else { |
| 72 | + return new Promise((resolve) => { |
| 73 | + cp.spawn(filename).once('error', common.mustCall(function(e) { |
| 74 | + assert.strictEqual(e.code, code); |
| 75 | + resolve(); |
| 76 | + })); |
| 77 | + }); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function testSpawnSync(filename, code) { |
| 82 | + { |
| 83 | + const r = cp.spawnSync(filename); |
| 84 | + assert.strictEqual(r.error.code, code); |
| 85 | + } |
| 86 | + { |
| 87 | + const r = cp.spawnSync(filename, { shell: true }); |
| 88 | + assert.strictEqual(r.status, expectedStatus); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +testExecSync('./nosuchdir/nosuchfile'); |
| 93 | +testSpawnSync('./nosuchdir/nosuchfile', 'ENOENT'); |
| 94 | +for (const suffix of suffixes) { |
| 95 | + testExecSync(`./nosuchdir/nosuchfile.${suffix}`); |
| 96 | + testSpawnSync(`./nosuchdir/nosuchfile.${suffix}`, expectedCode); |
| 97 | +} |
| 98 | + |
| 99 | +go().catch((ex) => { throw ex; }); |
| 100 | + |
| 101 | +async function go() { |
| 102 | + await testExec('./nosuchdir/nosuchfile'); |
| 103 | + await testSpawn('./nosuchdir/nosuchfile', 'ENOENT'); |
| 104 | + for (const suffix of suffixes) { |
| 105 | + await testExec(`./nosuchdir/nosuchfile.${suffix}`); |
| 106 | + await testSpawn(`./nosuchdir/nosuchfile.${suffix}`, expectedCode); |
| 107 | + } |
| 108 | +} |
0 commit comments