* Check for potentially dangerous command patterns
(command: string)
| 194 | * Check for potentially dangerous command patterns |
| 195 | */ |
| 196 | public static checkDangerousPatterns(command: string): string[] { |
| 197 | const warnings: string[] = []; |
| 198 | |
| 199 | // Guard against undefined or null commands |
| 200 | if (!command || typeof command !== 'string') { |
| 201 | return warnings; |
| 202 | } |
| 203 | |
| 204 | const patterns = [ |
| 205 | { pattern: /rm\s+-rf\s+\/(?:\s|$)/, message: 'Destructive command on root directory' }, |
| 206 | { pattern: /rm\s+-rf\s+~/, message: 'Destructive command on home directory' }, |
| 207 | { pattern: /:\s*\(\s*\)\s*\{.*\}\s*;/, message: 'Fork bomb pattern detected' }, |
| 208 | { pattern: /curl.*\|\s*(?:bash|sh)/, message: 'Downloading and executing remote code' }, |
| 209 | { pattern: /wget.*\|\s*(?:bash|sh)/, message: 'Downloading and executing remote code' }, |
| 210 | { pattern: />\/dev\/sda/, message: 'Direct disk write operation' }, |
| 211 | { pattern: /sudo\s+/, message: 'Elevated privileges required' }, |
| 212 | { pattern: /dd\s+.*of=\/dev\//, message: 'Dangerous disk operation' }, |
| 213 | { pattern: /mkfs\./, message: 'Filesystem formatting command' }, |
| 214 | { pattern: /:(){ :|:& };:/, message: 'Fork bomb detected' }, |
| 215 | ]; |
| 216 | |
| 217 | for (const { pattern, message } of patterns) { |
| 218 | if (pattern.test(command)) { |
| 219 | warnings.push(message); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // Check for unescaped variables that could lead to code injection |
| 224 | if (command.includes('$') && !command.includes('"$')) { |
| 225 | warnings.push('Unquoted shell variable detected - potential code injection risk'); |
| 226 | } |
| 227 | |
| 228 | return warnings; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Escape a command for safe shell execution |
no outgoing calls
no test coverage detected