diff --git a/.antigravity-plugin/hooks.json b/.antigravity-plugin/hooks.json index 8935093a1..816cae758 100644 --- a/.antigravity-plugin/hooks.json +++ b/.antigravity-plugin/hooks.json @@ -6,7 +6,7 @@ "hooks": [ { "type": "command", - "command": "node -e \"const p=require('path'),f=require('fs');function G(){var e=process.env.ANTIGRAVITY_PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT||process.env.ECC_PLUGIN_ROOT;if(e&&f.existsSync(p.join(e,'scripts','antigravity','hook-bridge.js')))return p.join(e,'scripts','antigravity','hook-bridge.js');try{var r=p.resolve('.agents','ecc-install-state.json');if(f.existsSync(r)){var s=JSON.parse(f.readFileSync(r,'utf8'));if(s&&s.schemaVersion==='ecc.install.v1'&&s.target&&s.target.id==='antigravity-project'){var b=p.resolve('.agents','scripts','antigravity','hook-bridge.js');if(b.startsWith(p.resolve('.agents')+p.sep)&&f.existsSync(b))return b;}}}catch(_){}var h=require('os').homedir();var c=[p.join(h,'.claude','plugins','marketplaces','ecc'),p.join(h,'.gemini','config','plugins','ecc'),p.join(h,'.gemini','antigravity'),p.join(h,'.claude')];for(var i=0;i { + const fs = require('fs'); + const os = require('os'); + const tempWorkspace = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-user-project-')); + try { + const protectedFile = path.join(tempWorkspace, 'eslint.config.js'); + fs.writeFileSync(protectedFile, 'module.exports = {};'); + const safeFile = path.join(tempWorkspace, 'safe-app.js'); + fs.writeFileSync(safeFile, 'console.log("hello");'); + + const hooksConfig = JSON.parse(fs.readFileSync(path.join(REPO_ROOT, 'hooks', 'antigravity-hooks.json'), 'utf8')); + const hookCmd = hooksConfig['ecc-guardrails'].PreToolUse[0].hooks[0].command; + + const envTrusted = { + ...process.env, + ANTIGRAVITY_PLUGIN_ROOT: REPO_ROOT, + }; + + // Allowed call + const resAllow = spawnSync(hookCmd, { + shell: true, + input: JSON.stringify({ + conversationId: 'allow-cmd-test', + toolCall: { name: 'edit_file', args: { Path: safeFile } }, + workspacePaths: [tempWorkspace], + }), + encoding: 'utf8', + env: envTrusted, + cwd: tempWorkspace, + }); + + assert.strictEqual(resAllow.status, 0); + const allowDecision = JSON.parse(resAllow.stdout.trim()); + assert.strictEqual(allowDecision.decision, 'allow'); + + // Denied call + const resDeny = spawnSync(hookCmd, { + shell: true, + input: JSON.stringify({ + conversationId: 'deny-cmd-test', + toolCall: { name: 'edit_file', args: { Path: protectedFile } }, + workspacePaths: [tempWorkspace], + }), + encoding: 'utf8', + env: envTrusted, + cwd: tempWorkspace, + }); + + assert.strictEqual(resDeny.status, 0); + const denyDecision = JSON.parse(resDeny.stdout.trim()); + assert.strictEqual(denyDecision.decision, 'deny'); + assert.ok(denyDecision.reason && denyDecision.reason.length > 0); + } finally { + fs.rmSync(tempWorkspace, { recursive: true, force: true }); + } +}); + +test('registered command in .antigravity-plugin/hooks.json executes allowed and denied calls via entry point', () => { + const fs = require('fs'); + const os = require('os'); + const tempWorkspace = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-user-plugin-project-')); + try { + const protectedFile = path.join(tempWorkspace, 'eslint.config.js'); + fs.writeFileSync(protectedFile, 'module.exports = {};'); + const safeFile = path.join(tempWorkspace, 'safe-app.js'); + fs.writeFileSync(safeFile, 'console.log("hello");'); + + const hooksConfig = JSON.parse(fs.readFileSync(path.join(REPO_ROOT, '.antigravity-plugin', 'hooks.json'), 'utf8')); + const hookCmd = hooksConfig['ecc-guardrails'].PreToolUse[0].hooks[0].command; + + const envTrusted = { + ...process.env, + ANTIGRAVITY_PLUGIN_ROOT: REPO_ROOT, + }; + + // Allowed call + const resAllow = spawnSync(hookCmd, { + shell: true, + input: JSON.stringify({ + conversationId: 'allow-plugin-cmd-test', + toolCall: { name: 'edit_file', args: { Path: safeFile } }, + workspacePaths: [tempWorkspace], + }), + encoding: 'utf8', + env: envTrusted, + cwd: tempWorkspace, + }); + + assert.strictEqual(resAllow.status, 0); + const allowDecision = JSON.parse(resAllow.stdout.trim()); + assert.strictEqual(allowDecision.decision, 'allow'); + + // Denied call + const resDeny = spawnSync(hookCmd, { + shell: true, + input: JSON.stringify({ + conversationId: 'deny-plugin-cmd-test', + toolCall: { name: 'edit_file', args: { Path: protectedFile } }, + workspacePaths: [tempWorkspace], + }), + encoding: 'utf8', + env: envTrusted, + cwd: tempWorkspace, + }); + + assert.strictEqual(resDeny.status, 0); + const denyDecision = JSON.parse(resDeny.stdout.trim()); + assert.strictEqual(denyDecision.decision, 'deny'); + assert.ok(denyDecision.reason && denyDecision.reason.length > 0); + } finally { + fs.rmSync(tempWorkspace, { recursive: true, force: true }); + } +}); + +test('rejects execution of forged-receipt sentinel planted in untrusted project', () => { + const fs = require('fs'); + const os = require('os'); + const tempUntrusted = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-forged-repo-')); + try { + const decoyAgents = path.join(tempUntrusted, '.agents'); + const decoyBridgeDir = path.join(decoyAgents, 'scripts', 'antigravity'); + fs.mkdirSync(decoyBridgeDir, { recursive: true }); + + // Plant forged receipt + fs.writeFileSync(path.join(decoyAgents, 'ecc-install-state.json'), JSON.stringify({ + schemaVersion: 'ecc.install.v1', + target: { id: 'antigravity-project' }, + })); + + // Plant sentinel bridge that creates a marker file + const sentinelFile = path.join(tempUntrusted, 'forged-sentinel-executed.marker'); + const maliciousCode = ` + require('fs').writeFileSync(${JSON.stringify(sentinelFile)}, 'FORGED_EXECUTED'); + module.exports = { main() { process.stdout.write(JSON.stringify({ decision: 'deny', reason: 'SENTINEL' })); } }; + `; + fs.writeFileSync(path.join(decoyBridgeDir, 'hook-bridge.js'), maliciousCode); + + const hooksConfig = JSON.parse(fs.readFileSync(path.join(REPO_ROOT, 'hooks', 'antigravity-hooks.json'), 'utf8')); + const hookCmd = hooksConfig['ecc-guardrails'].PreToolUse[0].hooks[0].command; + + const cleanEnv = { ...process.env }; + delete cleanEnv.ANTIGRAVITY_PLUGIN_ROOT; + delete cleanEnv.CLAUDE_PLUGIN_ROOT; + delete cleanEnv.ECC_PLUGIN_ROOT; + cleanEnv.HOME = path.join(tempUntrusted, 'empty-home'); + cleanEnv.USERPROFILE = path.join(tempUntrusted, 'empty-home'); + + const result = spawnSync(hookCmd, { + shell: true, + input: JSON.stringify({ + conversationId: 'forged-receipt-test', + toolCall: { name: 'edit_file', args: { Path: 'safe.js' } }, + }), + encoding: 'utf8', + cwd: tempUntrusted, + env: cleanEnv, + }); + + assert.strictEqual(result.status, 0, 'Hook runner must succeed and fail open'); + assert.strictEqual(fs.existsSync(sentinelFile), false, 'Forged receipt sentinel must NOT be executed!'); + const decision = JSON.parse(result.stdout.trim()); + assert.strictEqual(decision.decision, 'allow'); + } finally { + fs.rmSync(tempUntrusted, { recursive: true, force: true }); + } +}); + +test('rejects symlink-decoy realpath escapes pointing into untrusted directory', () => { + const fs = require('fs'); + const os = require('os'); + const tempUntrusted = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-symlink-repo-')); + try { + const sentinelFile = path.join(tempUntrusted, 'symlink-sentinel-executed.marker'); + const sentinelDir = path.join(tempUntrusted, 'sentinel-src', 'scripts', 'antigravity'); + fs.mkdirSync(sentinelDir, { recursive: true }); + fs.writeFileSync(path.join(sentinelDir, 'hook-bridge.js'), ` + require('fs').writeFileSync(${JSON.stringify(sentinelFile)}, 'SYMLINK_EXECUTED'); + module.exports = { main() { process.stdout.write(JSON.stringify({ decision: 'deny', reason: 'SENTINEL' })); } }; + `); + + // Create a symlink or junction attempting to escape to sentinel + const symlinkDir = path.join(tempUntrusted, 'symlink-plugin'); + try { + fs.symlinkSync(path.join(tempUntrusted, 'sentinel-src'), symlinkDir, 'junction'); + } catch (_symErr) { + // If junction/symlink fails on restricted Windows permissions, skip symlink creation + return; + } + + const hooksConfig = JSON.parse(fs.readFileSync(path.join(REPO_ROOT, 'hooks', 'antigravity-hooks.json'), 'utf8')); + const hookCmd = hooksConfig['ecc-guardrails'].PreToolUse[0].hooks[0].command; + + const envWithSymlink = { + ...process.env, + ANTIGRAVITY_PLUGIN_ROOT: symlinkDir, + }; + + const result = spawnSync(hookCmd, { + shell: true, + input: JSON.stringify({ + conversationId: 'symlink-test', + toolCall: { name: 'edit_file', args: { Path: 'safe.js' } }, + }), + encoding: 'utf8', + cwd: tempUntrusted, + env: envWithSymlink, + }); + + assert.strictEqual(result.status, 0); + assert.strictEqual(fs.existsSync(sentinelFile), false, 'Symlink decoy sentinel must NOT be executed!'); + const decision = JSON.parse(result.stdout.trim()); + assert.strictEqual(decision.decision, 'allow'); + } finally { + fs.rmSync(tempUntrusted, { recursive: true, force: true }); + } +}); + console.log(`\nResults: Passed: ${passed}, Failed: ${failed}`); if (failed > 0) { process.exit(1); diff --git a/tests/lib/antigravity-install-target.test.js b/tests/lib/antigravity-install-target.test.js index 62717f452..f0b990bb9 100644 --- a/tests/lib/antigravity-install-target.test.js +++ b/tests/lib/antigravity-install-target.test.js @@ -171,6 +171,41 @@ test('pinned native loader receipt establishes distinct editor and CLI capabilit 'search_web', ]); assert.strictEqual(editorParsed.tools.includes('invoke_subagent'), false, 'Editor runtime must exclude invoke_subagent'); + + // Isolated tool mappings for editor + const grepAdapted = adaptAntigravityAgent('---\nname: grep-agent\ntools: Grep\n---\nbody', 'agents/grep.md', { runtime: 'editor' }); + assert.deepStrictEqual(yaml.load(grepAdapted.match(/^---\n([\s\S]*?)\n---\n/)[1]).tools, ['view_file']); + + const globAdapted = adaptAntigravityAgent('---\nname: glob-agent\ntools: Glob\n---\nbody', 'agents/glob.md', { runtime: 'editor' }); + assert.deepStrictEqual(yaml.load(globAdapted.match(/^---\n([\s\S]*?)\n---\n/)[1]).tools, ['view_file']); + + const fetchAdapted = adaptAntigravityAgent('---\nname: fetch-agent\ntools: WebFetch\n---\nbody', 'agents/fetch.md', { runtime: 'editor' }); + assert.deepStrictEqual(yaml.load(fetchAdapted.match(/^---\n([\s\S]*?)\n---\n/)[1]).tools, ['search_web']); + + const taskAdapted = adaptAntigravityAgent('---\nname: task-agent\ntools: Task\n---\nbody', 'agents/task.md', { runtime: 'editor' }); + assert.deepStrictEqual(yaml.load(taskAdapted.match(/^---\n([\s\S]*?)\n---\n/)[1]).tools, []); + + // Exercise ANTIGRAVITY_RUNTIME environment variable + const priorRuntime = process.env.ANTIGRAVITY_RUNTIME; + try { + process.env.ANTIGRAVITY_RUNTIME = 'editor'; + const envAdapted = adaptAntigravityAgent(source, 'agents/full-agent.md'); + const envParsed = yaml.load(envAdapted.match(/^---\n([\s\S]*?)\n---\n/)[1]); + assert.deepStrictEqual(envParsed.tools, [ + 'view_file', + 'write_to_file', + 'replace_file_content', + 'run_command', + 'search_web', + ]); + assert.strictEqual(envParsed.tools.includes('invoke_subagent'), false); + } finally { + if (priorRuntime === undefined) { + delete process.env.ANTIGRAVITY_RUNTIME; + } else { + process.env.ANTIGRAVITY_RUNTIME = priorRuntime; + } + } }); test('transform preserves CRLF line endings consistently on Windows-saved agents', () => {