From ad90aa67015db839189542224bfc10a49ad9de3d Mon Sep 17 00:00:00 2001 From: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com> Date: Tue, 17 Nov 2026 04:17:35 -0800 Subject: [PATCH] fix(hooks): keep a GateGuard deny when another Bash hook adds context GateGuard denies by printing a PreToolUse permissionDecision on stdout at exit 0. runHooks only stopped early on a non-zero exit, and its final return preferred any advisory additionalContext over the hook stdout, so in the strict profile the git-push reminder replaced GateGuard's deny and the command ran. Stop at the first deny and return it unchanged. --- scripts/hooks/bash-hook-dispatcher.js | 31 ++++++++++++++++++++++++ tests/hooks/bash-hook-dispatcher.test.js | 25 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/scripts/hooks/bash-hook-dispatcher.js b/scripts/hooks/bash-hook-dispatcher.js index f76aadad8..03381e7b7 100644 --- a/scripts/hooks/bash-hook-dispatcher.js +++ b/scripts/hooks/bash-hook-dispatcher.js @@ -106,6 +106,27 @@ function normalizeHookResult(previousRaw, output) { }; } +/** + * True when hook stdout is a PreToolUse `deny` decision. GateGuard and other + * hooks deny by writing that JSON to stdout at exit code 0, so the dispatcher + * must recognise it as a decision rather than as pass-through output. + */ +function isPermissionDeny(raw) { + if (typeof raw !== 'string' || !raw.includes('permissionDecision')) { + return false; + } + try { + const parsed = JSON.parse(raw); + return Boolean( + parsed + && parsed.hookSpecificOutput + && parsed.hookSpecificOutput.permissionDecision === 'deny' + ); + } catch { + return false; + } +} + function runHooks(rawInput, hooks) { let currentRaw = rawInput; // Track whether a sub-hook deliberately produced stdout (a string or @@ -134,6 +155,16 @@ function runHooks(rawInput, hooks) { if (result.additionalContext) { additionalContext = combineAdditionalContext(additionalContext, result.additionalContext); } + // A deny is a decision, not a note: stop here so neither a later hook + // nor an earlier hook's advisory additionalContext can replace it. + if (result.exitCode === 0 && rawModified && isPermissionDeny(currentRaw)) { + return { + output: currentRaw, + stderr, + additionalContext, + exitCode: 0, + }; + } if (result.exitCode !== 0) { return { output: rawModified ? currentRaw : '', diff --git a/tests/hooks/bash-hook-dispatcher.test.js b/tests/hooks/bash-hook-dispatcher.test.js index 2f98d7aeb..243458d21 100644 --- a/tests/hooks/bash-hook-dispatcher.test.js +++ b/tests/hooks/bash-hook-dispatcher.test.js @@ -134,6 +134,31 @@ function runTests() { assert.ok(!disabled.stderr.includes('Review changes before push'), 'Disabled hook should not emit reminder'); })) passed++; else failed++; + if (test('pre dispatcher keeps a GateGuard deny when an advisory hook also adds context', () => { + // In the strict profile the git-push reminder adds additionalContext for + // every `git push`, and GateGuard denies a force-push with a + // permissionDecision on stdout at exit 0. The advisory note must not + // replace the denial. + const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-bash-dispatcher-gateguard-')); + try { + const input = { + tool_name: 'Bash', + session_id: 'dispatcher-deny-precedence', + tool_input: { command: 'git push --force origin main' }, + }; + const result = runScript(preDispatcher, input, { + ECC_HOOK_PROFILE: 'strict', + GATEGUARD_STATE_DIR: stateDir, + }); + assert.strictEqual(result.status, 0, result.stderr); + const output = parseHookOutput(result.stdout).hookSpecificOutput; + assert.strictEqual(output.permissionDecision, 'deny', `Expected GateGuard deny, got: ${result.stdout}`); + assert.ok(output.permissionDecisionReason.includes('Destructive command'), 'Expected the GateGuard reason'); + } finally { + fs.rmSync(stateDir, { recursive: true, force: true }); + } + })) passed++; else failed++; + if (test('pre dispatcher respects hook profiles inside the consolidated path', () => { const input = { tool_input: { command: 'git push origin main' } }; const result = runScript(preDispatcher, input, { ECC_HOOK_PROFILE: 'minimal' });