Summary
Enhance the PolicyEngine to leverage existing shell parsing utilities for more granular and sophisticated policy rules on shell commands. This will enable policies based on specific commands within compound statements, operators used, and command patterns.
Context
The codebase already has robust shell parsing capabilities:
shell-utils.ts provides splitCommands(), getCommandRoot(), and pattern detection
shell-quote library is installed and can parse complex shell syntax into AST-like structures
- The shell tool currently extracts root commands for allowlisting
The PolicyEngine should leverage these capabilities to enable more sophisticated policies.
Proposed Implementation
1. Add Shell Command Analysis to PolicyEngine
Create a shell analysis utility that extracts:
- Individual commands from compound statements
- Operators used (
&&, ||, |, ;, &)
- Dangerous patterns (pipes, background jobs, command substitution)
- Command arguments and flags
2. Extend PolicyRule for Shell-Specific Matching
interface PolicyRule {
// Existing fields...
// New shell-specific fields
shellCommand?: string; // Match specific shell commands (e.g., "rm", "git")
shellPattern?: RegExp; // Match command patterns (e.g., /^rm.*-rf/)
allowPipes?: boolean; // Policy on piped commands
allowBackground?: boolean; // Policy on background jobs
allowConditionals?: boolean; // Policy on && and || operators
maxChainLength?: number; // Limit command chain length
}
3. Example Policy Configurations
// Deny any rm command with -rf flags
{
shellCommand: 'rm',
shellPattern: /-rf|-fr/,
decision: PolicyDecision.DENY
}
// Allow git commands but not in pipes
{
shellCommand: 'git',
allowPipes: false,
decision: PolicyDecision.ALLOW
}
// Require confirmation for compound commands
{
allowConditionals: true,
maxChainLength: 2,
decision: PolicyDecision.ASK_USER
}
4. Integration with Tool Confirmation Message Bus
When a shell command is submitted:
- Parse the command using existing utilities
- Extract all root commands and operators
- Check each command against PolicyRules
- Apply the highest priority matching rule
- Consider operator-based rules (pipes, conditionals)
Implementation Details
Utility Function
import { parse } from 'shell-quote';
import { getCommandRoots, detectCommandSubstitution } from '../utils/shell-utils.js';
export function analyzeShellCommand(command: string) {
const roots = getCommandRoots(command);
const parsed = parse(command);
const operators = parsed.filter(item =>
typeof item === 'object' && 'op' in item
).map(item => item.op);
return {
rootCommands: roots,
operators,
hasPipes: operators.includes('|'),
hasConditionals: operators.includes('&&') || operators.includes('||'),
hasBackground: operators.includes('&'),
hasSubstitution: detectCommandSubstitution(command),
chainLength: roots.length,
parsed
};
}
PolicyEngine Enhancement
The PolicyEngine.check() method would:
- Detect if the tool call is a shell command
- Analyze the command structure
- Apply shell-specific rules in addition to general rules
- Return the appropriate decision based on the analysis
Benefits
- Granular Control: Policies can target specific dangerous commands or patterns
- Security: Better detection and prevention of risky command combinations
- Flexibility: Different rules for different command types and operators
- User Experience: More intelligent confirmation prompts based on actual risk
Dependencies
Acceptance Criteria
Related Issues
/cc @allenhutchison
Summary
Enhance the PolicyEngine to leverage existing shell parsing utilities for more granular and sophisticated policy rules on shell commands. This will enable policies based on specific commands within compound statements, operators used, and command patterns.
Context
The codebase already has robust shell parsing capabilities:
shell-utils.tsprovidessplitCommands(),getCommandRoot(), and pattern detectionshell-quotelibrary is installed and can parse complex shell syntax into AST-like structuresThe PolicyEngine should leverage these capabilities to enable more sophisticated policies.
Proposed Implementation
1. Add Shell Command Analysis to PolicyEngine
Create a shell analysis utility that extracts:
&&,||,|,;,&)2. Extend PolicyRule for Shell-Specific Matching
3. Example Policy Configurations
4. Integration with Tool Confirmation Message Bus
When a shell command is submitted:
Implementation Details
Utility Function
PolicyEngine Enhancement
The PolicyEngine.check() method would:
Benefits
Dependencies
Acceptance Criteria
Related Issues
/cc @allenhutchison