Files
reverse-engineering-anything/src/domain/javascript/javascriptSemanticAnalysis.child_process_options_shape.test.ts
T
吴佳浩Alben f285fd4f82 fix(javascript): emit no child process for a literal non-object options slot (#8019)
* 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 <expr>` 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.
2026-11-04 19:39:43 +08:00

110 lines
4.8 KiB
TypeScript

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 <expr>` 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"]);
});