Skip to content

Commit 1a5cd25

Browse files
bradygasterCopilot
andcommitted
fix(gh-aw): bound staged install queries to required paths
Independently revise the rejected ownership staging handoff. Avoid recursive consumer-tree enumeration and preserve exact staged type and digest checks. Cover 20,000 unrelated tracked files, unchanged staging, bounded blob reads, index locks, and substituted directories. Closes #2113 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 25b113d commit 1a5cd25

5 files changed

Lines changed: 75 additions & 8 deletions

File tree

‎test/gh-aw-install-staging.test.ts‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterEach, describe, expect, it } from 'vitest';
1+
import { afterEach, describe, expect, it, vi } from 'vitest';
22
import { execFileSync, spawnSync } from 'node:child_process';
33
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
44
import { tmpdir } from 'node:os';
@@ -12,6 +12,11 @@ import {
1212
} from '../workflows/shared/squad-install-verifier.mjs';
1313
import { createFirstInstallFixture } from './helpers/gh-aw-install-fixture.js';
1414

15+
vi.mock('node:child_process', async (importOriginal) => {
16+
const actual = await importOriginal<typeof import('node:child_process')>();
17+
return { ...actual, spawnSync: vi.fn(actual.spawnSync) };
18+
});
19+
1520
const revision = 'a'.repeat(40);
1621
const roots: string[] = [];
1722
const verifier = resolve('workflows/shared/squad-install-verifier.mjs');
@@ -49,10 +54,52 @@ function stagedPaths(root: string): string[] {
4954
}
5055

5156
afterEach(() => {
57+
vi.clearAllMocks();
5258
for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });
5359
});
5460

5561
describe('gh-aw: verified ownership staging under consumer ignore rules', () => {
62+
it('bounds staged reads to required paths in a large unrelated consumer tree', () => {
63+
const root = consumer();
64+
const unrelated = 'consumer-data';
65+
for (let index = 0; index < 20_000; index += 1) {
66+
write(root, `${unrelated}/ordinary-file-${index.toString().padStart(5, '0')}.txt`, 'consumer content\n');
67+
}
68+
git(root, 'add', '--', unrelated);
69+
git(root, '-c', 'user.name=Test', '-c', 'user.email=test@example.invalid',
70+
'-c', 'core.hooksPath=/dev/null', 'commit', '--quiet', '-m', 'large consumer baseline');
71+
write(root, `${unrelated}/ordinary-file-00000.txt`, 'unstaged consumer edit\n');
72+
write(root, `${unrelated}/untracked.txt`, 'untracked consumer content\n');
73+
const before = stagedPaths(root);
74+
const tree = git(root, 'write-tree').trim();
75+
const unbounded = spawnSync('git', ['ls-tree', '-r', '-z', tree], { cwd: root });
76+
expect(unbounded.error).toMatchObject({ code: 'ENOBUFS' });
77+
const manifest = JSON.parse(readFileSync(join(root, CONTRACT_DESTINATION), 'utf8'));
78+
const required = [
79+
CONTRACT_DESTINATION,
80+
OWNERSHIP_DESTINATION,
81+
...manifest.workflows.flatMap((entry: { destination: string; lock: string }) =>
82+
[entry.destination, entry.lock]),
83+
...manifest.shared_runtime.flatMap((entry: { package_destination: string; destination: string }) =>
84+
[entry.package_destination, entry.destination]),
85+
...manifest.skills.map((entry: { destination: string }) => entry.destination),
86+
];
87+
const spawn = vi.mocked(spawnSync);
88+
spawn.mockClear();
89+
expect(verifyStagedInstall(root, { expectedRevision: revision, stageOwnership: true }).failures).toEqual([]);
90+
const queries = spawn.mock.calls.filter(([command, args]) => command === 'git' && args?.[0] === 'ls-tree');
91+
expect(queries).toHaveLength(1);
92+
expect(queries[0][1]).toEqual(['ls-tree', '-z', expect.any(String), '--', ...new Set(required)]);
93+
const unrelatedObject = git(root, 'rev-parse', `HEAD:${unrelated}/ordinary-file-00000.txt`).trim();
94+
const blobReads = spawn.mock.calls.filter(([command, args]) => command === 'git' && args?.[0] === 'cat-file');
95+
expect(blobReads).toHaveLength(new Set(required).size);
96+
expect(blobReads.some(([, args]) => args?.includes(unrelatedObject))).toBe(false);
97+
expect(stagedPaths(root)).toEqual([...before, OWNERSHIP_DESTINATION].sort());
98+
expect(git(root, 'diff', '--cached', '--name-only').trim()).toBe(OWNERSHIP_DESTINATION);
99+
expect(git(root, 'diff', '--name-only').trim()).toBe(`${unrelated}/ordinary-file-00000.txt`);
100+
expect(readFileSync(join(root, unrelated, 'untracked.txt'), 'utf8')).toBe('untracked consumer content\n');
101+
}, 30_000);
102+
56103
it('reproduces the silent omission, then force-stages only the exact required JSON', () => {
57104
const root = consumer();
58105
expect(verifyInstall(root, { expectedRevision: revision }).failures).toEqual([]);
@@ -97,6 +144,13 @@ describe('gh-aw: verified ownership staging under consumer ignore rules', () =>
97144
expect(stagedPaths(root)).not.toContain(OWNERSHIP_DESTINATION);
98145
});
99146

147+
it('fails closed when the verified index cannot be snapshotted', () => {
148+
const root = consumer();
149+
expect(verifyStagedInstall(root, { stageOwnership: true }).failures).toEqual([]);
150+
write(root, '.git/index.lock', 'locked\n');
151+
expect(verifyStagedInstall(root).failures.join('\n')).toMatch(/git failed:[\s\S]*index\.lock/);
152+
});
153+
100154
it.each([
101155
CONTRACT_DESTINATION,
102156
'.github/workflows/squad.md',
@@ -136,4 +190,16 @@ describe('gh-aw: verified ownership staging under consumer ignore rules', () =>
136190
expect(verifyStagedInstall(root).failures.join('\n'))
137191
.toContain(`Required staged path is not a regular file: ${path}`);
138192
});
193+
194+
it('rejects a required path replaced by an index directory without reading its descendants', () => {
195+
const root = consumer();
196+
expect(verifyStagedInstall(root, { stageOwnership: true }).failures).toEqual([]);
197+
const path = '.github/workflows/squad.md';
198+
const object = git(root, 'rev-parse', `:${path}`).trim();
199+
git(root, 'update-index', '--force-remove', '--', path);
200+
git(root, 'update-index', '--add', '--cacheinfo', `100644,${object},${path}/unexpected.txt`);
201+
expect(verifyInstall(root).failures).toEqual([]);
202+
expect(verifyStagedInstall(root).failures.join('\n'))
203+
.toContain(`Required staged path is not a regular file: ${path}`);
204+
});
139205
});

‎workflows/package/squad-bootstrap.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pre-agent-steps:
151151
node --check "$path" >/dev/null
152152
}
153153
# BEGIN GENERATED RESOURCE DIGESTS
154-
check_hash "$install_verifier" "f7c0d8e750068a030450de954b457caa9819b8940a28813a9154458e5f7e2205"
154+
check_hash "$install_verifier" "40753edd5995c5f7ba058f57fbcc5aff0ccf3341c721b764fa0cf1dc70eba3f1"
155155
check_hash "$cast_validator" "0988e04aeef316f4d7a0107c902bbbcf6538b8899b9fffba5f62150717967685"
156156
check_hash "$bootstrap_validator" "d449b9204f7fad133ff7133c1a30c9381c87e3c0c9d481352819ca93ea1a1dad"
157157
# END GENERATED RESOURCE DIGESTS

‎workflows/shared/squad-install-verifier.mjs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,8 @@ export function verifyStagedInstall(root, { expectedRevision = '', stageOwnershi
901901
}
902902
// Snapshot the index, not HEAD or the working tree, including unchanged tracked files.
903903
const tree = spawnChecked('git', ['write-tree'], root).stdout.trim();
904-
const entries = spawnChecked('git', ['ls-tree', '-r', '-z', tree], root).stdout
904+
// Exact allowlisted paths bound output; no recursion into unrelated or substituted trees.
905+
const entries = spawnChecked('git', ['ls-tree', '-z', tree, '--', ...required], root).stdout
905906
.split('\0').filter(Boolean);
906907
const staged = new Map(entries.map(entry => {
907908
const tab = entry.indexOf('\t');

‎workflows/squad-bootstrap.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pre-agent-steps:
158158
node --check "$path" >/dev/null
159159
}
160160
# BEGIN GENERATED RESOURCE DIGESTS
161-
check_hash "$install_verifier" "f7c0d8e750068a030450de954b457caa9819b8940a28813a9154458e5f7e2205"
161+
check_hash "$install_verifier" "40753edd5995c5f7ba058f57fbcc5aff0ccf3341c721b764fa0cf1dc70eba3f1"
162162
check_hash "$cast_validator" "0988e04aeef316f4d7a0107c902bbbcf6538b8899b9fffba5f62150717967685"
163163
check_hash "$bootstrap_validator" "d449b9204f7fad133ff7133c1a30c9381c87e3c0c9d481352819ca93ea1a1dad"
164164
# END GENERATED RESOURCE DIGESTS

‎workflows/squad-workflows.manifest.json‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@
6868
"source": "workflows/package/squad-bootstrap.md",
6969
"destination": ".github/workflows/squad-bootstrap.md",
7070
"lock": ".github/workflows/squad-bootstrap.lock.yml",
71-
"source_sha256": "8eeb1f9f6f9da4a84b137b71e7345cc091ee924d63c2f2f5cbfe052b17813c48",
72-
"lock_sha256": "e6f5df8f00269ff6d6dc794b6b8550bd10e896cd16dfbcdca9cef8a30ba6d782",
73-
"package_lock_sha256": "c5312c6c30fa41934a207ab519ddbfb6e26db5ec669a04bdb7e8874715267a9f"
71+
"source_sha256": "e4950f83da8b0e93141a1ad606138c24a69b34df3fed2f5bf2d2fea8ebe68722",
72+
"lock_sha256": "cb43077d52699f30f7ac5da6cbb5933195c7acf94c96e04f45386d00a1376266",
73+
"package_lock_sha256": "d35fcad18bd24258b4c1cf948f10ec3e8fc019e5b1ce5e6f92a854192653ecc9"
7474
},
7575
{
7676
"name": "squad-command-router",
@@ -97,7 +97,7 @@
9797
"package_destination": ".github/workflows/shared/squad-install-verifier.mjs",
9898
"destination": ".github/workflows/shared/squad-install-verifier.mjs",
9999
"ownership": "manifest",
100-
"sha256": "f7c0d8e750068a030450de954b457caa9819b8940a28813a9154458e5f7e2205"
100+
"sha256": "40753edd5995c5f7ba058f57fbcc5aff0ccf3341c721b764fa0cf1dc70eba3f1"
101101
},
102102
{
103103
"path": "shared/squad-command-contract.mjs",

0 commit comments

Comments
 (0)