Skip to content

Commit 24b52b7

Browse files
committed
bananas
1 parent e4be12f commit 24b52b7

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

src/lib/util.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import argvSplit from 'argv-split';
22
import fs from 'fs-extra';
33
import E from './errors.js';
44
import path from 'path';
5+
import os from 'os';
56
import semver from 'semver';
67
import which from 'which';
8+
import child_process from 'child_process';
79
import { fileURLToPath } from 'url';
810
import { packageDirectorySync } from 'pkg-dir';
11+
import { execPath } from 'process';
912

1013
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1114

@@ -264,3 +267,27 @@ export function wrap(str, width, indent) {
264267
})
265268
.join('\n');
266269
}
270+
271+
// cache to avoid extra lookups
272+
let _nodePath;
273+
export function nodePath() {
274+
if (!_nodePath) {
275+
const execPath = process.execPath;
276+
// cannot exec cmd on windows on new versions of node https://nodejs.org/en/blog/vulnerability/april-2024-security-releases-2
277+
// CVE-2024-27980. Can't pass shell: true to get around this on windows since it breaks non-shell executions.
278+
// Can't imagine node would be a bat but who knows. It's .cmd on windows often.
279+
if (os.platform() === 'win32' && [ 'cmd', 'bat' ].includes(path.extname(execPath))) {
280+
// try and see if the node.exe lives in the same dir
281+
const newNodePath = execPath.replace(new RegExp(`${path.extname(execPath)}$`), 'exe');
282+
try {
283+
fs.statSync(newNodePath);
284+
_nodePath = newNodePath;
285+
} catch (err) {
286+
_nodePath = 'node.exe';
287+
}
288+
} else {
289+
_nodePath = execPath;
290+
}
291+
}
292+
return _nodePath;
293+
}

src/parser/extension.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import E from '../lib/errors.js';
44
import helpCommand from '../commands/help.js';
55
import _path from 'path';
66

7-
import { declareCLIKitClass, filename, findPackage, isExecutable } from '../lib/util.js';
7+
import { declareCLIKitClass, filename, findPackage, isExecutable, nodePath } from '../lib/util.js';
88
import { spawn } from 'child_process';
99

1010
const { log, warn } = debug('cli-kit:extension');
@@ -114,7 +114,7 @@ export default class Extension {
114114
const makeDefaultAction = main => {
115115
return async ({ __argv, cmd }) => {
116116
process.argv = [
117-
'node',
117+
nodePath(),
118118
main
119119
];
120120

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import CLI from '../../../src/index.js';
2+
import { nodePath } from '../../../src/lib/util.js';
23

34
new CLI({
4-
extensions: [ 'node' ]
5+
extensions: [ nodePath() ]
56
}).exec();

test/test-extension.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { fileURLToPath } from 'url';
77
import { platform } from 'os';
88
import { spawnSync } from 'child_process';
99
import { WritableStream } from 'memory-streams';
10+
import { nodePath } from '../src/lib/util.js';
1011

1112
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1213

@@ -58,14 +59,14 @@ describe('Extension', () => {
5859
delete env.SNOOPLOGG;
5960
const args = [
6061
path.join(__dirname, 'examples', 'external-binary', 'extbin.js'),
61-
'node',
62+
nodePath(),
6263
'-e',
6364
'console.log(\'foo\');'
6465
];
6566

6667
let s = '',o = '',e = '';
6768
try {
68-
const { status, stdout, stderr } = spawnSync('where', [ 'node.exe' ]);
69+
const { status, stdout, stderr } = spawnSync('where', [ nodePath() ]);
6970
s = status; o = stdout; e = stderr;
7071
} catch (e) {
7172
console.log('EROROROROROR!', e);
@@ -74,7 +75,7 @@ describe('Extension', () => {
7475
console.log('PLATFORM:', platform());
7576
}
7677

77-
const { status, stdout, stderr } = spawnSync('node.exe', args, {
78+
const { status, stdout, stderr } = spawnSync(nodePath(), args, {
7879
env,
7980
shell: platform() === 'win32'
8081
});
@@ -90,7 +91,7 @@ describe('Extension', () => {
9091
const env = { ...process.env };
9192
delete env.SNOOPLOGG;
9293

93-
const { status, stdout, stderr } = spawnSync('node', [
94+
const { status, stdout, stderr } = spawnSync(nodePath(), [
9495
path.join(__dirname, 'examples', 'run-node', 'run.js'), 'run', 'console.log(\'It works\')'
9596
], {
9697
env,
@@ -186,7 +187,7 @@ describe('Extension', () => {
186187
const env = { ...process.env };
187188
delete env.SNOOPLOGG;
188189

189-
const { status, stdout, stderr } = spawnSync('node', [
190+
const { status, stdout, stderr } = spawnSync(nodePath(), [
190191
path.join(__dirname, 'examples', 'external-js-file', 'extjsfile.js'), 'simple', 'foo', 'bar'
191192
], {
192193
env
@@ -202,7 +203,7 @@ describe('Extension', () => {
202203
const env = { ...process.env };
203204
delete env.SNOOPLOGG;
204205

205-
const { status, stdout, stderr } = spawnSync('node', [
206+
const { status, stdout, stderr } = spawnSync(nodePath(), [
206207
path.join(__dirname, 'examples', 'external-module', 'extmod.js'), 'foo', 'bar'
207208
], {
208209
env

test/test-parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import CLI, { ansi, Terminal } from '../src/index.js';
22
import path from 'path';
33
import { expect } from 'chai';
4-
import { platform } from 'os';
54
import { fileURLToPath } from 'url';
65
import { spawnSync } from 'child_process';
76
import { WritableStream } from 'memory-streams';
7+
import { nodePath } from '../src/lib/util.js';
88

99
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1010

@@ -87,7 +87,7 @@ describe('Parser', () => {
8787
const env = Object.assign({}, process.env);
8888
delete env.SNOOPLOGG;
8989

90-
const { status, stdout } = spawnSync('node', [ path.join(__dirname, 'examples', 'version-test', 'ver.js'), '--version' ], {
90+
const { status, stdout } = spawnSync(nodePath(), [ path.join(__dirname, 'examples', 'version-test', 'ver.js'), '--version' ], {
9191
env
9292
});
9393
expect(status).to.equal(0);

0 commit comments

Comments
 (0)