Skip to content

Commit d564848

Browse files
juliodelimasclaude
andcommitted
Fix: spawn EINVAL on Windows when running execute_test_plan
Node throws EINVAL when spawning a .bat/.cmd file directly on Windows without shell:true (CVE-2024-27980 hardening), and resolveJmeterBin() returns jmeter.bat there, so every execution failed on Windows. Route .bat/.cmd invocations through the shell and manually quote args containing spaces, since Node's shell:true on Windows just joins command+args without escaping. Also switch stopExecution to taskkill /t on Windows, since the JMeter process now runs under cmd.exe and killing only the registered pid would orphan java.exe. Fixes github/github-mcp-server#3180 Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Sa9f6K3PgpHndMtPfzeAWa
1 parent 8974956 commit d564848

3 files changed

Lines changed: 73 additions & 5 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"start": "node dist/index.js",
1919
"dev": "tsx src/index.ts",
2020
"pretest": "npm run build",
21-
"test": "tsx --test test/serializer.test.ts test/jtlParser.test.ts test/tools.stdio.test.ts test/workflow.stdio.test.ts",
21+
"test": "tsx --test test/serializer.test.ts test/jtlParser.test.ts test/processManager.test.ts test/tools.stdio.test.ts test/workflow.stdio.test.ts",
2222
"test:integration": "npm run build && tsx --test test/integration.execution.test.ts",
2323
"test:all": "npm test && npm run test:integration"
2424
},

‎src/execution/processManager.ts‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type ChildProcess, spawn } from "node:child_process";
1+
import { type ChildProcess, execFileSync, spawn } from "node:child_process";
22
import { closeSync, existsSync, openSync, readFileSync, writeFileSync } from "node:fs";
33
import path from "node:path";
44
import { resolveJmeterBin } from "../jmeter.js";
@@ -47,6 +47,26 @@ function hasNodeOfType(root: TestNode, type: TestNode["type"]): boolean {
4747
return root.children.some((child) => hasNodeOfType(child, type));
4848
}
4949

50+
/**
51+
* Node refuses to spawn .bat/.cmd files directly on Windows (throws EINVAL) unless
52+
* `shell: true` is set - see https://nodejs.org/en/blog/vulnerability/february-2024-security-releases
53+
* (CVE-2024-27980). With `shell: true` on Windows, Node joins command+args with plain
54+
* spaces before handing the line to cmd.exe, so we must quote any piece containing
55+
* whitespace ourselves (JMETER_HOME and JMX paths routinely contain spaces).
56+
*/
57+
export function buildSpawnInvocation(
58+
bin: string,
59+
args: string[],
60+
platform: NodeJS.Platform,
61+
): { command: string; args: string[]; shell: boolean } {
62+
const isWindowsBatch = platform === "win32" && /\.(bat|cmd)$/i.test(bin);
63+
if (!isWindowsBatch) {
64+
return { command: bin, args, shell: false };
65+
}
66+
const quote = (value: string): string => (/[\s"]/.test(value) ? `"${value.replace(/"/g, '\\"')}"` : value);
67+
return { command: quote(bin), args: args.map(quote), shell: true };
68+
}
69+
5070
export function startExecution(planId: string): { executionId: string; status: ExecutionStatus } {
5171
const jmeterBin = resolveJmeterBin();
5272
const plan = readPlan(planId);
@@ -71,11 +91,16 @@ export function startExecution(planId: string): { executionId: string; status: E
7191
writeFileSync(jmxPath, jmx, "utf-8");
7292

7393
const stdoutFd = openSync(stdoutLogPath, "a");
74-
const child = spawn(
94+
const invocation = buildSpawnInvocation(
7595
jmeterBin,
7696
["-n", "-t", jmxPath, "-j", jmeterLogPath, "-Jjmeter.save.saveservice.output_format=csv"],
77-
{ cwd: execDir, stdio: ["ignore", stdoutFd, stdoutFd] },
97+
process.platform,
7898
);
99+
const child = spawn(invocation.command, invocation.args, {
100+
cwd: execDir,
101+
stdio: ["ignore", stdoutFd, stdoutFd],
102+
shell: invocation.shell,
103+
});
79104
closeSync(stdoutFd);
80105

81106
const meta: ExecutionMeta = {
@@ -118,8 +143,14 @@ export function stopExecution(executionId: string): { stopped: boolean; message:
118143
if (meta.status !== "running") {
119144
return { stopped: false, message: `Execution is already ${meta.status}.` };
120145
}
121-
const child = registry.get(executionId);
122146
try {
147+
if (process.platform === "win32") {
148+
// JMeter runs under cmd.exe (see buildSpawnInvocation), so killing just the
149+
// registered pid would leave the actual java.exe orphaned; /t kills the tree.
150+
execFileSync("taskkill", ["/pid", String(meta.pid), "/t", "/f"]);
151+
return { stopped: true, message: "Terminated the JMeter process tree." };
152+
}
153+
const child = registry.get(executionId);
123154
if (child) {
124155
child.kill("SIGTERM");
125156
} else {

‎test/processManager.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { test } from "node:test";
2+
import assert from "node:assert/strict";
3+
import { buildSpawnInvocation } from "../src/execution/processManager.js";
4+
5+
test("buildSpawnInvocation leaves non-Windows spawns untouched", () => {
6+
const result = buildSpawnInvocation("/opt/jmeter/bin/jmeter", ["-n", "-t", "/tmp/plan.jmx"], "linux");
7+
assert.equal(result.shell, false);
8+
assert.equal(result.command, "/opt/jmeter/bin/jmeter");
9+
assert.deepEqual(result.args, ["-n", "-t", "/tmp/plan.jmx"]);
10+
});
11+
12+
test("buildSpawnInvocation leaves a non-batch Windows binary untouched", () => {
13+
const result = buildSpawnInvocation("C:\\jmeter\\bin\\jmeter.exe", ["-n"], "win32");
14+
assert.equal(result.shell, false);
15+
assert.equal(result.command, "C:\\jmeter\\bin\\jmeter.exe");
16+
});
17+
18+
test("buildSpawnInvocation routes .bat files through the shell on Windows", () => {
19+
const result = buildSpawnInvocation("C:\\jmeter\\bin\\jmeter.bat", ["-n", "-t", "plan.jmx"], "win32");
20+
assert.equal(result.shell, true);
21+
assert.equal(result.command, "C:\\jmeter\\bin\\jmeter.bat");
22+
});
23+
24+
test("buildSpawnInvocation quotes Windows paths containing spaces", () => {
25+
const result = buildSpawnInvocation(
26+
"C:\\Program Files\\jmeter\\bin\\jmeter.bat",
27+
["-n", "-t", "C:\\Users\\Jane Doe\\plan.jmx"],
28+
"win32",
29+
);
30+
assert.equal(result.command, '"C:\\Program Files\\jmeter\\bin\\jmeter.bat"');
31+
assert.deepEqual(result.args, ["-n", "-t", '"C:\\Users\\Jane Doe\\plan.jmx"']);
32+
});
33+
34+
test("buildSpawnInvocation is case-insensitive for .cmd files", () => {
35+
const result = buildSpawnInvocation("C:\\jmeter\\bin\\jmeter.CMD", [], "win32");
36+
assert.equal(result.shell, true);
37+
});

0 commit comments

Comments
 (0)