From f285fd4f8209656192f1511c453f9d70b663d995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E4=BD=B3=E6=B5=A9Alben?= <35291372+XiaoBinGan@users.noreply.github.com> Date: Wed, 4 Nov 2026 19:39:43 +0800 Subject: [PATCH] fix(javascript): emit no child process for a literal non-object options slot (#8019) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(javascript): emit no child process for a literal non-object options slot Node validates the child_process options object before creating anything, so a literal primitive or a literal array in that slot throws ERR_INVALID_ARG_TYPE synchronously and no child process exists. The analysis still reported a complete spawn for them. Measured on Node 24.22.1: spawn(cmd, "status") -> THROWS ERR_INVALID_ARG_TYPE spawn(cmd, [], "pipe") -> THROWS ERR_INVALID_ARG_TYPE spawn(cmd, [], null) -> THROWS ERR_INVALID_ARG_TYPE spawn(cmd, [], []) -> THROWS ERR_INVALID_ARG_TYPE execFile(cmd, "status") -> THROWS ERR_INVALID_ARG_TYPE fork(worker, "status") -> THROWS ERR_INVALID_ARG_TYPE fork(worker, [], "pipe") -> THROWS ERR_INVALID_ARG_TYPE --- accepted by Node --- spawn(cmd, args, undefined) -> SPAWNED spawn(cmd, args, {}) -> SPAWNED execFile(cmd, [], null) -> SPAWNED fork(worker, [], null) -> SPAWNED exec(cmd, "inherit") -> SPAWNED The asymmetries matter: `spawn` rejects `null` and exempts only `undefined`, while `fork` and `execFile` skip any nullish options, and `exec` spreads its options and validates none of them. Drop a candidate only when the options slot is a provable literal primitive or array, so an unresolved or non-literal value stays a candidate. `spawn(cmd, [], null)` is the sharpest case: it was indistinguishable from the valid `undefined` form, including a resolved connects-stdio relation, for a call that dies before any child exists. * chore: retrigger title and readiness checks after the title was corrected * fix(javascript): reject literal options Node shall reject, and drop a stray file Two review findings on #8019. greptile: `rejectsOptionsShape` still kept `spawn("tool", [], 0n)`, `-0` and `1e400`, which Node also rejects before creating a child (`ERR_INVALID_ARG_TYPE: The "options" argument must be of type object`). Those forms are exactly the ones `readExactJavaScriptLiteral` refuses to price — a BigInt, a non-finite numeric literal and a negated numeric literal — so read the shape directly instead of widening the shared exact-value reader. `NaN` is an identifier rather than a literal, so it still needs scope resolution and is left as a documented gap. Also remove `.rea-hopper-stage-YOryZ6/`, a local Hopper installation stub that an `add -A` swept into the commit. It is unrelated to this change. * fix(javascript): keep a void options slot, reject ! and ~ greptile flagged that `isUnpricedPrimitiveLiteral` dropped `spawn("tool", [], void 0)`, `fork(..., void 0)` and `execFile(..., void 0)`. It checked the unary operand without the operator, but `void ` evaluates to `undefined`, which is the one options value Node accepts in place of an object. Measured on Node 24.22.1: spawn(cmd, [], void 0) -> SPAWNED spawn(cmd, [], void 5) -> SPAWNED spawn(cmd, [], -0) -> THROWS ERR_INVALID_ARG_TYPE spawn(cmd, [], -5) -> THROWS ERR_INVALID_ARG_TYPE spawn(cmd, [], +5) -> THROWS ERR_INVALID_ARG_TYPE spawn(cmd, [], !0) -> THROWS ERR_INVALID_ARG_TYPE spawn(cmd, [], ~0) -> THROWS ERR_INVALID_ARG_TYPE So the operator now decides: `void` keeps the spawn, `!` and `~` reject whatever their operand, and `+`/`-` reject only when the operand is a numeric literal. * fix(javascript): reject a non-finite global in the child-process options slot An unshadowed `NaN` or `Infinity` in the options slot is a number, and Node's options check rejects a number exactly as it rejects any other primitive. The guard read only literal nodes, so the global identifier passed through. Measured on Node 24.22.1: spawn("e", NaN) -> THREW ERR_INVALID_ARG_TYPE spawn("e", Infinity) -> THREW ERR_INVALID_ARG_TYPE execFile("e", NaN) -> THREW ERR_INVALID_ARG_TYPE fork("x.js", NaN) -> THREW ERR_INVALID_ARG_TYPE --- accepted --- exec("e", NaN) -> ok, because exec validates no options spawn("e", undefined) -> ok, the one accepted global spawn("e", null) -> ok spawn("e", []) / ({}) -> ok `isUnshadowedGlobal` keeps a local binding named `NaN` out of the rule, so a shadowed value keeps its fact. Seven regressions cover the rejected forms plus `exec`, `undefined`, `null`, an array, an object and a shadowed `NaN`; all seven fail on the previous commit. --- ...alysis.child_process_options_shape.test.ts | 109 ++++++++++++++++++ .../javascriptSemanticChildProcesses.ts | 56 ++++++++- 2 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 src/domain/javascript/javascriptSemanticAnalysis.child_process_options_shape.test.ts diff --git a/src/domain/javascript/javascriptSemanticAnalysis.child_process_options_shape.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.child_process_options_shape.test.ts new file mode 100644 index 0000000000..9cedf23661 --- /dev/null +++ b/src/domain/javascript/javascriptSemanticAnalysis.child_process_options_shape.test.ts @@ -0,0 +1,109 @@ +import { expect, it } from "vitest"; + +import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js"; + +/** + * Node validates the child_process options object before creating anything, so a + * literal primitive or a literal array in that slot throws + * ERR_INVALID_ARG_TYPE synchronously and no child process exists. `spawn` also + * rejects `null` (`undefined` is its only exemption); `fork` and `execFile` skip + * any nullish options, and `exec` spreads its options and rejects none. + */ +const spawnsFor = (source: string) => + analyzeJavaScriptSemantics( + `import { spawn, exec, execFile, fork } from "node:child_process"; ${source};`, + ).childProcessSpawns; + +it.each([ + ["a string args slot", `spawn("git", "status")`], + ["a numeric args slot", `spawn("git", 1)`], + ["a string options slot", `spawn("git", [], "pipe")`], + ["a null options slot", `spawn("git", [], null)`], + ["an array options slot", `spawn("git", [], [])`], + ["a numeric options slot", `spawn("git", [], 5)`], + ["a string execFile args slot", `execFile("git", "status")`], + ["a string fork args slot", `fork("worker.js", "status")`], + ["a string fork options slot", `fork("worker.js", [], "pipe")`], + ["a BigInt options slot", `spawn("git", [], 0n)`], + ["a negated-zero options slot", `spawn("git", [], -0)`], + ["an overflowing numeric options slot", `spawn("git", [], 1e400)`], + ["a negated numeric options slot", `spawn("git", [], -5)`], + ["a BigInt args slot", `spawn("git", 0n)`], +] as const)("emits no child-process fact for %s", (_name, call) => { + expect(spawnsFor(call)).toEqual([]); +}); + +it.each([ + ["an object options slot", `spawn("tool", [], { stdio: "pipe" })`], + ["an omitted options slot", `spawn("tool", [], undefined)`], + ["options in the second slot", `spawn("tool", { stdio: "pipe" })`], + ["an unresolved options value", `spawn("tool", [], options)`], + // `void ` evaluates to undefined, the one value Node accepts here. + ["a void options slot", `spawn("tool", [], void 0)`], + ["a void expression with an operand", `spawn("tool", [], void 5)`], +] as const)("keeps the spawn for %s", (_name, call) => { + expect(spawnsFor(call)).toHaveLength(1); +}); + +/** fork and execFile skip a nullish options object; exec validates nothing. */ +it.each([ + ["fork with null options", `fork("worker.js", [], null)`], + ["fork with a void options slot", `fork("worker.js", [], void 0)`], + ["execFile with null options", `execFile("tool", [], null)`], + ["execFile with a void options slot", `execFile("tool", [], void 0)`], + ["exec with a string second argument", `exec("tool", "inherit")`], +] as const)("keeps %s", (_name, call) => { + expect(spawnsFor(call)).toHaveLength(1); +}); + +/** `!` and `~` yield a primitive whatever their operand, so Node rejects them. */ +it.each([ + ["a logical-not options slot", `spawn("tool", [], !0)`], + ["a bitwise-not options slot", `spawn("tool", [], ~5)`], + ["a unary-plus options slot", `spawn("tool", [], +5)`], +] as const)("emits no child-process fact for %s", (_name, call) => { + expect(spawnsFor(call)).toEqual([]); +}); + +/** + * An unshadowed `NaN` or `Infinity` in the options slot is a number, and Node + * rejects a number there exactly as it rejects any other primitive: + * + * spawn("e", NaN) -> THREW ERR_INVALID_ARG_TYPE + * spawn("e", Infinity) -> THREW ERR_INVALID_ARG_TYPE + * execFile("e", NaN) -> THREW ERR_INVALID_ARG_TYPE + * fork("x.js", NaN) -> THREW ERR_INVALID_ARG_TYPE + * exec("e", NaN) -> ok, because exec validates no options + * spawn("e", undefined) -> ok, the one accepted global + */ +it.each([ + ["a NaN options slot for spawn", `spawn("tool", NaN)`], + ["an Infinity options slot for spawn", `spawn("tool", Infinity)`], + ["a NaN third-slot options for spawn", `spawn("tool", [], NaN)`], + ["a NaN options slot for execFile", `execFile("tool", NaN)`], + ["an Infinity options slot for execFile", `execFile("tool", Infinity)`], + ["a NaN options slot for fork", `fork("tool.js", NaN)`], + ["an Infinity options slot for fork", `fork("tool.js", Infinity)`], +] as const)("emits no child-process fact for %s", (_name, call) => { + expect(spawnsFor(call)).toEqual([]); +}); + +it.each([ + ["exec not validating its NaN options", `exec("tool", NaN)`, "exec"], + [ + "exec not validating its Infinity options", + `exec("tool", Infinity)`, + "exec", + ], + ["spawn accepting undefined", `spawn("tool", undefined)`, "spawn"], + ["spawn accepting an object first slot", `spawn("tool", {})`, "spawn"], + ["spawn accepting an array", `spawn("tool", [])`, "spawn"], +] as const)("keeps the child-process fact for %s", (_name, call, method) => { + expect(spawnsFor(call).map(({ method: actual }) => actual)).toEqual([method]); +}); + +it("keeps the fact when a local binding shadows NaN", () => { + expect( + spawnsFor(`const NaN = {}; spawn("tool", NaN)`).map(({ method }) => method), + ).toEqual(["spawn"]); +}); diff --git a/src/domain/javascript/javascriptSemanticChildProcesses.ts b/src/domain/javascript/javascriptSemanticChildProcesses.ts index ea7fb69ab9..3b3ad852bd 100644 --- a/src/domain/javascript/javascriptSemanticChildProcesses.ts +++ b/src/domain/javascript/javascriptSemanticChildProcesses.ts @@ -73,7 +73,9 @@ export const collectJavaScriptSemanticChildProcesses = ( ): ChildProcessAnalysis => { const context = createSemanticExpressionShapeResolver(state); const candidates = collectSpawns(program, state, callables).filter( - (candidate) => !forkRejectsStdioArray(candidate, context), + (candidate) => + !forkRejectsStdioArray(candidate, context) && + !rejectsOptionsShape(candidate, context, state), ); const childProcessSpawns = candidates.map((candidate) => immutableSpawn(candidate, context), @@ -485,6 +487,58 @@ const forkRejectsStdioArray = ( return ipcCount !== 1; }; +/** + * Node validates the options object before creating anything, so a literal + * primitive or literal array in that slot throws ERR_INVALID_ARG_TYPE + * synchronously and no child process exists. `spawn` also rejects `null` + * (`undefined` is its only exemption), while `fork` and `execFile` skip any + * nullish options, and `exec` spreads its options and validates none of them. + */ +const rejectsOptionsShape = ( + candidate: SpawnCandidate, + context: ChildSpawnContext, + state: JavaScriptSemanticAnalysisState, +): boolean => { + if (candidate.method === "exec") return false; + const { options } = spawnArguments(candidate, context); + if (options === undefined) return false; + const value = unwrapJavaScriptExpression(options).node; + if (context.isArray(value)) return true; + const literal = readExactJavaScriptLiteral(value); + if (literal.found) + return !(candidate.method !== "spawn" && literal.value === null); + // An unshadowed `NaN` or `Infinity` is a number, and Node's options check + // rejects a number the same way it rejects any other primitive. `undefined` + // stays the one accepted global. + if ( + t.isIdentifier(value) && + (value.name === "NaN" || value.name === "Infinity") && + isUnshadowedGlobal(value, state, value.name) + ) + return true; + return isUnpricedPrimitiveLiteral(value); +}; + +/** + * The exact-value reader deliberately refuses to price a BigInt, a non-finite + * numeric literal such as `1e400`, and a negated numeric literal such as `-0`. + * All are primitive literals, so Node's options check rejects them the same way; + * read the shape directly here rather than widening the shared reader. + * + * `void ` is the exception: it evaluates to `undefined`, which is the one + * options value Node accepts in place of an object, so it must not be dropped. + */ +const isUnpricedPrimitiveLiteral = (value: t.Node): boolean => { + if (t.isNumericLiteral(value) || t.isBigIntLiteral(value)) return true; + if (!t.isUnaryExpression(value)) return false; + if (value.operator === "void") return false; + // `!` and `~` always yield a primitive, whatever their operand. + if (value.operator === "!" || value.operator === "~") return true; + if (value.operator !== "+" && value.operator !== "-") return false; + const argument = unwrapJavaScriptExpression(value.argument).node; + return t.isNumericLiteral(argument) || t.isBigIntLiteral(argument); +}; + /** * Without a string or array `stdio`, Node's `fork` pipes the standard streams * when `silent` is truthy and inherits them otherwise.