Files
adnify/tests/agent/utils/commandApproval.test.ts
adnaan 6c7e7f9ad0 feat: Enhance command approval system and UI for terminal commands
- Introduced deriveTerminalCommandRule to derive conservative command scopes without AI proposals.
- Updated ToolCallCard to validate terminal command rules and derive rules when necessary.
- Added a new approvals tab in UnifiedStatusTray to manage pending tool calls.
- Enhanced SecuritySettings to allow adding cross-task automation rules.
- Improved error handling and logging in TerminalManager for clipboard pasting.
- Updated tests to cover new command approval logic and terminal manager functionality.
- Refined command approval rules to ensure safety and prevent misuse of dynamic launcher commands.
2026-08-25 11:21:52 +08:00

70 lines
3.9 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
deriveTerminalCommandRule,
isTerminalCommandAutoApproved,
isTerminalCommandEligibleForAutoApproval,
matchesTerminalCommandRule,
validateTerminalCommandRuleProposal,
} from '@renderer/agent/utils/commandApproval'
const rule = (executable: string, ...argumentPrefix: string[]) => ({ executable, argumentPrefix })
describe('terminal command approval', () => {
it('matches a literal executable and argument prefix', () => {
expect(matchesTerminalCommandRule('git status', rule('git', 'status'))).toBe(true)
expect(matchesTerminalCommandRule('git status --short', rule('git', 'status'))).toBe(true)
expect(matchesTerminalCommandRule('git statusx', rule('git', 'status'))).toBe(false)
})
it('requires both a trusted executable and a matching terminal rule', () => {
expect(isTerminalCommandEligibleForAutoApproval('node --version', [rule('node', '--version')], ['node'])).toBe(true)
expect(isTerminalCommandEligibleForAutoApproval('custom-tool --version', [rule('custom-tool', '--version')], ['node'])).toBe(false)
expect(isTerminalCommandEligibleForAutoApproval('rm -rf build', [rule('rm', '-rf')], ['rm'])).toBe(false)
})
it('requires every compound subcommand to be allowed', () => {
expect(isTerminalCommandAutoApproved('git status && npm test', [rule('git', 'status'), rule('npm', 'test')])).toBe(true)
expect(isTerminalCommandAutoApproved('git status && rm -rf build', [rule('git', 'status')])).toBe(false)
expect(isTerminalCommandAutoApproved('git status; npm test', [rule('git', 'status')])).toBe(false)
})
it('does not auto-approve dynamic command substitution', () => {
expect(isTerminalCommandAutoApproved('echo $(dangerous)', [rule('echo', 'safe')])).toBe(false)
expect(isTerminalCommandAutoApproved('echo `dangerous`', [rule('echo', 'safe')])).toBe(false)
})
it('never auto-approves destructive commands even when a rule matches', () => {
expect(isTerminalCommandAutoApproved('rm -rf build', [rule('rm', '-rf')])).toBe(false)
expect(isTerminalCommandAutoApproved('powershell -EncodedCommand ZQBjAGgAbwA=', [rule('powershell', '-EncodedCommand')])).toBe(false)
expect(isTerminalCommandAutoApproved('git reset --hard HEAD', [rule('git', 'reset')])).toBe(false)
})
it('accepts only AI proposals that are literal prefixes of the actual safe command', () => {
expect(validateTerminalCommandRuleProposal('pnpm run test --watch', {
executable: 'pnpm', argument_prefix: ['run', 'test'], description: 'Run tests',
})).toEqual({ executable: 'pnpm', argumentPrefix: ['run', 'test'], description: 'Run tests' })
expect(validateTerminalCommandRuleProposal('git status --short', {
executable: 'git', argument_prefix: ['diff'], description: 'Too broad and mismatched',
})).toBeNull()
expect(validateTerminalCommandRuleProposal('git status && npm test', {
executable: 'git', argument_prefix: ['status'], description: 'Compound',
})).toBeNull()
expect(validateTerminalCommandRuleProposal('git reset --hard HEAD', {
executable: 'git', argument_prefix: ['reset'], description: 'Dangerous',
})).toBeNull()
})
it('derives a conservative similar-command scope without an AI proposal', () => {
expect(deriveTerminalCommandRule('git status --short')).toEqual(rule('git', 'status'))
expect(deriveTerminalCommandRule('pnpm run test --watch')).toEqual(rule('pnpm', 'run', 'test'))
expect(deriveTerminalCommandRule('node --version')).toEqual(rule('node', '--version'))
})
it('does not derive scopes for compound, path-bound, destructive, or dynamic launcher commands', () => {
expect(deriveTerminalCommandRule('git status | Out-File result.txt')).toBeNull()
expect(deriveTerminalCommandRule('node scripts/build.js')).toBeNull()
expect(deriveTerminalCommandRule('rm -rf build')).toBeNull()
expect(deriveTerminalCommandRule('powershell -Command Get-Date')).toBeNull()
})
})