NewCommandValidator builds a validator with default rules.
(logger *zap.Logger)
| 43 | |
| 44 | // NewCommandValidator builds a validator with default rules. |
| 45 | func NewCommandValidator(logger *zap.Logger) *CommandValidator { |
| 46 | validator := &CommandValidator{ |
| 47 | logger: logger, |
| 48 | allowSudo: strings.EqualFold(os.Getenv("CHATCLI_AGENT_ALLOW_SUDO"), "true"), |
| 49 | inlineCodeAnalyzer: NewInlineCodeRiskAnalyzer(), |
| 50 | } |
| 51 | |
| 52 | // Default dangerous patterns. The inline-code regex set has been |
| 53 | // removed: those invocations are now classified dynamically by the |
| 54 | // analyzer — `python -c "print(1)"` is safe, `python -c "import os; |
| 55 | // os.system(...)"` is not. |
| 56 | defaultPatterns := []string{ |
| 57 | `(?i)rm\s+-rf\s+`, |
| 58 | `(?i)rm\s+--no-preserve-root`, |
| 59 | `(?i)dd\s+if=`, |
| 60 | `(?i)mkfs\w*\s+`, |
| 61 | `(?i)shutdown(\s+|$)`, |
| 62 | `(?i)reboot(\s+|$)`, |
| 63 | `(?i)init\s+0`, |
| 64 | `(?i)curl\s+[^\|;]*\|\s*sh`, |
| 65 | `(?i)wget\s+[^\|;]*\|\s*sh`, |
| 66 | `(?i)curl\s+[^\|;]*\|\s*bash`, |
| 67 | `(?i)wget\s+[^\|;]*\|\s*bash`, |
| 68 | `(?i)\bsudo\b.*`, |
| 69 | `(?i)\bdrop\s+database\b`, |
| 70 | `(?i)\bmkfs\b`, |
| 71 | `(?i)\buserdel\b`, |
| 72 | `(?i)\bchmod\s+777\s+/.*`, |
| 73 | `(?i)\bbase64\b.*\|\s*(sh|bash|zsh|dash)`, |
| 74 | `(?i)\beval\s+`, |
| 75 | `(?i)\$\(\s*curl`, |
| 76 | `(?i)\$\(\s*wget`, |
| 77 | "(?i)`\\s*curl", |
| 78 | "(?i)`\\s*wget", |
| 79 | `(?i)\bchown\s+-R\s+.*\s+/`, |
| 80 | `(?i)>\s*/etc/`, |
| 81 | `(?i)>\s*/dev/[sh]d`, |
| 82 | `(?i)\bsource\s+/dev/tcp`, |
| 83 | `(?i)/dev/tcp/`, |
| 84 | `(?i)\bexport\s+.*PATH\s*=`, |
| 85 | `(?i)\bnc\b.*-[el]`, |
| 86 | `(?i)\bncat\b.*-[el]`, |
| 87 | `(?i)\bxargs\b.*\b(rm|del|shutdown|reboot|mkfs)\b`, |
| 88 | `(?i)\bfind\b.*-exec\b.*(rm|del|shutdown|reboot)\b`, |
| 89 | `(?i)\bcrontab\s+-r\b`, |
| 90 | `(?i)\biptables\s+-F\b`, |
| 91 | `(?i)\bsysctl\s+-w\b`, |
| 92 | `(?i)\bkillall\b`, |
| 93 | `(?i)\bpkill\s+-9\b`, |
| 94 | `(?i)\bexec\s+\d*[<>]`, |
| 95 | `(?i)>\s*/proc/`, |
| 96 | `(?i)\btee\s+/etc/`, |
| 97 | `(?i)\bsource\s+/dev/`, |
| 98 | `(?i)\benv\b.*\|\s*(sh|bash)`, |
| 99 | `\$\{[^}]*[;|&][^}]*\}`, |
| 100 | `(?i)\$\(\s*(bash|sh|zsh|dash)\b`, |
| 101 | `<\(`, |
| 102 | `>\(`, |