gstack's script runner and merge-queue checks now refuse cut-off command output (GST-3104) (#4502)

The helper that runs gstack's own scripts and `bash -c` commands, and
the merge queue's branch-ownership, stacked-test, release-version and
merge-group record checks, now fail with `SPAWN_OUTPUT_INCOMPLETE` when
Bun cuts a command's output off, instead of treating part of it as the
whole answer.

Why: #4492 added this for the scanners and gates but left these on
Node's `spawnSync`, and its release note claimed wider coverage than it
shipped.

Nothing to do: a new test stops any new gstack command from calling
Node's `spawnSync` directly.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
Garry Tanandcapy-ai[bot] authored and GitHub committed 2026-11-18 18:40:12 +00:00
1 parent d789dd965b
commit 2f57b07495
14 files changed
+237 -11

No files matched your search

+124 -3
View File
@@ -12,8 +12,20 @@ import * as os from 'node:os';
import * as path from 'node:path';
import { RESULT_CODES } from '../lib/result-codes';
import { spawnSync } from '../lib/spawn-sync';
import { listRepoFiles } from '../lib/pregate/import-graph';
import { gitEnv } from './helpers/git-env';
/**
* The git binary itself, from `git --exec-path`, so a stub named git cannot be reached again through
* a PATH-resolving git wrapper (Bun.which('git') can return the wrapper). Falls back to Bun.which.
*/
function realGit(env: NodeJS.ProcessEnv = process.env): string {
const r = rawSpawnSync('git', ['--exec-path'], { encoding: 'utf8', timeout: 10_000, env });
const bin = path.join((r.stdout ?? '').trim(), process.platform === 'win32' ? 'git.exe' : 'git');
if (r.status === 0 && fs.existsSync(bin)) return bin;
return Bun.which('git', { PATH: env.PATH }) || '/usr/bin/git';
}
const OVERFLOW = "process.on('SIGTERM', () => {}); process.stdout.write('x'.repeat(200000)); setTimeout(() => {}, 300);";
const overflowArgs = ['-e', OVERFLOW];
@@ -82,17 +94,17 @@ describe('the pre-push credential scan with a cut-off diff', () => {
const head = git(['rev-parse', 'HEAD']);
// The diff call prints about 90 MB of context lines first, so the real diff (and its key) lands past the
// hook's 64 MiB maxBuffer; it ignores SIGTERM and exits 0, which is what Bun then reports as the status.
const realGit = Bun.which('git') || '/usr/bin/git';
const gitBin = realGit();
const stub = [
'#!/bin/sh',
'if [ "$1" = "diff" ]; then',
" trap '' TERM",
" printf 'diff --git a/pad.txt b/pad.txt\\n--- a/pad.txt\\n+++ b/pad.txt\\n@@ -1,2000000 +1,2000000 @@\\n'",
" yes ' padding line that only fills the read buffer of the scanner' | head -n 2000000",
` "${realGit}" "$@"`,
` "${gitBin}" "$@"`,
' exit 0',
'fi',
`exec "${realGit}" "$@"`,
`exec "${gitBin}" "$@"`,
'',
].join('\n');
fs.writeFileSync(path.join(stubDir, 'git'), stub, { mode: 0o755 });
@@ -112,3 +124,112 @@ describe('the pre-push credential scan with a cut-off diff', () => {
}
}, 90_000);
});
describe('the stub git finds the real git binary', () => {
// POSIX only: the wrapper is a #!/bin/sh file named git.
test.skipIf(process.platform === 'win32')('a PATH-resolving git wrapper first on PATH does not become the stub\'s real git', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'spawn-sync-wrapper-'));
try {
fs.writeFileSync(path.join(dir, 'git'), `#!/bin/sh\nPATH=${process.env.PATH}\nexec git "$@"\n`, { mode: 0o755 });
const PATH = `${dir}${path.delimiter}${process.env.PATH ?? ''}`;
expect(Bun.which('git', { PATH })).toBe(path.join(dir, 'git'));
const git = realGit({ ...process.env, PATH });
expect(path.dirname(git)).not.toBe(dir);
expect(fs.readFileSync(git).subarray(0, 2).toString()).not.toBe('#!');
expect(rawSpawnSync(git, ['--version'], { encoding: 'utf8', timeout: 10_000 }).stdout).toStartWith('git version');
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
});
describe('listRepoFiles (lib/pregate/import-graph.ts) refuses an unfinished git ls-files', () => {
test('a git that cannot start throws instead of returning no files', () => {
expect(() => listRepoFiles(path.join(os.tmpdir(), 'spawn-sync-no-such-repo', String(process.pid)))).toThrow('git ls-files did not finish');
});
// POSIX only: the stub is a #!/bin/sh file named git, which Windows does not run.
test.skipIf(process.platform === 'win32')('a file list cut off past 64 MiB that exits 0 throws SPAWN_OUTPUT_INCOMPLETE', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'spawn-sync-ls-files-'));
const pathKey = Object.keys(process.env).find(k => k.toLowerCase() === 'path') || 'PATH';
const saved = process.env[pathKey];
try {
fs.writeFileSync(path.join(dir, 'git'), "#!/bin/sh\ntrap '' TERM\nyes 'lib/a-file-name-that-fills-the-list.ts' | head -n 2000000\nexit 0\n", { mode: 0o755 });
process.env[pathKey] = `${dir}${path.delimiter}${saved ?? ''}`;
expect(() => listRepoFiles(dir)).toThrow('SPAWN_OUTPUT_INCOMPLETE');
} finally {
process.env[pathKey] = saved;
fs.rmSync(dir, { recursive: true, force: true });
}
}, 60_000);
});
/**
* GST-3104: product code takes spawnSync from lib/spawn-sync.ts. The files that still import it from
* node:child_process are listed in test/fixtures/spawn-sync-raw-importers.json, and that list may only
* shrink. Only code files and bin/ scripts are listed and read.
*/
const ROOT = path.resolve(import.meta.dir, '..');
const BASELINE_PATH = 'test/fixtures/spawn-sync-raw-importers.json';
const OWNER = 'lib/spawn-sync.ts';
const CODE = /\.(?:[cm]?[jt]s|tsx)$/;
const BIN = /(?:^|\/)bin\/[^/.]+$/;
const CODE_PATHSPECS = ['*.ts', '*.tsx', '*.mts', '*.cts', '*.js', '*.mjs', '*.cjs', 'bin/*', '*/bin/*'];
const isTest = (f: string) => /(^|\/)(test|tests|__tests__|fixtures)\//.test(f) || /\.test\.[cm]?[jt]sx?$/.test(f) || f === 'test-setup.ts';
const CHILD_PROCESS = String.raw`['"](?:node:)?child_process['"]`;
/** Whether this source takes spawnSync from node:child_process (named, aliased, namespace, default or require). */
export function importsRawSpawnSync(text: string): boolean {
for (const m of text.matchAll(new RegExp(String.raw`import\s*\{([^}]*)\}\s*from\s*${CHILD_PROCESS}`, 'g'))) {
if (/(?:^|[,{\s])spawnSync\b/.test(m[1].replace(/\btype\s+spawnSync\b/g, ''))) return true;
}
for (const m of text.matchAll(new RegExp(String.raw`import\s+(?:\*\s+as\s+)?(\w+)\s+from\s*${CHILD_PROCESS}`, 'g'))) {
if (new RegExp(String.raw`\b${m[1]}\.spawnSync\b`).test(text)) return true;
}
return new RegExp(String.raw`require\(\s*${CHILD_PROCESS}\s*\)`).test(text) && /\bspawnSync\b/.test(text);
}
function productSources(): Map<string, string> {
const r = rawSpawnSync('git', ['ls-files', '-z', '--', ...CODE_PATHSPECS], { cwd: ROOT, encoding: 'utf8', timeout: 60_000, maxBuffer: 256 * 1024 * 1024 });
if (r.status !== 0 || r.error) throw new Error(`git ls-files failed: ${r.error?.message ?? r.stderr}`);
const out = new Map<string, string>();
for (const f of r.stdout.split('\0').filter(Boolean)) {
if (isTest(f) || f === OWNER) continue;
const bin = BIN.test(f);
if (!CODE.test(f) && !bin) continue;
let text: string;
try { text = fs.readFileSync(path.join(ROOT, f), 'utf8'); } catch { continue; }
if (bin && !/^#!.*\b(?:bun|node|tsx)\b/.test(text)) continue;
out.set(f, text);
}
return out;
}
describe('raw spawnSync import ratchet', () => {
const baseline = (JSON.parse(fs.readFileSync(path.join(ROOT, BASELINE_PATH), 'utf8')) as { files: string[] }).files;
const raw = [...productSources()].filter(([, text]) => importsRawSpawnSync(text)).map(([f]) => f).sort();
test('no product file outside the baseline imports spawnSync from node:child_process', () => {
const added = raw.filter(f => !baseline.includes(f));
expect(added, `import spawnSync from ${OWNER} instead (it fails closed on cut-off output, GST-3086)`).toEqual([]);
});
test('the baseline only shrinks: a file that no longer imports it raw leaves the list', () => {
const stale = baseline.filter(f => !raw.includes(f));
expect(stale, `remove these from ${BASELINE_PATH}`).toEqual([]);
expect([...baseline].sort(), `keep ${BASELINE_PATH} sorted`).toEqual(baseline);
});
test('the scanner sees every import form and ignores type-only and other imports', () => {
expect(importsRawSpawnSync("import { spawnSync } from 'node:child_process';")).toBe(true);
expect(importsRawSpawnSync('import { execSync, spawnSync as run } from "child_process";')).toBe(true);
expect(importsRawSpawnSync("import {\n spawn,\n spawnSync,\n} from 'node:child_process';")).toBe(true);
expect(importsRawSpawnSync("import * as cp from 'node:child_process';\ncp.spawnSync('git', [], { timeout: 1000 });")).toBe(true);
expect(importsRawSpawnSync("const { spawnSync } = require('child_process');")).toBe(true);
expect(importsRawSpawnSync("import type { SpawnSyncReturns } from 'node:child_process';")).toBe(false);
expect(importsRawSpawnSync("import { spawn, type spawnSync } from 'node:child_process';")).toBe(false);
expect(importsRawSpawnSync("import * as cp from 'node:child_process';\ncp.spawn('git', []);")).toBe(false);
expect(importsRawSpawnSync("import { spawnSync } from './spawn-sync';")).toBe(false);
});
});