Skip to content

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.

fix(hooks): bound staged secret scans [P4]

Corey Olson906a443b543f
diff --git a/src/git.js b/src/git.js
index bbc75048..fbe3aa65 100644
--- a/src/git.js
+++ b/src/git.js
@@ -28,13 +28,15 @@ export function gitChildEnv(root) {
*
* @param {string} cwd
* @param {string[]} args
+ * @param {{ maxBuffer?: number }} [options] - Bound output captures for callers parsing larger diffs.
* @returns {string}
*/
-export function git(cwd, args) {
+export function git(cwd, args, { maxBuffer } = {}) {
return execFileSync('git', [...GIT_FLAGS, ...args], {
cwd,
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
+ ...(maxBuffer === undefined ? {} : { maxBuffer }),
}).trim();
}
diff --git a/src/hooks.js b/src/hooks.js
index 9e73ebc8..c1bab03e 100644
--- a/src/hooks.js
+++ b/src/hooks.js
@@ -7,6 +7,7 @@ import { spawnSync } from 'node:child_process';
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { git, gitChildEnv, tryGit } from './git.js';
+import { Refused } from './refused.js';
import { outOfLane } from './lanes.js';
import { citedIds, deletedIds, idProblems } from './spec.js';
@@ -18,6 +19,7 @@ export const FIX_NOTE = 'Fix what each line names. Never work around a refusal w
const EMOJI_RE = /\p{Extended_Pictographic}|\p{Regional_Indicator}|⃣|️/u;
const EXEMPT_RE = /^(Merge |Revert "|fixup! |squash! |amend! )/;
const ZERO_SHA = /^0+$/;
+const MAX_SECRET_DIFF_BYTES = 8 * 1024 * 1024;
/**
* Secrets by kind, most specific first, so a line is named once by what it most likely is.
@@ -204,7 +206,13 @@ export function preCommitProblems({ root, isMain, config, agent }) {
(path) => `${path} is a blocked file (env or secrets); keep it out of git`,
);
if (config.protect.secrets) {
- const diff = git(root, ['diff', '--cached', '--text', '--no-ext-diff', '--no-textconv', '--no-color', '-U0']);
+ let diff;
+ try {
+ diff = git(root, ['diff', '--cached', '--text', '--no-ext-diff', '--no-textconv', '--no-color', '-U0'], { maxBuffer: MAX_SECRET_DIFF_BYTES });
+ } catch (error) {
+ if (error.code !== 'ENOBUFS') throw error;
+ throw new Refused('DIFF_TOO_LARGE', 'the staged text diff exceeds the safe scan limit; split the change into smaller commits, then retry');
+ }
problems.push(...secretsIn(addedLines(diff)).map((where) => `possible secret: ${where}`));
}
problems.push(...deletedRowProblems(root, [config.spec, config.practice]));
diff --git a/test/hooks.test.js b/test/hooks.test.js
index f238d6ab..e5e44276 100644
--- a/test/hooks.test.js
+++ b/test/hooks.test.js
@@ -1,15 +1,36 @@
/**
- * The commit-message rules (C1, C2) and the pre-commit pattern checks, without git.
+ * The commit-message rules (C1, C2) and the pre-commit pattern checks on real staged changes.
*/
import assert from 'node:assert/strict';
+import { execFileSync } from 'node:child_process';
+import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
import { test } from 'node:test';
import { defaults } from '../src/config.js';
-import { addedLines, blockedPaths, commitMsgProblems, matchesPattern, secretsIn } from '../src/hooks.js';
+import { addedLines, blockedPaths, commitMsgProblems, matchesPattern, preCommitProblems, secretsIn } from '../src/hooks.js';
import { parseSpec } from '../src/spec.js';
+import { Refused } from '../src/refused.js';
const SPEC = { ...parseSpec('## G\n- G1 [approved, must] One. | gate: t\n- G2 [retired] Gone.\n'), name: 'SPEC.md' };
const RULES = defaults().commits;
+/** Make a private real repository for staged-diff hook tests, with cleanup tied to the test. */
+function stagedRepo(t) {
+ const root = mkdtempSync(join(tmpdir(), 'pullboard-staged-diff-'));
+ const env = { ...process.env, GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1' };
+ t.after(() => rmSync(root, { recursive: true, force: true }));
+ /** Run Git only in this fixture, without the maintainer's identity or configuration. */
+ const git = (...args) => execFileSync('git', args, { cwd: root, env, encoding: 'utf8', stdio: 'pipe' }).trim();
+ git('init', '-q', '-b', 'main');
+ return { root, git };
+}
+
+/** Run the same staged protections called by the installed pre-commit hook. */
+function stagedProblems(root) {
+ return preCommitProblems({ root, isMain: true, config: defaults(), agent: null });
+}
+
/**
* Problems with a message under the default rules, or the given ones.
*/
@@ -78,6 +99,45 @@ test('secrets are named by kind and place, never echoed', () => {
assert.ok(found.every((entry) => !entry.includes('abcdefghij')));
});
+test('the secret scan handles 2.4 MiB of binary images and finds an embedded key [P4]', (t) => {
+ const box = stagedRepo(t);
+ const secret = `sk-ant-${'A'.repeat(32)}`;
+ for (let index = 0; index < 17; index += 1) {
+ const image = Buffer.alloc(150_000);
+ image.set([0xff, 0xd8, 0xff, 0xe0, 0, 0x10, 0x4a, 0x46, 0x49, 0x46, 0]);
+ if (index === 0) image.write(`${secret}\n`, 11, 'ascii');
+ writeFileSync(join(box.root, `image-${index}.jpg`), image);
+ }
+ box.git('add', '--', '*.jpg');
+ const problems = stagedProblems(box.root);
+ assert.deepEqual(problems, ['possible secret: Anthropic key at image-0.jpg:1']);
+ assert.ok(!problems.join(' ').includes(secret));
+});
+
+test('a staged diff beyond the safe output limit is a private coded refusal [P4]', (t) => {
+ const box = stagedRepo(t);
+ const marker = 'STAGED_CONTENT_MUST_NOT_BE_ECHOED';
+ writeFileSync(join(box.root, 'large.txt'), `${marker}\n${'ordinary text\n'.repeat(700_000)}`);
+ box.git('add', '--', 'large.txt');
+ assert.throws(() => stagedProblems(box.root), (error) => {
+ assert.ok(error instanceof Refused);
+ assert.equal(error.code, 'DIFF_TOO_LARGE');
+ assert.match(error.message, /split the change into smaller commits, then retry/);
+ assert.ok(!error.message.includes(marker));
+ return true;
+ });
+});
+
+test('pre-commit still refuses and redacts a staged text secret [P4]', (t) => {
+ const box = stagedRepo(t);
+ const secret = `sk-ant-${'A'.repeat(32)}`;
+ writeFileSync(join(box.root, 'settings.js'), `const token = \"${secret}\";\n`);
+ box.git('add', '--', 'settings.js');
+ const problems = stagedProblems(box.root);
+ assert.deepEqual(problems, ['possible secret: Anthropic key at settings.js:1']);
+ assert.ok(!problems.join(' ').includes(secret));
+});
+
test('a refused merge message points to the message git writes, which is exempt [C4]', () => {
assert.match(check('merge: include store date validation')[0], /For a merge, keep the message git writes, which is exempt: git merge --no-edit/);
assert.deepEqual(check("Merge branch 'store/1'"), []);