// Static pattern scan. It never blocks a submission on its own: every submission goes to // human review, and these findings are shown to the reviewer next to the AI review. export type Finding = { severity: 'high' | 'medium' | 'low'; rule: string; file: string; line: number; excerpt: string }; export type ScanFile = { path: string; text: string }; type Rule = { id: string; severity: Finding['severity']; re: RegExp; note: string }; const RULES: Rule[] = [ { id: 'pipe-to-shell', severity: 'high', re: /\b(curl|wget|iwr|Invoke-WebRequest)\b[^\n|]*\|\s*(sudo\s+)?(ba|z|da)?sh\b/i, note: 'Downloads a script and runs it in one step' }, { id: 'pipe-to-interpreter', severity: 'high', re: /\b(curl|wget)\b[^\n|]*\|\s*(python3?|node|perl|ruby)\b/i, note: 'Downloads code and runs it in one step' }, { id: 'reverse-shell', severity: 'high', re: /\/dev\/tcp\/|\bnc(at)?\b[^\n]*\s-e\s|\bbash\s+-i\s+>&|\bsocat\b[^\n]*exec:/i, note: 'Looks like a reverse shell' }, { id: 'destructive-rm', severity: 'high', re: /\brm\s+-[a-z]*r[a-z]*f?[a-z]*\s+(--no-preserve-root\s+)?(\/|~|\$HOME)(\s|$|\*)/i, note: 'Deletes a home or root directory' }, { id: 'credential-files', severity: 'high', re: /(~|\$HOME|\/home\/[^/\s]+|\/Users\/[^/\s]+)\/\.(ssh\/id_|aws\/credentials|netrc|config\/gcloud|kube\/config|gnupg)|\.claude\/\.credentials|\bid_(rsa|ed25519)\b|\/etc\/shadow/i, note: 'Reads credential or key files' }, { id: 'secret-exfiltration', severity: 'high', re: /\b(curl|wget|fetch|requests\.(post|get)|http\.request)\b[^\n]*\$\{?(ANTHROPIC|OPENAI|AWS|GITHUB|GH|STRIPE|CLOUDFLARE|GOOGLE)[A-Z_]*(KEY|TOKEN|SECRET)/i, note: 'Sends an API key or token over the network' }, { id: 'env-dump', severity: 'medium', re: /\b(printenv|env)\b\s*(\||>)\s*|\bprocess\.env\b[^\n]*\b(fetch|axios|http)|\bos\.environ\b[^\n]*\brequests\./i, note: 'Collects environment variables' }, { id: 'obfuscated-exec', severity: 'high', re: /\bbase64\s+(-d|--decode|-D)\b[^\n]*\|\s*(ba|z)?sh\b|\beval\s*\(\s*(atob|Buffer\.from)\s*\(|\bexec\s*\(\s*base64\.b64decode|\bpython3?\s+-c\s+["'][^"']*base64/i, note: 'Decodes and runs hidden code' }, { id: 'prompt-injection', severity: 'high', re: /\b(ignore|disregard|forget)\s+(all\s+|any\s+)?(previous|prior|earlier|above)\s+(instructions|rules|prompts)|\bdo\s+not\s+(tell|inform|mention\s+(this\s+)?to)\s+the\s+user\b|\bwithout\s+(telling|informing|asking)\s+the\s+user\b|\bhide\s+this\s+from\s+the\s+user\b/i, note: 'Tells the agent to override its instructions or hide actions from the user' }, { id: 'safety-bypass', severity: 'medium', re: /--dangerously-skip-permissions|\bbypass\s+(the\s+)?(permission|safety|approval)|\bdisable\s+(the\s+)?(sandbox|safety|guardrails)/i, note: 'Asks to turn off permission or safety checks' }, { id: 'persistence', severity: 'medium', re: /\bcrontab\s+-|>>\s*~?\/?[^\s]*\.(bashrc|zshrc|profile|bash_profile)\b|\blaunchctl\s+load\b|\/etc\/cron\.|\bsystemctl\s+enable\b/i, note: 'Installs something that keeps running or runs at login' }, { id: 'sudo', severity: 'low', re: /\bsudo\s+\S+/i, note: 'Runs a command as root' }, { id: 'crypto-wallet', severity: 'medium', re: /\b(wallet\.dat|seed\s+phrase|mnemonic\s+phrase|private\s+key)\b[^\n]*\b(send|upload|post|curl)|\bxmrig\b|\bstratum\+tcp:\/\//i, note: 'Touches crypto wallets or mining' }, { id: 'chmod-exec-download', severity: 'medium', re: /\b(curl|wget)\b[^\n]*(-o|-O|>)\s*\S+[^\n]*(&&|;)\s*chmod\s+\+x/i, note: 'Downloads a file and makes it executable' }, { id: 'paste-site', severity: 'medium', re: /https?:\/\/(pastebin\.com|paste\.ee|transfer\.sh|0x0\.st|ngrok\.io|[a-z0-9-]+\.ngrok(-free)?\.app|webhook\.site|requestbin\.[a-z]+|pipedream\.net)\b/i, note: 'Links to a paste, tunnel or request-capture service' }, ]; const HIDDEN_CHARS = /[​-‏‪-‮⁠-⁤⁦-⁩\u{E0000}-\u{E007F}]/u; const LONG_BLOB = /[A-Za-z0-9+/=]{400,}/; export function staticScan(files: ScanFile[]): { status: 'clean' | 'flagged' | 'high'; findings: Finding[] } { const findings: Finding[] = []; for (const f of files) { const lines = f.text.split('\n'); for (let i = 0; i < lines.length && findings.length < 60; i++) { const line = lines[i]; if (line.length > 4000 && !LONG_BLOB.test(line)) continue; for (const rule of RULES) { if (rule.re.test(line)) { findings.push({ severity: rule.severity, rule: rule.note, file: f.path, line: i + 1, excerpt: line.trim().slice(0, 200) }); } } if (i > 0 || !line.startsWith('')) { if (HIDDEN_CHARS.test(i === 0 ? line.replace(/^/, '') : line)) { findings.push({ severity: 'high', rule: 'Contains invisible or direction-changing characters that can hide text', file: f.path, line: i + 1, excerpt: JSON.stringify(line.trim().slice(0, 120)) }); } } if (LONG_BLOB.test(line)) { findings.push({ severity: 'medium', rule: 'Contains a long encoded blob that cannot be read by eye', file: f.path, line: i + 1, excerpt: line.trim().slice(0, 80) + '…' }); } } } const status = findings.some((f) => f.severity === 'high') ? 'high' : findings.length ? 'flagged' : 'clean'; return { status, findings }; }