From 37bc30f16486475a1277cc1f091722966c92bcac Mon Sep 17 00:00:00 2001 From: lhh010 Date: Sun, 18 Oct 2026 19:51:49 +0800 Subject: [PATCH] =?UTF-8?q?fix(git):=20harden=20runGit's=20output=20caps?= =?UTF-8?q?=20=E2=80=94=20latch,=20killTree,=20stderr=20budget?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit dev already caps stdout bytes (GIT_OUTPUT_MAX_BYTES) and NUL fields, but three gaps remain, all proven by the 2026-10-13 host crashes (twice: RangeError: Invalid string length at the stdout accumulator, a junction- forest workspace driving 'git status -uall' to gigabytes): 1. no latch after a byte-cap reject — later chunks kept ACCUMULATING on the retained strings. On Windows spawn('git') resolves to the cmd shim, whose grandchild survives child.kill() and keeps feeding the dead accumulator (measured: the orphan outlived kill() by 20+s). The fieldCapped guard covered only the row cap; the byte cap had none. 2. kill() reaches only the wrapper — the orphaned grandchild needs its pipes destroyed (EPIPE) to actually stop. killTree() destroys both streams and is now used by every kill path (timeout, byte cap, field cap). 3. stderr was uncapped — a command whose DIAGNOSTICS stream past the budget is as able to kill the host via string length as its answer; stderr now shares the same maxBytes budget. Worst case in a pathological workspace is now one failed SCM refresh, never a dead host. --- src/git.ts | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/git.ts b/src/git.ts index f0c96b26..140568a6 100644 --- a/src/git.ts +++ b/src/git.ts @@ -435,12 +435,29 @@ function spawnGit( let outBytes = 0 let outFields = 0 let stderr = '' + let errBytes = 0 // Set when the caller's row cap stopped production: the partial answer is // then the answer, and the killed child's non-zero exit must not be // reported as a git failure of its own. let fieldCapped = false - const timer = setTimeout(() => { + // Set when a byte cap refused the call: every later chunk is dropped, not + // accumulated. Without the latch the killed child's pipe remainder (on + // Windows the cmd shim's orphaned grandchild survives kill() and keeps + // streaming) keeps growing the retained strings — first-hand: a + // junction-forest workspace drove 'git status -uall' past the V8 string + // limit and crashed the whole host with RangeError after the cap had + // already rejected. + let byteCapped = false + // Kill the whole pipe, not just the wrapper: destroying the streams + // EPIPE-terminates the orphaned grandchild that plain kill() leaves + // feeding a dead accumulator on Windows. + const killTree = (): void => { + try { child.stdout.destroy() } catch { /* already closed */ } + try { child.stderr.destroy() } catch { /* already closed */ } child.kill('SIGKILL') + } + const timer = setTimeout(() => { + killTree() reject(new GitCommandError(`git ${args[0] ?? ''} timed out after ${timeoutMs}ms`, 'git-error', args.join(' '))) }, timeoutMs) child.stdout.on('data', (chunk: Buffer) => { @@ -450,11 +467,12 @@ function spawnGit( // pipe, so "capped" was only a claim about the kill and not a bound on // what the caller receives (CI: a 125 KB answer plus a fast child made // the capped call return all 2,500 rows). - if (fieldCapped) return + if (fieldCapped || byteCapped) return stdout += chunk.toString('utf8') outBytes += chunk.length if (outBytes > maxBytes) { - child.kill('SIGKILL') + byteCapped = true + killTree() reject(new GitCommandError( `git ${args[0] ?? ''} output exceeded ${maxBytes} bytes`, 'git-error', @@ -466,11 +484,27 @@ function spawnGit( outFields += countNulFields(chunk) if (outFields >= maxNulFields) { fieldCapped = true - child.kill('SIGKILL') + killTree() } } }) - child.stderr.on('data', (chunk: Buffer) => { stderr += chunk.toString('utf8') }) + child.stderr.on('data', (chunk: Buffer) => { + if (byteCapped) return + stderr += chunk.toString('utf8') + errBytes += chunk.length + // stderr shares the byte budget: a command whose DIAGNOSTICS stream + // past the cap is as able to kill the host via string length as one + // whose answer does. + if (errBytes > maxBytes) { + byteCapped = true + killTree() + reject(new GitCommandError( + `git ${args[0] ?? ''} stderr exceeded ${maxBytes} bytes`, + 'git-error', + args.join(' '), + )) + } + }) child.on('error', (error) => { clearTimeout(timer) reject(new GitCommandError(`cannot run git: ${error.message}`, 'git-error', args.join(' ')))