Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(git): hardened git allows only the exact options callers use; hardenedPathDirty pathspecs are literal (CSO G1, G2)
time-attack3d15732cb7c7
diff --git a/src/core/hardened-git.ts b/src/core/hardened-git.tsindex 2193eedb2b..917ef404aa 100644--- a/src/core/hardened-git.ts+++ b/src/core/hardened-git.ts@@ -32,18 +32,34 @@ export function hardenedGitEnvironment(): NodeJS.ProcessEnv {export type HardenedGitResult = { ok: true; stdout: Buffer } | { ok: false; reason: 'exit' | 'timeout' | 'too_large' | 'unavailable'; status?: number };-const CONVERTING_OPTIONS = new Set(['--filters', '--textconv', '--ext-diff', '-p', '--patch', '-u', '--stat', '--word-diff']);-const ALLOWED_SUBCOMMANDS = new Set(['rev-parse', 'ls-files', 'ls-tree', 'cat-file', 'hash-object', 'check-attr', 'diff-index']);+/**+ * The exact options each allowed plumbing subcommand may take (CSO G1): Git+ * accepts unambiguous abbreviations (`--textc` is `--textconv`), so a blocklist+ * of converting options can't hold; anything not listed here refuses.+ */+const ALLOWED_OPTIONS: ReadonlyMap<string, ReadonlySet<string>> = new Map([+ ['rev-parse', new Set(['--show-toplevel', '--absolute-git-dir', '--verify', '--quiet', '--show-object-format'])],+ ['ls-files', new Set(['-s', '--stage', '--eol', '-z', '--cached', '--others', '--exclude-standard'])],+ ['ls-tree', new Set(['-r', '-l', '-z', '--full-tree'])],+ ['cat-file', new Set<string>()],+ ['hash-object', new Set(['--no-filters'])],+ ['check-attr', new Set(['-z'])],+ ['diff-index', new Set(['--cached', '--quiet'])],+]);/*** Refuses (throws) any Git invocation that could run a repository-configured- * clean/process filter or textconv: only the allow-listed plumbing, with- * `hash-object` only under `--no-filters` and `diff-index` only `--cached`.+ * clean/process filter or textconv: only the allow-listed plumbing with its+ * exact options before `--`/`--end-of-options`, `hash-object` only under+ * `--no-filters` and `diff-index` only `--cached`.*/export function assertHardenedGitArgs(args: readonly string[]): void {const sub = args[0] ?? '';- const ok = ALLOWED_SUBCOMMANDS.has(sub) && !args.some(arg => CONVERTING_OPTIONS.has(arg) || arg.startsWith('--textconv') || arg.startsWith('--filters'))- && (sub !== 'hash-object' || args.includes('--no-filters')) && (sub !== 'diff-index' || args.includes('--cached'));+ const allowed = ALLOWED_OPTIONS.get(sub);+ const end = args.findIndex(arg => arg === '--' || arg === '--end-of-options');+ const options = args.slice(1, end < 0 ? undefined : end).filter(arg => arg.startsWith('-'));+ const ok = allowed !== undefined && options.every(arg => allowed.has(arg))+ && (sub !== 'hash-object' || options.includes('--no-filters')) && (sub !== 'diff-index' || options.includes('--cached'));if (!ok) throw new Error(`hardened git refuses \`git ${args.slice(0, 2).join(' ')}\`: only filter-free plumbing may run in an untrusted checkout (see src/core/hardened-git.ts)`);}@@ -74,11 +90,12 @@ const PATH_LIMITS = { timeoutMs: 10_000, maxBytes: 1024 * 1024 };* any Git failure.*/export function hardenedPathDirty(root: string, rel: string): boolean | null {- const indexed = hardenedGitSync(root, ['ls-files', '-s', '--eol', '-z', '--', rel], PATH_LIMITS);+ const pathspec = `:(literal)${rel}`;+ const indexed = hardenedGitSync(root, ['ls-files', '-s', '--eol', '-z', '--', pathspec], PATH_LIMITS);if (!indexed.ok) return null;const entry = indexed.stdout.toString('utf8').split('\0')[0];if (!entry) {- const untracked = hardenedGitSync(root, ['ls-files', '-z', '--others', '--exclude-standard', '--', rel], PATH_LIMITS);+ const untracked = hardenedGitSync(root, ['ls-files', '-z', '--others', '--exclude-standard', '--', pathspec], PATH_LIMITS);return untracked.ok ? untracked.stdout.length > 0 : null;}const [meta = '', eolInfo = ''] = entry.split('\t');@@ -89,7 +106,7 @@ export function hardenedPathDirty(root: string, rel: string): boolean | null {if (mode !== '100644' && mode !== '100755') return null;const head = hardenedGitSync(root, ['rev-parse', '--verify', '--quiet', 'HEAD^{commit}'], PATH_LIMITS);if (!head.ok) return head.reason === 'exit' && head.status === 1 ? true : null;- const staged = hardenedGitSync(root, ['diff-index', '--cached', '--quiet', 'HEAD', '--', rel], PATH_LIMITS);+ const staged = hardenedGitSync(root, ['diff-index', '--cached', '--quiet', 'HEAD', '--', pathspec], PATH_LIMITS);if (!staged.ok) return staged.reason === 'exit' && staged.status === 1 ? true : null;const hashed = hardenedGitSync(root, ['hash-object', '--no-filters', '--', rel], PATH_LIMITS);if (!hashed.ok) return null;diff --git a/test/hardened-git-filters.test.ts b/test/hardened-git-filters.test.tsindex e9c1b6304c..e4a7ba5dc8 100644--- a/test/hardened-git-filters.test.ts+++ b/test/hardened-git-filters.test.ts@@ -133,3 +133,41 @@ describe('W4.3: hardenedPathDirty matches git status without running it', () =>expect(hardenedPathDirty(join(plain, 'sub'), 'note.md')).toBeNull();});});++describe('wave 13 CSO G1/G2: exact options and literal pathspecs', () => {+ test('G1: abbreviated converting options and unknown options refuse; every current call shape passes', () => {+ for (const args of [['cat-file', '--textc', 'HEAD:note.md'], ['cat-file', '--filt', 'HEAD:note.md'], ['cat-file', '-p', 'HEAD:note.md'],+ ['ls-files', '--with-tree=HEAD'], ['rev-parse', '--git-path', 'hooks'], ['ls-tree', '--format=%(objectname)', 'HEAD'],+ ['hash-object', '--no-filters', '--path=x', '--', 'note.md'], ['diff-index', '--cached', '--ext', 'HEAD']]) {+ expect(() => assertHardenedGitArgs(args)).toThrow(/hardened git refuses/);+ }+ for (const args of [['rev-parse', '--show-toplevel'], ['rev-parse', '--absolute-git-dir'], ['rev-parse', '--verify', '--end-of-options', 'HEAD^{commit}'],+ ['rev-parse', '--show-object-format'], ['rev-parse', '--verify', '--quiet', 'HEAD^{commit}'], ['ls-tree', '-r', '-l', '-z', '--full-tree', 'abc'],+ ['ls-tree', '-r', '-l', '-z', '--full-tree', 'abc', '--', ':(literal)a.md'], ['cat-file', 'blob', 'abc'], ['ls-files', '--stage', '-z'],+ ['ls-files', '--others', '--exclude-standard', '-z'], ['ls-files', '-z', '--cached', '--stage'], ['ls-files', '-z', '--others', '--exclude-standard'],+ ['ls-files', '-s', '--eol', '-z', '--', ':(literal)a.md'], ['ls-files', '-z', '--others', '--exclude-standard', '--', ':(literal)a.md'],+ ['diff-index', '--cached', '--quiet', 'HEAD', '--', ':(literal)a.md'], ['hash-object', '--no-filters', '--', '-odd.md'],+ ['check-attr', '-z', 'filter', 'text', 'eol', 'working-tree-encoding', '--', 'a.md']]) {+ expect(() => assertHardenedGitArgs(args)).not.toThrow();+ }+ });++ test('G1: an abbreviated --textconv never reaches git, so a repository textconv cannot run', () => {+ const root = repo(r => writeFileSync(join(r, '.gitattributes'), 'note.md diff=evil\n'));+ const marker = join(root, '..', `${root.split('/').pop()}-textconv-ran`);+ git(root, 'config', 'diff.evil.textconv', `sh -c 'touch ${marker}; cat "$1"' -`);+ expect(() => hardenedGitSync(root, ['cat-file', '--textc', 'HEAD:note.md'], { timeoutMs: 5_000, maxBytes: 1024 })).toThrow(/hardened git refuses/);+ expect(existsSync(marker)).toBe(false);+ });++ test('G2: glob characters in a path are literal', () => {+ const root = repo(r => writeFileSync(join(r, '[ab].md'), 'bracket\n'));+ writeFileSync(join(root, 'a.md'), 'staged\n');+ git(root, 'add', 'a.md');+ expect(hardenedPathDirty(root, '[ab].md')).toBe(false);+ const star = repo(r => writeFileSync(join(r, '*.md'), 'star\n'));+ writeFileSync(join(star, 'other.md'), 'untracked\n');+ expect(hardenedPathDirty(star, '*.md')).toBe(false);+ expect(hardenedPathDirty(root, 'a.md')).toBe(true);+ });+});