`gsd-sdk query check.ship-ready <phase>` built a git command as a shell
string with the current branch name interpolated. Git branch names can
legally contain shell metacharacters, so a repo checked out on a
malicious branch like `foo;touch${IFS}INJ;bar` executed arbitrary shell
commands.
Vulnerability site (pre-fix):
sdk/src/query/check-ship-ready.ts:50
runSyncSafe(`git config --get branch.${current_branch}.merge`, cwd)
→ execSync('git config --get branch.foo;touch${IFS}INJ;bar.merge')
→ /bin/sh -c parses three commands; the middle one runs `touch INJ`
in the project dir and creates the sentinel file.
Manually reproduced on git 2.53.0:
- refname `foo;touch${IFS}INJ;bar` is accepted by `git check-ref-format`
and by `git checkout -b`.
- `current_branch` returned from `git rev-parse --abbrev-ref HEAD`
contains the metacharacters verbatim.
- Interpolation into the buggy execSync call creates the sentinel.
Fix:
- Replace `runSyncSafe(cmd: string, cwd)` (execSync, shell-string) with
`runArgvSafe(file, args: readonly string[], cwd)` (execFileSync,
argv-based, no shell).
- Same shape for the boolean wrapper: `boolArgvSafe`.
- Convert all 7 subprocess sites in the module to argv form:
- `git status --porcelain`
- `git rev-parse --abbrev-ref HEAD`
- `git config --get branch.<name>.merge` ← the interpolation site
- `git rev-parse --verify main`
- `git remote`
- `gh --version`
- `which gh`
- Shell is never invoked. Branch names — even ones with `;`, `$IFS`,
backticks, `$()` — are passed as a single argv element and treated
as opaque data.
Regression test (`sdk/src/query/check-ship-ready.test.ts`):
- `#3587: branch name with shell-injection payload does not execute
injected command` — creates a real git repo, checks out the proven
exploit branch `foo;touch${IFS}INJECTED_BY_3587;bar`, runs
checkShipReady, and asserts the sentinel file does NOT exist. This
test FAILS on the unfixed code (verified pre-implementation) and
PASSES on the fixed code — true red→green TDD.
- `#3587: round-trips a metacharacter branch name verbatim in
current_branch` — positive proof the branch name survives argv as
data (would fail if a future change re-introduces shell quoting).
- `#3587: gh probe does not invoke a shell` — locks the gh path
against a future regression that might add an interpolation site.
Validation:
- SDK unit suite via vitest: 1,869/1,869 pass.
- Full root suite via gsd-test-both (per CLAUDE.md): 10,676/10,676
on Mac AND 10,676/10,676 on Linux Docker, zero cross-platform diff.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
130 lines
4.6 KiB
TypeScript
130 lines
4.6 KiB
TypeScript
/**
|
|
* Ship preflight checks (`check.ship-ready`).
|
|
*
|
|
* Consolidates git/gh checks from `ship.md` into a single structured query.
|
|
* All subprocess calls are wrapped in try/catch — never throws on git/gh failures.
|
|
* See `.planning/research/decision-routing-audit.md` §3.9.
|
|
*
|
|
* #3587: every subprocess call uses argv-based execFileSync — never a
|
|
* shell-string execSync. Git branch names are repository-controlled data
|
|
* and can legally contain metacharacters (`;`, `$`, backticks, etc.); a
|
|
* shell-string `git config --get branch.${current_branch}.merge` allowed
|
|
* arbitrary command injection from a malicious branch name. Passing args
|
|
* as argv elements means the shell is never invoked.
|
|
*/
|
|
|
|
import { execFileSync } from 'node:child_process';
|
|
import { GSDError, ErrorClassification } from '../errors.js';
|
|
import { normalizePhaseName } from './helpers.js';
|
|
import { checkVerificationStatus } from './check-verification-status.js';
|
|
import type { QueryHandler } from './utils.js';
|
|
|
|
/**
|
|
* Run a subprocess via argv (NEVER a shell string). Returns trimmed stdout
|
|
* on success or null on any failure (non-zero exit, missing binary, etc.).
|
|
* The pre-#3587 helper used `execSync(cmd, …)` which spawned `/bin/sh -c`
|
|
* and parsed `cmd` as shell syntax — that path is gone.
|
|
*/
|
|
function runArgvSafe(file: string, args: readonly string[], cwd: string): string | null {
|
|
try {
|
|
return execFileSync(file, args, {
|
|
cwd,
|
|
encoding: 'utf-8',
|
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
}).trim();
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function boolArgvSafe(file: string, args: readonly string[], cwd: string): boolean {
|
|
return runArgvSafe(file, args, cwd) !== null;
|
|
}
|
|
|
|
export const checkShipReady: QueryHandler = async (args, projectDir) => {
|
|
const raw = args[0];
|
|
if (!raw) {
|
|
throw new GSDError('phase number required for check ship-ready', ErrorClassification.Validation);
|
|
}
|
|
|
|
normalizePhaseName(raw); // validate format
|
|
|
|
const blockers: string[] = [];
|
|
|
|
// git checks — all wrapped in try/catch via helpers, all argv-based.
|
|
const porcelain = runArgvSafe('git', ['status', '--porcelain'], projectDir);
|
|
const clean_tree = porcelain !== null && porcelain === '';
|
|
|
|
const current_branch = runArgvSafe('git', ['rev-parse', '--abbrev-ref', 'HEAD'], projectDir);
|
|
const on_feature_branch =
|
|
current_branch !== null &&
|
|
current_branch !== 'main' &&
|
|
current_branch !== 'master';
|
|
|
|
// Determine base branch
|
|
let base_branch: string | null = null;
|
|
if (current_branch) {
|
|
// #3587: branch name passed as a single argv element — git treats it
|
|
// as data, the shell is never invoked, no interpolation possible.
|
|
const mergeRef = runArgvSafe(
|
|
'git',
|
|
['config', '--get', `branch.${current_branch}.merge`],
|
|
projectDir,
|
|
);
|
|
if (mergeRef) {
|
|
base_branch = mergeRef.replace('refs/heads/', '');
|
|
} else {
|
|
// Fallback: check if 'main' branch exists, else 'master'
|
|
const mainExists = boolArgvSafe('git', ['rev-parse', '--verify', 'main'], projectDir);
|
|
base_branch = mainExists ? 'main' : 'master';
|
|
}
|
|
}
|
|
|
|
const remoteOut = runArgvSafe('git', ['remote'], projectDir);
|
|
const remote_configured = remoteOut !== null && remoteOut.trim().length > 0;
|
|
|
|
// gh availability — argv as well so a future change that interpolates
|
|
// a user-controlled value into the probe cannot silently introduce a
|
|
// new injection seam.
|
|
const gh_available =
|
|
boolArgvSafe('gh', ['--version'], projectDir) ||
|
|
boolArgvSafe('which', ['gh'], projectDir);
|
|
|
|
// gh_authenticated: advisory — skip actual auth check to avoid slow network call
|
|
const gh_authenticated = false;
|
|
|
|
// Verification status
|
|
let verification_passed = false;
|
|
try {
|
|
const verRes = await checkVerificationStatus([raw], projectDir);
|
|
const vdata = verRes.data as Record<string, unknown>;
|
|
const status = String(vdata.status ?? '').toLowerCase();
|
|
verification_passed = status === 'pass' || status === 'passed';
|
|
} catch {
|
|
verification_passed = false;
|
|
}
|
|
|
|
// Collect blockers
|
|
if (!verification_passed) blockers.push('verification status is not passed');
|
|
if (!clean_tree) blockers.push('working tree is not clean (uncommitted changes)');
|
|
if (!on_feature_branch) blockers.push('not on a feature branch (currently on main/master or unknown)');
|
|
if (!remote_configured) blockers.push('no git remote configured');
|
|
|
|
const ready = verification_passed && clean_tree && on_feature_branch && remote_configured;
|
|
|
|
return {
|
|
data: {
|
|
ready,
|
|
verification_passed,
|
|
clean_tree,
|
|
on_feature_branch,
|
|
current_branch,
|
|
base_branch,
|
|
remote_configured,
|
|
gh_available,
|
|
gh_authenticated,
|
|
blockers,
|
|
},
|
|
};
|
|
};
|