enhance(#3617): one canonical Windows binary resolver in the platform seam (epic #3411 Phase 1) (#3621)

* feat(#3411): one canonical Windows binary resolver in the platform seam

CONTEXT.md declares src/shell-command-projection.cts the single OS-facing seam,
but Windows binary resolution had grown four divergent implementations outside
it. #3445 folded two of them together — inside gsd-core/bin/gsd-tools.cjs, not
the seam — so the declaration stayed untrue and execTool still had no handling
at all.

Lift the resolver into the seam as resolveExecutableBinary, and export the half
that actually executes as projectSpawnInvocation: CreateProcess cannot run a
.cmd/.bat, so the cmd.exe mediation is inseparable from the lookup and splitting
them is how the copies accumulated. cmd.exe is invoked with an explicit argv
array, never shell:true — CVE-2024-27980's vector and Node 26's DEP0190.

execTool now resolves on win32. POSIX is a strict no-op by construction, which
matters: execTool rates CRITICAL blast radius (167 symbols, 53 files).
gsd-tools.cjs deletes its private scan and its private mediation and delegates.

Two semantics grown beyond #3445's resolver, both additive: a name already
carrying a PATHEXT-listed extension is tried as-is before the append loop, and a
suffix outside PATHEXT is not treated as an extension.

Refs #3411

* fix(#3411): keep mediating a declared .cmd that PATH resolution misses

Standards review caught a narrowing against the code this replaces. gsd-tools.cjs
computed `target = resolveSpawnBinary(binary) || binary` and keyed the shim test
on `target`, so a declared .cmd mediated whether or not PATH resolution found it.
That is load-bearing: resolveExecutableBinary scans PATH only, while `cmd.exe /c`
also finds a batch file in the current directory.

Mediation now keys on the target — resolved path, else declared name. The ENOENT
contract still holds for BARE unresolved names, which is the case it was written
for. P9/P10 pin both halves.

Spec review found E1/E2/E3/E5 promised by 50-test-matrix.md but never written;
added. E3 is the integration proof that the CVE-relevant mediation fires through
execTool, not only through projectSpawnInvocation in isolation.

Also adds the CONTEXT.md glossary entry for the seam's new resolution ownership
(a PR gate) and the changeset fragment.

Refs #3411

* fix(#3617): pass mediated cmd.exe arguments verbatim so metacharacters cannot inject

The isolated security pass found the mediation shape carried an argument-injection
surface. libuv's quote_cmd_arg force-quotes an argv element only when it contains
a space, tab, or quote — never for a cmd metacharacter — and cmd.exe re-parses
everything after /c. So an arg of a&calc arrived unquoted and cmd ran calc.
Node's own CVE-2024-27980 escaping cannot help: it fires only when the spawned
FILE is the .bat/.cmd, and here the file is cmd.exe.

Caret-escaping is not a fix. It is correct only when libuv does not quote, and
libuv quotes whenever the arg also contains a space — no per-arg transform is
right in both cases. So build the command line and pass it through verbatim, the
shape Rust's std adopted for the sibling CVE-2024-24576: one outer quote pair
that cmd /c strips, every token inside force-quoted, embedded quotes doubled.

An argument containing CR or LF is refused rather than mediated — a newline
cannot be represented in a Windows command line, so mediating would silently
truncate. Failing visibly is correct.

Known limit, documented at the seam: %VAR% still expands inside a /c string and
has no escape outside a batch file. That is information disclosure, not arbitrary
execution, and is the same limit Rust's std documents.

This was byte-for-byte the shape #3445 shipped, so the fix closes it for the
reviewer-lane spawn path too, not only for execTool's newly reachable route.

Refs #3411

* docs(#3617): document the subprocess-execution security posture

Adds Layer 4 to the security model: why GSD never uses shell:true for binary
invocation (CVE-2024-27980, Node 26 DEP0190), why resolution is explicit and
never tries the bare name on Windows (the npm extensionless-shim trap behind
#3275), and why .cmd/.bat mediation builds a verbatim force-quoted command line
rather than relying on default escaping — Node's own CVE protection cannot fire
once the started program is cmd.exe.

The residual %VAR% expansion limit is stated plainly under Trade-offs rather
than left implicit: it is information disclosure, not arbitrary execution, and
callers passing untrusted text to a Windows .cmd should not assume the value
arrives byte-identical.

Docs-only; no code change.

Refs #3411

* chore(#3617): backfill changeset pr number 3621

* fix(#3617): read PATH, PATHEXT and ComSpec case-insensitively

The Windows CI lane on #3621 failed E5, and the root cause was a defect in the
implementation, not the assertion.

Windows names the variable Path, not PATH. process.env is a case-insensitive
proxy, so process.env.PATH works — but execTool builds
{ ...process.env, ...opts.env } whenever a caller supplies opts.env, and
spreading discards the proxy while keeping the OS's actual casing. The exact-case
env['PATH'] lookup then returned undefined, the PATH scan saw zero segments,
resolution returned null, and the change degraded to precisely the spawn ENOENT
it exists to fix. ComSpec and PATHEXT had the same exposure.

#3445's tests never caught it because they pass uppercase keys explicitly, and
neither did the Linux remote runner — this is a defect only the Windows lane
could see.

_envGet resolves a variable by exact match first (so a canonical caller pays no
scan) and falls back to a case-insensitive sweep.

R23 and P16 pin it and were proven RED by execution: with the fix stashed and
build:lib re-run, R23 returned null and P16 returned the cmd.exe default.

R24 was rewritten because the first version was vacuous — it staged foo.CMD, so
the default PATHEXT already contained .CMD and it passed against the broken code
for the wrong reason. It now stages foo.XYZ, an extension absent from the
default, and carries a negative control asserting that dropping the Pathext key
yields null. Re-proven RED the same way.

E5's assertion was corrected alongside the fix: 'PATH' in options.env expressed
the wrong contract. It now checks case-insensitively for the key.

Refs #3411

* fix(#3617): execTool spawns the declared name unless mediation is required

The Windows full-test lane on #3621 failed tests/graphify.test.cjs — the python3
identity check asserted 'python3' and got the absolute resolved path
C:\hostedtoolcache\windows\Python\3.12.10\x64\python3.EXE instead.

Those tests are correct and the change was wrong. They pin a long-standing
contract — execTool spawns the program name it was given — by spying on
spawnSync's first argument, and routing every win32 call through the projected
invocation broke it.

Resolving a .exe buys nothing. libuv's CreateProcess path already performs
PATH + PATHEXT search, which is why spawning a bare 'node' has always worked on
Windows. The only case the OS genuinely cannot spawn is a .cmd/.bat. So execTool
now adopts the projection only when mediation actually happened —
windowsVerbatimArguments is exactly that flag — and otherwise passes the declared
program and args through untouched.

40-design.md already rejected gratuitous change for this reason: symmetry is not
worth a behavior change to 53 files that fixes nothing. That reasoning was
applied to POSIX and missed the win32 non-batch case. Rows 5 and 20 now record
it, and the CONTEXT.md glossary states the caller-choice rule.

deps.spawn deliberately still adopts the resolved path: its hasBinary probe
answers from the same resolver, so probe and spawn must agree on the exact file
(#3445). The asymmetry is now documented at both call sites rather than latent.

E7 pins the restored contract and was verified by executing execTool against a
monkeypatched spawnSync: python3 in, python3 spawned.

Refs #3411

---------

Co-authored-by: sim <sim@local>
This commit is contained in:
Tom Boucher
2026-08-18 14:12:44 -04:00
committed by GitHub
parent bf2332e67c
commit bf87dd4156
6 changed files with 992 additions and 32 deletions

View File

@@ -643,15 +643,254 @@ export function execNpm(args: string[], opts: { cwd?: string; timeout?: number }
return _spawnResult(result, 'npm');
}
/**
* Default PATHEXT when Windows does not supply one. Matches the value
* `gsd-tools.cjs`'s `resolveSpawnBinary` shipped in #3275; kept identical so the
* delegation is a behavior-preserving move rather than a redefinition.
*/
const DEFAULT_PATHEXT = '.EXE;.CMD;.BAT;.COM';
/** Windows extensions that must be mediated through cmd.exe rather than spawned. */
const CMD_MEDIATED_EXT = /\.(cmd|bat)$/i;
function _isFile(candidate: string): boolean {
try {
return fs.statSync(candidate).isFile();
} catch {
// Missing, unreadable (EACCES), or a broken link — all mean "not this one".
return false;
}
}
/**
* Read an environment variable by name, case-insensitively.
*
* Windows environment variable names are case-insensitive and conventionally
* cased `Path` / `ComSpec`, and `process.env` is a case-insensitive proxy that
* hides the difference. Spreading it (`{ ...process.env, ...opts.env }`, which
* `execTool` does whenever a caller supplies `opts.env`) produces a PLAIN object
* that keeps the OS's actual casing and loses the proxy — so an exact-case
* `env['PATH']` lookup returns undefined there and the PATH scan silently sees
* nothing. Caught by the Windows CI lane on #3617; the #3445 tests never hit it
* because they pass uppercase keys explicitly.
*
* Exact match wins when present, so a caller who sets the canonical name pays
* no scan.
*/
function _envGet(env: NodeJS.ProcessEnv, name: string): string | undefined {
const exact = env[name];
if (exact !== undefined) return exact;
const lower = name.toLowerCase();
for (const key of Object.keys(env)) {
if (key.toLowerCase() === lower) return env[key];
}
return undefined;
}
/** cmd.exe's own quoting rule inside a `/c` string: a literal quote is doubled. */
function _cmdQuoteToken(token: string): string {
return `"${token.replace(/"/g, '""')}"`;
}
/**
* Build a single verbatim command-line string for `cmd.exe /c` where every
* token is force-quoted, then wrap the whole thing in one more outer pair.
* cmd.exe strips exactly one outer quote pair when the string begins with a
* quote and contains at least two — so the outer wrap disappears and what's
* left is a sequence of individually-quoted tokens. Force-quoting is the
* point: an unquoted `a&b` is split by cmd's own metacharacter parsing, but a
* quoted `"a&b"` is one literal argument.
*/
function _buildVerbatimCmdLine(target: string, args: string[]): string {
return `"${[target, ...args].map(_cmdQuoteToken).join(' ')}"`;
}
/**
* #3411: resolve a DECLARED command name to the file a spawn can actually start.
* The single canonical answer to "where is this binary?" for the whole tree.
*
* This is the seam `CONTEXT.md` declares as "All OS-facing I/O; single platform
* seam". Four divergent implementations of this logic existed (#3411): `execNpm`'s
* `shell:true`, `execTool`'s absence of any handling, `gsd-tools.cjs`'s private
* scan, and `fallow-runner.cts`'s own candidate array. #3275 folded two of them
* together in `bin/`; this lifts that resolver into the seam so `bin/` delegates
* instead of owning a copy.
*
* **win32** tries PATHEXT entries ONLY — never the bare name. npm global installs
* drop an EXTENSIONLESS POSIX sh shim (`...\npm\codex`) next to `codex.CMD`; a
* bare-name-first scan resolves to it, the cmd.exe mediation gate sees no `.cmd`,
* and the ENOENT returns unchanged (field-reported on Windows 11 — see #3275).
* A name that ALREADY carries a PATHEXT-listed extension is tried as-is first, so
* `foo.exe` resolves to `foo.exe` rather than being probed as `foo.exe.EXE`; a
* suffix that is not in PATHEXT (`foo.txt`) is not an extension and only feeds the
* append loop.
*
* **POSIX** answers EXISTENCE by scanning PATH for the bare name. `execTool` does
* NOT consult this on POSIX — the bare name goes to spawnSync unchanged and Node's
* own PATH search does the work, so macOS/Linux behavior is untouched (#3275
* acceptance contract).
*
* Path-like names (any `/` or `\`) bypass the PATH scan: the name is already an
* address, so it passes through when it names an existing file.
*
* @returns the resolved path, or `null` when nothing matched. Callers fall back to
* the declared name on `null` so a genuine ENOENT still surfaces (#3086).
*/
export function resolveExecutableBinary(
name: string | null | undefined,
opts: { platform?: string; env?: NodeJS.ProcessEnv } = {},
): string | null {
if (!name) return null;
if (name.includes('/') || name.includes('\\')) {
return _isFile(name) ? name : null;
}
const env = opts.env ?? process.env;
const platform = opts.platform ?? process.platform;
const segments = String(_envGet(env, 'PATH') || '').split(path.delimiter).filter(Boolean);
if (platform !== 'win32') {
for (const dir of segments) {
const candidate = path.join(dir, name);
if (_isFile(candidate)) return candidate;
}
return null;
}
const exts = String(_envGet(env, 'PATHEXT') || DEFAULT_PATHEXT).split(';').filter(Boolean);
// A name already ending in a PATHEXT-listed extension is an address, not a stem:
// probing `foo.exe` as `foo.exe.EXE` would miss the file sitting right there.
// Compared case-insensitively because PATHEXT casing is not guaranteed.
const lower = name.toLowerCase();
const carriesKnownExt = exts.some((ext) => lower.endsWith(ext.toLowerCase()));
for (const dir of segments) {
if (carriesKnownExt) {
const asIs = path.join(dir, name);
if (_isFile(asIs)) return asIs;
}
for (const ext of exts) {
const candidate = path.join(dir, name + ext);
if (_isFile(candidate)) return candidate;
}
}
return null;
}
/**
* #3411: project a declared `(command, args)` into the pair `spawnSync` can
* actually execute on this platform.
*
* Resolution alone does not make Windows work: `CreateProcess` cannot execute a
* `.cmd`/`.bat` at all, so the cmd.exe mediation is inseparable from the lookup.
* Exporting only the resolver would leave every caller to re-derive that half —
* which is precisely how #3411's four copies accumulated.
*
* cmd.exe is invoked as an ordinary program with an EXPLICIT argv array, never via
* `shell: true`. `shell:true` on Windows is the mechanism behind CVE-2024-27980
* (argument injection through `.bat`/`.cmd`), and Node 26 deprecates it with an
* args array (DEP0190) because arguments are concatenated rather than escaped.
*
* Mediation fires when the target — the resolved path, or the declared name when
* resolution found nothing — carries a `.cmd`/`.bat` extension. A BARE name that
* resolved to nothing is passed through verbatim so the spawn fails with ENOENT;
* mediating it would turn `{exitCode:127, 'foo: not found'}` into cmd.exe's exit
* 9009 and silently change the not-found contract callers depend on.
*
* POSIX is a strict no-op: the declared command is returned unchanged and the
* environment is never consulted.
*
* The mediated command line is built VERBATIM rather than left to libuv: libuv's
* `quote_cmd_arg` only force-quotes an argument that contains a space, tab, or
* quote — it does not know about cmd.exe metacharacters (`&`, `|`, `>`, `<`,
* `^`, ...) at all, so an argument like `a&calc` reaches cmd.exe unquoted and
* gets re-parsed as two commands (the CVE-2024-27980 argument-injection class).
* Node's own CVE-2024-27980 escaping does not help here because it only fires
* when the spawned FILE itself is a `.bat`/`.cmd` — in this seam the spawned
* file is `cmd.exe`, not the target. Building the line ourselves and passing
* `windowsVerbatimArguments: true` (the shape Rust's std uses for the sibling
* CVE-2024-24576) means every token is force-quoted inside one outer pair, so
* a metacharacter inside a quoted token can never split the command line.
*
* KNOWN LIMIT: `%VAR%` still expands inside a cmd `/c` string, and there is no
* escape for `%` outside a batch file — an argument containing `%FOO%` is
* substituted with the environment value regardless of quoting. That is an
* information-disclosure limit, not arbitrary execution, and it's the same
* limit Rust's std documents for its own `CommandExt::raw_arg` escape hatch.
*
* CALLER CHOICE: this function's return value carries two independently
* adoptable pieces of information, and a caller may take either, both, or
* neither. `windowsVerbatimArguments: true` marks the cases where mediation
* was REQUIRED — the caller MUST adopt `command`+`args` together, since a
* `.cmd`/`.bat` genuinely cannot be spawned any other way. A merely-resolved
* `.exe` path (no mediation flag set) is only an OFFER: a caller may decline
* it and keep spawning the declared name instead, to hold its own observable
* contract stable. `execTool` (this file, below) is exactly such a caller —
* it adopts the mediated pair when `windowsVerbatimArguments` is set, but
* otherwise passes the declared `program`/`args` through untouched.
*/
export function projectSpawnInvocation(
command: string,
args: string[] = [],
opts: { platform?: string; env?: NodeJS.ProcessEnv } = {},
): { command: string; args: string[]; resolved: string | null; windowsVerbatimArguments?: boolean } {
const platform = opts.platform ?? process.platform;
if (platform !== 'win32') return { command, args, resolved: null };
const env = opts.env ?? process.env;
const resolved = resolveExecutableBinary(command, { platform, env });
// Mediate against the resolved path when we have one, else against the declared
// name. An unresolved name is mediated ONLY when it already declares .cmd/.bat:
// PATH-only resolution misses a batch file sitting in the current directory,
// which `cmd.exe /c` still finds — the behavior gsd-tools.cjs shipped before
// this consolidation, preserved here rather than silently narrowed.
const target = resolved ?? command;
if (!CMD_MEDIATED_EXT.test(path.basename(target))) {
// A BARE name that resolved to nothing is passed through untouched so the
// spawn fails with ENOENT. Mediating it would turn {exitCode:127,
// '<name>: not found'} into cmd.exe's exit 9009 and silently change the
// not-found contract `_spawnResult` and its 53 dependent files rely on.
return resolved ? { command: resolved, args, resolved } : { command, args, resolved: null };
}
// A CR/LF cannot be represented inside a Windows command line at all — cmd.exe
// treats it as a line terminator, so mediating it would silently truncate the
// argument rather than pass it through. Fail visibly instead: fall back to the
// unmediated shape so the spawn either fails with ENOENT (bare unresolved name)
// or hands the raw string to CreateProcess, whichever the caller was already
// prepared to see for a non-.cmd/.bat target.
if (/[\r\n]/.test(target) || args.some((a) => /[\r\n]/.test(a))) {
return resolved ? { command: resolved, args, resolved } : { command, args, resolved: null };
}
return {
command: String(_envGet(env, 'ComSpec') || 'cmd.exe'),
args: ['/d', '/s', '/c', _buildVerbatimCmdLine(target, args)],
resolved,
windowsVerbatimArguments: true,
};
}
export function execTool(program: string, args: string[], opts: { cwd?: string; env?: Record<string, string>; timeout?: number } = {}): SpawnResultOutput {
const result = childProcess.spawnSync(program, args, {
// #3411: Windows cannot spawn a .cmd/.bat at all — CreateProcess refuses it —
// so those are mediated through cmd.exe. Everything else keeps the DECLARED
// program name: libuv's CreateProcess path already performs PATH + PATHEXT
// search, so resolving a .exe here would buy nothing and would change what
// this seam's 167 dependents observe being spawned. `tests/graphify.test.cjs`
// pins that contract by spying on spawnSync's first argument. POSIX never
// reaches the mediation branch at all.
const spawnEnv = opts.env ? { ...process.env, ...opts.env } : undefined;
const invocation = projectSpawnInvocation(program, args, { env: spawnEnv ?? process.env });
const mediated = invocation.windowsVerbatimArguments === true;
const result = childProcess.spawnSync(mediated ? invocation.command : program, mediated ? invocation.args : args, {
cwd: opts.cwd,
env: opts.env ? { ...process.env, ...opts.env } : undefined,
env: spawnEnv,
encoding: 'utf-8',
stdio: 'pipe',
timeout: opts.timeout ?? 30_000,
windowsHide: true,
...(mediated ? { windowsVerbatimArguments: true } : {}),
});
// Stamp the DECLARED name, never the resolved path: `_spawnResult` renders
// `${program}: not found`, and callers across 53 files match on the string they
// passed. Resolution must not leak an absolute path into that message.
return _spawnResult(result, program);
}