mirror of
https://github.com/affaan-m/ECC.git
synced 2026-11-24 00:35:31 +00:00
fix(antigravity): guard hook stdout json parse to prevent false denials
This commit is contained in:
1 parent
9dfa26f24d
commit
611ec0d3ab
3 files changed
+45
-6
No files matched your search
@@ -113,19 +113,32 @@ async function main() {
|
||||
result = {
|
||||
exitCode: child.error || child.status === null ? 2 : child.status,
|
||||
stderr: child.stderr || '',
|
||||
stdout: typeof child.stdout === 'string' ? child.stdout : '',
|
||||
};
|
||||
} else if (options.hook === 'pre:write:doc-file-warning') {
|
||||
result = runDocFileWarning(eccJsonString);
|
||||
}
|
||||
|
||||
const output = result && (result.stdout || result.output);
|
||||
const hookOutput = output ? JSON.parse(output).hookSpecificOutput : null;
|
||||
const denied = result && ((result.exitCode !== undefined && result.exitCode !== 0) || result.denied || result.blocked)
|
||||
|| hookOutput?.permissionDecision === 'deny';
|
||||
const output = typeof result?.stdout === 'string'
|
||||
? result.stdout
|
||||
: (typeof result?.output === 'string' ? result.output : null);
|
||||
let hookOutput = null;
|
||||
if (output) {
|
||||
try {
|
||||
const parsed = JSON.parse(output);
|
||||
hookOutput = parsed && typeof parsed === 'object' ? parsed.hookSpecificOutput : null;
|
||||
} catch {
|
||||
hookOutput = null;
|
||||
}
|
||||
}
|
||||
const denied = Boolean(
|
||||
(result && ((result.exitCode !== undefined && result.exitCode !== 0) || result.denied || result.blocked))
|
||||
|| hookOutput?.permissionDecision === 'deny'
|
||||
);
|
||||
const context = result?.additionalContext || hookOutput?.additionalContext;
|
||||
const reason = denied
|
||||
? hookOutput?.permissionDecisionReason || result.stderr || result.reason || 'Blocked by ECC safety hook'
|
||||
: Array.isArray(context) ? context.join(' ') : context;
|
||||
? hookOutput?.permissionDecisionReason || result?.stderr || result?.reason || 'Blocked by ECC safety hook'
|
||||
: (Array.isArray(context) ? context.join(' ') : context);
|
||||
process.stdout.write(JSON.stringify({
|
||||
decision: denied ? 'deny' : 'allow',
|
||||
...(reason ? { reason: String(reason).replace(/\r?\n/g, ' ').trim() } : {}),
|
||||
|
||||
@@ -477,6 +477,29 @@ function runTests() {
|
||||
assert.match(output.reason, /completely|input/i);
|
||||
});
|
||||
|
||||
test('antigravity-hook-bridge safely ignores non-JSON hook stdout and allows safe tool call', () => {
|
||||
const bridgePath = path.join(__dirname, '..', '..', 'scripts', 'hooks', 'antigravity-hook-bridge.js');
|
||||
const input = JSON.stringify({
|
||||
conversationId: 'test-conv',
|
||||
workspacePaths: ['/tmp'],
|
||||
toolCall: {
|
||||
name: 'run_command',
|
||||
args: {
|
||||
CommandLine: 'echo safe',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const run = spawnSync(process.execPath, [bridgePath, '--mode', 'pre-tool-use', '--hook', 'pre:write:doc-file-warning'], {
|
||||
input,
|
||||
encoding: 'utf8',
|
||||
});
|
||||
|
||||
assert.strictEqual(run.status, 0);
|
||||
const output = JSON.parse(run.stdout);
|
||||
assert.strictEqual(output.decision, 'allow');
|
||||
});
|
||||
|
||||
test('antigravity-project plans mcp_config.json when platform configs are selected', () => {
|
||||
const projectRoot = createTempDir('antigravity-proj-mcp-');
|
||||
try {
|
||||
|
||||
@@ -62,6 +62,9 @@ async function main() {
|
||||
}
|
||||
assert.strictEqual((await invoke({ hook: 'pre:config-protection', child: { status: 0, stderr: '' } })).response.decision, 'allow');
|
||||
assert.strictEqual((await invoke({ result: '{}' })).response.decision, 'allow');
|
||||
assert.strictEqual((await invoke({ result: { exitCode: 0, stdout: 'warning: non-json line' } })).response.decision, 'allow');
|
||||
assert.strictEqual((await invoke({ result: { exitCode: 0, output: ['ignored', 'output array'] } })).response.decision, 'allow');
|
||||
assert.strictEqual((await invoke({ hook: 'pre:config-protection', child: { status: 0, stdout: 'non-json warning', stderr: '' } })).response.decision, 'allow');
|
||||
assert.strictEqual((await invoke({ mode: 'stop', raw: '{' })).response.decision, 'allow');
|
||||
assert.strictEqual((await invoke({ mode: 'post-tool-use', hook: 'post:edit:accumulator' })).hookCalls, 1);
|
||||
assert.strictEqual((await invoke({ mode: 'post-tool-use', hook: 'post:edit:accumulator', raw: JSON.stringify({ error: 'write failed' }) })).hookCalls, 0);
|
||||
|
||||
Reference in new issue
Block a user