Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.
JavaScript agentic-codingai-agentsai-governanceautonomous-agentsclaude-codeclicode-reviewcodexcoding-agentsdeveloper-toolsgit-hooksllmlocal-firstmulti-agentsoftware-factoryspec-driven-developmentverificationvibe-coding
fix(spec): bind initial signer hashes in receipts [S17,S19,S20]
Corey Olson06873fc4e468
diff --git a/docs/api.md b/docs/api.mdindex 425d5c15..26ae8dbc 100644--- a/docs/api.md+++ b/docs/api.md@@ -142,3 +142,5 @@ HTTP refusals use the same versioned error envelope above: 400 for malformed cal`pullboard spec signers add [--key <path>] [--by <principal>]` opts a repo into OpenSSH-signed spec sign-offs. The key defaults to Git `user.signingkey`, then `~/.ssh/id_ed25519.pub`. The principal defaults to the exact Git `user.email`; `--by` overrides it. `spec signoff` uses the same email default in a repo with SSH signers, and accepts `--by` when a listed principal differs. A row's `signers:` names those exact principals. Spec checking refuses a required principal without a corresponding signer entry.The initial command output names `.pullboard/signers`, `.pullboard/first-commit` and `.pullboard/signers.initial` for staging and committing. Later sign-offs and signed signer-list changes are recorded in `.pullboard/signoffs.jsonl`; commit that file with the corresponding signer-list change.++Every signed row and signer-list change binds the initial signer-list hash in its canonical text. Rewriting `.pullboard/signers.initial`, or relabelling that hash in earlier receipts, invalidates those receipts.diff --git a/src/signature.js b/src/signature.jsindex 6c3f818d..48fe6718 100644--- a/src/signature.js+++ b/src/signature.js@@ -119,7 +119,7 @@ export function readFirstCommit(root) {return commit;}-/** Read the hash anchoring the first, intentionally unsigned signer-list version. */+/** Read the first signer-list hash that every later signed receipt binds. */function readInitialSignerHash(root) {const file = join(root, INITIAL_SIGNERS_HASH_FILE);if (!existsSync(file)) throw new Refused('BAD_SIGNERS_BASE', `missing ${INITIAL_SIGNERS_HASH_FILE}; restore the initial signer-list hash`);@@ -131,9 +131,9 @@ function readInitialSignerHash(root) {/** Serialize canonical signed content with stable field order. */export function canonical(record) {if (record.type === 'signers') {- return JSON.stringify({ version: 1, type: record.type, firstCommit: record.firstCommit, previousHash: record.previousHash, previousSigners: record.previousSigners, hash: record.hash, by: record.by, on: record.on });+ return JSON.stringify({ version: 1, type: record.type, firstCommit: record.firstCommit, initialHash: record.initialHash, previousHash: record.previousHash, previousSigners: record.previousSigners, hash: record.hash, by: record.by, on: record.on });}- return JSON.stringify({ version: 1, type: 'row', firstCommit: record.firstCommit, id: record.id, text: record.text, commit: record.commit, by: record.by, on: record.on, note: record.note ?? '' });+ return JSON.stringify({ version: 1, type: 'row', firstCommit: record.firstCommit, initialHash: record.initialHash, id: record.id, text: record.text, commit: record.commit, by: record.by, on: record.on, note: record.note ?? '' });}/** Sign canonical text with ssh-keygen under the fixed Pullboard namespace. */@@ -174,8 +174,10 @@ export function verifySignerHistory(root, records) {const current = readSignerText(root);if (!current) return;const firstCommit = readFirstCommit(root);- let expected = readInitialSignerHash(root);+ const initialHash = readInitialSignerHash(root);+ let expected = initialHash;for (const record of records.filter((entry) => entry.type === 'signers')) {+ if (record.initialHash !== initialHash) throw new Refused('BAD_SIGNERS_BASE', 'a signer-list receipt names a different initial list; restore .pullboard/signers.initial and its signed receipts');if (record.firstCommit !== firstCommit || record.previousHash !== expected || sha256(record.previousSigners ?? '') !== expected) {throw new Refused('UNAUTHORIZED_SIGNERS_CHANGE', 'signer-list history is broken; restore the previous list or sign its change with a key it already listed');}@@ -197,8 +199,10 @@ export function verifySignedRecords(root, records) {if (!current.trim()) throw new Refused('EMPTY_SIGNERS', `${SIGNERS_FILE} is empty; restore its SSH signer list before signing or checking`);verifySignerHistory(root, records);const first = readFirstCommit(root);+ const initialHash = readInitialSignerHash(root);for (const record of records) {if (record.type === 'signers') continue;+ if (record.initialHash !== initialHash) throw new Refused('BAD_SIGNERS_BASE', 'a sign-off names a different initial signer list; restore .pullboard/signers.initial and its signed receipts');if (!record.signature) throw new Refused('MISSING_SIGNATURE', `${record.id ?? 'sign-off'} is unsigned after SSH signers were enabled`);if (record.firstCommit !== first) throw new Refused('BAD_SIGNOFF', `${record.id} was signed for a different first commit`);if (!listsPrincipal(current, record.by)) throw new Refused('UNLISTED_SIGNER', `${record.by} is not listed in ${SIGNERS_FILE}`);@@ -250,6 +254,7 @@ export function addSigner(root, { by, key } = {}) {version: 1,type: 'signers',firstCommit,+ initialHash: readInitialSignerHash(root),previousHash: sha256(text),previousSigners: text,hash: sha256(next),@@ -267,9 +272,10 @@ export function addSigner(root, { by, key } = {}) {export function signRows(root, records, requestedKey) {const firstCommit = readFirstCommit(root);const allowed = readSignerText(root);+ const initialHash = readInitialSignerHash(root);const signed = records.map((fields) => {if (!listsPrincipal(allowed, fields.by)) throw new Refused('UNLISTED_SIGNER', `${fields.by} is not listed in ${SIGNERS_FILE}`);- const record = { version: 1, type: 'row', firstCommit, ...fields };+ const record = { version: 1, type: 'row', firstCommit, initialHash, ...fields };record.signature = makeSignature(root, record, requestedKey);verifySignature(record, allowed);return record;diff --git a/test/signoff.test.js b/test/signoff.test.jsindex 12b7e30c..498f0ca0 100644--- a/test/signoff.test.js+++ b/test/signoff.test.js@@ -1,11 +1,12 @@/** SSH sign-off opt-in, rotation, row requirements and tamper rejection [S17,S18,S19,S20,S21]. */import assert from 'node:assert/strict';import { execFileSync, spawnSync } from 'node:child_process';+import { createHash } from 'node:crypto';import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join, resolve } from 'node:path';import { test } from 'node:test';-import { canonical } from '../src/signature.js';+import { canonical, verifySignature } from '../src/signature.js';const BIN = resolve(import.meta.dirname, '../bin/pullboard.js');const SPEC = `# Sign-off fixture@@ -360,3 +361,66 @@ test('[S21] setup chooses the oldest root with a hash tie-break and refuses an uassert.equal(existsSync(anchor), false, 'signing and checking never reconstruct a missing anchor');}});++for (const rotated of [false, true]) {+ test(`[S19,S20] rewriting the signer base cannot forge an existing principal${rotated ? ' after dropping a real transition' : ''}`, (t) => {+ const box = fixture(t);+ const principal = '[email protected]';+ const originalSpec = SPEC.replace('signers: CO,AB', `signers: ${principal}`)+ + `- G2 [approved, must] The second approved row stays exact. | gate: test/proof.test.js | signers: ${principal}\n`;+ writeFileSync(join(box.root, 'SPEC.md'), originalSpec);+ writeFileSync(join(box.root, 'test/proof.test.js'), "import { test } from 'node:test';\ntest('proof [G1,G2]', () => {});\n");+ box.git('add', 'SPEC.md', 'test/proof.test.js');+ box.git('commit', '-q', '-m', 'chore: commit both signer-test rows');+ succeeds(box, 'signers', 'add');+ succeeds(box, 'signoff', 'G1,G2', '--note', 'genuine signer receipts');+ if (rotated) succeeds(box, 'signers', 'add', '--key', box.ab.publicKey, '--by', '[email protected]');+ succeeds(box, 'check');+ const ledger = join(box.root, '.pullboard/signoffs.jsonl');+ const original = readFileSync(ledger, 'utf8').trim().split('\n').map(JSON.parse);+ assert.equal(original.filter((record) => record.type === 'signers').length, rotated ? 1 : 0);+ const rows = original.filter((record) => record.type === 'row');+ const attacker = makeKey(join(box.keys, 'mallory'));+ const allowedFile = join(box.root, '.pullboard/signers');+ const allowed = readFileSync(allowedFile, 'utf8')+ + `${principal} namespaces="pullboard-signoff" ${readFileSync(attacker.publicKey, 'utf8').trim()}\n`;+ const changedHash = createHash('sha256').update(allowed).digest('hex');+ writeFileSync(allowedFile, allowed);+ writeFileSync(join(box.root, '.pullboard/signers.initial'), `${changedHash}\n`);+ writeFileSync(ledger, rows.map((record) => JSON.stringify(record)).join('\n') + '\n');+ const rewritten = box.run('spec', 'check');+ assert.equal(rewritten.status, 1, 'rewriting the unsigned base cannot leave genuine row signatures valid');+ assert.match(rewritten.stderr, /BAD_SIGNERS_BASE/);++ const weakened = 'An attacker controls the second row.';+ writeFileSync(join(box.root, 'SPEC.md'), originalSpec.replace('The second approved row stays exact.', weakened));+ const forged = { ...rows.find((record) => record.id === 'G2'), text: weakened, initialHash: changedHash };+ const message = join(box.dir, 'forged-message');+ writeFileSync(message, canonical(forged));+ execFileSync('ssh-keygen', ['-Y', 'sign', '-f', attacker.privateKey, '-n', 'pullboard-signoff', message], { cwd: box.root, stdio: 'pipe' });+ forged.signature = readFileSync(`${message}.sig`, 'utf8').trimEnd();+ writeFileSync(ledger, [...rows, forged].map((record) => JSON.stringify(record)).join('\n') + '\n');+ const checked = box.run('spec', 'check', '--json');+ assert.equal(checked.status, 1, 'an actually signed forgery under the appended key cannot re-root the genuine receipts');+ assert.equal(JSON.parse(checked.stdout).error.code, 'BAD_SIGNERS_BASE');+ const shown = box.run('spec', 'show', 'G2', '--json');+ assert.equal(shown.status, 1, 'the forged required signer cannot be reported as met');+ assert.equal(JSON.parse(shown.stdout).error.code, 'BAD_SIGNERS_BASE');++ const relabelled = [...rows.map((record) => ({ ...record, initialHash: changedHash })), forged];+ writeFileSync(ledger, relabelled.map((record) => JSON.stringify(record)).join('\n') + '\n');+ const rebound = box.run('spec', 'check');+ assert.equal(rebound.status, 1, 'changing the base field on genuine receipts invalidates their actual SSH signatures');+ assert.match(rebound.stderr, /BAD_SIGNATURE/);+ });+}++test('[S20] a signer-change signature binds its initial-list hash', (t) => {+ const box = fixture(t);+ succeeds(box, 'signers', 'add', '--by', 'CO');+ succeeds(box, 'signers', 'add', '--key', box.ab.publicKey, '--by', 'AB');+ const change = readFileSync(join(box.root, '.pullboard/signoffs.jsonl'), 'utf8').trim().split('\n').map(JSON.parse).find((record) => record.type === 'signers');+ assert.match(change.initialHash, /^[0-9a-f]{64}$/);+ assert.equal(verifySignature(change, change.previousSigners), true);+ assert.throws(() => verifySignature({ ...change, initialHash: '0'.repeat(64) }, change.previousSigners), { code: 'BAD_SIGNATURE' });+});