// node9Firewall#383 follow-up: a command wrapped many times. Past the nesting // limit the walk used to stop reading and say nothing (`eval` x25 + // `cat ~/.ssh/id_rsa` was ALLOW), and the smart-rule readings recursed without // a bound (a 1,500-deep chain outlasted the agent's hook timeout). import { describe, it, expect, afterEach } from 'vitest'; import { analysisBudgetSpent, analyzeFsOperation, beginAnalysisBudget, commandReadings, endAnalysisBudget, MAX_UNWRAP_DEPTH, parseShared, readingsPastLimit, } from './index'; const wrapped = (n: number, inner: string) => 'eval '.repeat(n) + inner; describe('the nesting limit is never silent', () => { it('still reads and blocks a command at the limit', () => { expect(analyzeFsOperation(wrapped(MAX_UNWRAP_DEPTH, 'rm -rf ~/'))?.verdict).toBe('block'); expect(analyzeFsOperation(wrapped(MAX_UNWRAP_DEPTH, 'cat ~/.ssh/id_rsa'))?.verdict).toBe( 'block' ); }); it.each([MAX_UNWRAP_DEPTH + 1, 40, 100])( 'asks about a jailed read and a home delete nested %i deep, never allows', (n) => { for (const inner of ['cat ~/.ssh/id_rsa', 'rm -rf ~/']) { const v = analyzeFsOperation(wrapped(n, inner)); expect(v?.verdict).toBe('review'); expect(v?.ruleName).toBe('review-unanalysable-nesting'); } } ); it('the same for a find action: read inside the limit, asked about past it', () => { const find = (n: number) => `find . -exec sh -c ${JSON.stringify(wrapped(n, 'cat ~/.ssh/id_rsa'))} \\;`; expect(analyzeFsOperation(find(MAX_UNWRAP_DEPTH - 2))?.verdict).toBe('block'); expect(analyzeFsOperation(find(MAX_UNWRAP_DEPTH + 5))?.ruleName).toBe( 'review-unanalysable-nesting' ); }); it('a block elsewhere in the same command still wins', () => { const cmd = `${wrapped(30, 'ls')}; cat ~/.ssh/id_rsa`; expect(analyzeFsOperation(cmd)?.verdict).toBe('block'); }); }); describe('the readings are bounded by the same limit', () => { it('a 1,500-deep chain stops at the limit instead of reading every level', () => { // The work is bounded by the limit (24 levels), not by the chain: the // time it takes is the analysis budget's to watch, not this test's. const readings = commandReadings(wrapped(1500, 'rm -rf ~/')); expect(readings.length).toBeLessThanOrEqual(MAX_UNWRAP_DEPTH + 3); expect(readings).not.toContain('rm -rf ~/'); // Up to 24 parses of a 7.5 KB command: past vitest's 5 s default on a // loaded machine, so the bound is set here and the count is what is tested. }, 60_000); it('says when the readings stopped with a payload left', () => { expect(readingsPastLimit(wrapped(MAX_UNWRAP_DEPTH - 1, 'ls'))).toBe(false); expect(readingsPastLimit(wrapped(MAX_UNWRAP_DEPTH + 3, 'ls'))).toBe(true); expect(readingsPastLimit('git status')).toBe(false); }); it('a payload just inside the limit is still a reading', () => { expect(commandReadings(wrapped(MAX_UNWRAP_DEPTH - 1, 'rm -rf ~/'))).toContain('rm -rf ~/'); }); }); describe('the analysis budget', () => { afterEach(() => beginAnalysisBudget(null)); it('is off unless a caller starts it', () => { beginAnalysisBudget(null); expect(typeof parseShared('echo budget-off')).toBe('object'); expect(analysisBudgetSpent()).toBe(false); }); it('past its deadline a parse is skipped, not cached, and the budget is spent', async () => { beginAnalysisBudget(1); await new Promise((r) => setTimeout(r, 5)); const skipped = parseShared('echo after-the-deadline'); expect(typeof skipped).toBe('symbol'); expect(analysisBudgetSpent()).toBe(true); // The miss was not cached: with no budget the same command parses. beginAnalysisBudget(null); expect(typeof parseShared('echo after-the-deadline')).toBe('object'); }); it('ending it keeps the answer and lets later steps run', async () => { beginAnalysisBudget(1); await new Promise((r) => setTimeout(r, 5)); parseShared('echo spent-then-ended'); endAnalysisBudget(); expect(analysisBudgetSpent()).toBe(true); expect(typeof parseShared('echo runs-after-the-end')).toBe('object'); }); });