From d1423cd1a96a1dbadcc648bf7fbd775cff6d1db5 Mon Sep 17 00:00:00 2001 From: t Date: Fri, 10 Jul 2026 14:30:09 -0700 Subject: [PATCH] fix: fail closed across hook decision schemas --- .claude/dispatch/inbox.md | 3 -- README.md | 2 +- bin/gstack-team-init | 16 ++++++---- implementation-notes.html | 37 ----------------------- test/team-mode.test.ts | 63 +++++++++++++++++++++++++++++++++++++-- 5 files changed, 73 insertions(+), 48 deletions(-) delete mode 100644 .claude/dispatch/inbox.md delete mode 100644 implementation-notes.html diff --git a/.claude/dispatch/inbox.md b/.claude/dispatch/inbox.md deleted file mode 100644 index 41069cfa8..000000000 --- a/.claude/dispatch/inbox.md +++ /dev/null @@ -1,3 +0,0 @@ -# Dispatch inbox - -- [ ] Investigate Windows-safe suite filtering and prerequisites for `test/readme-throughput.test.ts` and `browse/test/tab-isolation.test.ts`; `origin/main` reproduced the failures. (files: package.json, test/readme-throughput.test.ts, browse/test/tab-isolation.test.ts) — noticed 2026-07-10 diff --git a/README.md b/README.md index 8f191f445..121f42099 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ From inside your repo, paste this. Switches you to team mode, bootstraps the rep (cd ~/.claude/skills/gstack && ./setup --team) && ~/.claude/skills/gstack/bin/gstack-team-init required && git add .claude/ CLAUDE.md && git commit -m "require gstack for AI-assisted work" ``` -No vendored gstack install in your repo, no version drift, no manual upgrades. Required mode commits one Node 18+ CommonJS enforcement hook, so the same project hook works from POSIX and Windows hook runners without duplicate shell-specific handlers. If the hook runner omits its project directory or provides a stale path, enforcement fails closed with an intentional denial instead of a hook error. Every Claude Code session starts with a fast auto-update check (throttled to once/hour, network-failure-safe, completely silent). +Your project stays small, and everyone gets the same up-to-date version of gstack. If gstack is missing or its safety check cannot run, Claude Code and Copilot stop and explain what to do. If everything is ready, the check stays out of the way and your usual permission questions still appear. Updates happen quietly when a Claude Code session starts, at most once an hour. Swap `required` for `optional` if you'd rather nudge teammates than block them. diff --git a/bin/gstack-team-init b/bin/gstack-team-init index bf5a07428..6157bad9c 100755 --- a/bin/gstack-team-init +++ b/bin/gstack-team-init @@ -155,11 +155,15 @@ Then restart your AI coding tool. `; process.stderr.write(instructions); + const decision = { + permissionDecision: 'deny', + permissionDecisionReason: instructions, + }; process.stdout.write(`${JSON.stringify({ + ...decision, hookSpecificOutput: { hookEventName: 'PreToolUse', - permissionDecision: 'deny', - permissionDecisionReason: instructions, + ...decision, }, })}\n`); } @@ -179,10 +183,11 @@ HOOK_EOF if (!settings.hooks) settings.hooks = {}; if (!settings.hooks.PreToolUse) settings.hooks.PreToolUse = []; - const hookCommand = \"node -e \\\"const deny=reason=>{console.error(reason);console.log(JSON.stringify({hookSpecificOutput:{hookEventName:'PreToolUse',permissionDecision:'deny',permissionDecisionReason:reason}}))};const projectDir=process.env.CLAUDE_PROJECT_DIR;if(!projectDir){deny('BLOCKED: CLAUDE_PROJECT_DIR is unavailable, so the required gstack hook cannot be loaded.')}else{try{require(require('node:path').join(projectDir, '.claude', 'hooks', 'check-gstack.cjs'))}catch{deny('BLOCKED: the required gstack hook could not be loaded. Verify project hook setup and retry.')}}\\\"\"; + const hookMatcher = 'Skill|skill'; + const hookCommand = \"node -e \\\"const deny=reason=>{const decision={permissionDecision:'deny',permissionDecisionReason:reason};console.error(reason);console.log(JSON.stringify({...decision,hookSpecificOutput:{hookEventName:'PreToolUse',...decision}}))};const projectDir=process.env.CLAUDE_PROJECT_DIR;if(!projectDir){deny('BLOCKED: CLAUDE_PROJECT_DIR is unavailable, so the required gstack hook cannot be loaded.')}else{try{require(require('node:path').join(projectDir, '.claude', 'hooks', 'check-gstack.cjs'))}catch{deny('BLOCKED: the required gstack hook could not be loaded. Verify project hook setup and retry.')}}\\\"\"; let found = false; settings.hooks.PreToolUse = settings.hooks.PreToolUse.filter(entry => { - if (entry.matcher !== 'Skill' || !entry.hooks) return true; + if (!['Skill', hookMatcher].includes(entry.matcher) || !entry.hooks) return true; const matchingHooks = entry.hooks.filter( h => h.command && h.command.includes('check-gstack') @@ -193,6 +198,7 @@ HOOK_EOF h => !h.command || !h.command.includes('check-gstack') ); if (!found) { + entry.matcher = hookMatcher; entry.hooks.push({ type: 'command', command: hookCommand }); found = true; } @@ -201,7 +207,7 @@ HOOK_EOF if (!found) { settings.hooks.PreToolUse.push({ - matcher: 'Skill', + matcher: hookMatcher, hooks: [{ type: 'command', command: hookCommand diff --git a/implementation-notes.html b/implementation-notes.html deleted file mode 100644 index c740af189..000000000 --- a/implementation-notes.html +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - Cross-platform gstack enforcement hook - - -
-

Cross-platform gstack enforcement hook

- -

Design decisions

- - -

Deviations

-

None. The implementation uses the requested Node launcher, one hook handler, and the repository's documented hook output schema.

- -

Tradeoffs

- - -

Open questions

-

None.

-
- - diff --git a/test/team-mode.test.ts b/test/team-mode.test.ts index 17c815160..5a4715e2e 100644 --- a/test/team-mode.test.ts +++ b/test/team-mode.test.ts @@ -89,6 +89,8 @@ function expectStructuredDeny( expect(result.stderr.trim()).toBe(reason); expect(result.stderr).not.toContain('MODULE_NOT_FOUND'); expect(JSON.parse(result.stdout)).toEqual({ + permissionDecision: 'deny', + permissionDecisionReason: reason, hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny', @@ -329,11 +331,11 @@ describe('gstack-team-init', () => { const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf-8')); expect(settings.hooks.PreToolUse).toHaveLength(1); const entry = settings.hooks.PreToolUse[0]; - expect(entry.matcher).toBe('Skill'); + expect(entry.matcher).toBe('Skill|skill'); expect(entry.hooks).toHaveLength(1); expect(entry.hooks[0]).not.toHaveProperty('shell'); expect(entry.hooks[0].command).toBe( - `node -e "const deny=reason=>{console.error(reason);console.log(JSON.stringify({hookSpecificOutput:{hookEventName:'PreToolUse',permissionDecision:'deny',permissionDecisionReason:reason}}))};const projectDir=process.env.CLAUDE_PROJECT_DIR;if(!projectDir){deny('BLOCKED: CLAUDE_PROJECT_DIR is unavailable, so the required gstack hook cannot be loaded.')}else{try{require(require('node:path').join(projectDir, '.claude', 'hooks', 'check-gstack.cjs'))}catch{deny('BLOCKED: the required gstack hook could not be loaded. Verify project hook setup and retry.')}}"`, + `node -e "const deny=reason=>{const decision={permissionDecision:'deny',permissionDecisionReason:reason};console.error(reason);console.log(JSON.stringify({...decision,hookSpecificOutput:{hookEventName:'PreToolUse',...decision}}))};const projectDir=process.env.CLAUDE_PROJECT_DIR;if(!projectDir){deny('BLOCKED: CLAUDE_PROJECT_DIR is unavailable, so the required gstack hook cannot be loaded.')}else{try{require(require('node:path').join(projectDir, '.claude', 'hooks', 'check-gstack.cjs'))}catch{deny('BLOCKED: the required gstack hook could not be loaded. Verify project hook setup and retry.')}}"`, ); expect(entry.hooks[0].command).not.toMatch( /\$CLAUDE_PROJECT_DIR|\$env:CLAUDE_PROJECT_DIR|%CLAUDE_PROJECT_DIR%/, @@ -498,6 +500,10 @@ describe('gstack-team-init', () => { expect(result.stderr).toContain('git clone --depth 1'); const decision = JSON.parse(result.stdout); expect(decision).toEqual({ + permissionDecision: 'deny', + permissionDecisionReason: expect.stringContaining( + 'Then restart your AI coding tool.', + ), hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny', @@ -508,6 +514,58 @@ describe('gstack-team-init', () => { }); }); + test('required: install verification errors return a structured deny', () => { + run(`${TEAM_INIT} required`, { cwd: tmpDir }); + const settings = JSON.parse( + fs.readFileSync(path.join(tmpDir, '.claude', 'settings.json'), 'utf-8'), + ); + const command = settings.hooks.PreToolUse[0].hooks[0].command; + const fakeHome = path.join(tmpDir, 'unreadable-home'); + const preload = path.join(tmpDir, 'fail-gstack-stat.cjs'); + fs.mkdirSync(fakeHome, { recursive: true }); + fs.writeFileSync( + preload, + `'use strict'; +const fs = require('node:fs'); +const original = fs.statSync; +fs.statSync = function (target, ...args) { + if (String(target).replace(/\\\\/g, '/').includes('skills/gstack/bin')) { + const error = new Error('injected verification failure'); + error.code = 'EACCES'; + throw error; + } + return original.call(this, target, ...args); +}; +`, + ); + + const result = runHook(command, { + cwd: tmpDir, + env: { + CLAUDE_PROJECT_DIR: tmpDir, + HOME: fakeHome, + USERPROFILE: fakeHome, + NODE_OPTIONS: `--require=${preload}`, + }, + }); + + expect(result.exitCode).toBe(0); + expect(result.stderr).toContain( + 'BLOCKED: the global gstack install could not be verified.', + ); + expect(result.stderr).not.toContain('injected verification failure'); + const decision = JSON.parse(result.stdout); + expect(decision.permissionDecision).toBe('deny'); + expect(decision.permissionDecisionReason).toContain( + 'BLOCKED: the global gstack install could not be verified.', + ); + expect(decision.hookSpecificOutput).toEqual({ + hookEventName: 'PreToolUse', + permissionDecision: 'deny', + permissionDecisionReason: decision.permissionDecisionReason, + }); + }); + test('required: rerun removes a legacy Bash hook without touching other hooks', () => { const hooksDir = path.join(tmpDir, '.claude', 'hooks'); const legacyHook = path.join(hooksDir, 'check-gstack.sh'); @@ -576,6 +634,7 @@ describe('gstack-team-init', () => { ).toBe(''); expect(settings.hooks.PreToolUse).toHaveLength(1); expect(settings.hooks.PreToolUse[0].hooks).toHaveLength(1); + expect(settings.hooks.PreToolUse[0].matcher).toBe('Skill|skill'); expect(settings.hooks.PreToolUse[0].hooks[0].command).toContain( 'check-gstack.cjs', );