Branchwell

Skillfarm / website · skillfarm.ai / src / lib / scan.ts

Raw 5.1 KB on 9a071bd1de362520c199c643265ba6c57566da6f
1// Static pattern scan. It never blocks a submission on its own: every submission goes to
2// human review, and these findings are shown to the reviewer next to the AI review.
3
4export type Finding = { severity: 'high' | 'medium' | 'low'; rule: string; file: string; line: number; excerpt: string };
5export type ScanFile = { path: string; text: string };
6
7type Rule = { id: string; severity: Finding['severity']; re: RegExp; note: string };
8
9const RULES: Rule[] = [
10 { 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' },
11 { 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' },
12 { 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' },
13 { 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' },
14 { 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' },
15 { 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' },
16 { 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' },
17 { 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' },
18 { 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' },
19 { 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' },
20 { 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' },
21 { id: 'sudo', severity: 'low', re: /\bsudo\s+\S+/i, note: 'Runs a command as root' },
22 { 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' },
23 { 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' },
24 { 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' },
25];
26
27const HIDDEN_CHARS = /[​-‏‪-‮⁠-⁤⁦-⁩\u{E0000}-\u{E007F}]/u;
28const LONG_BLOB = /[A-Za-z0-9+/=]{400,}/;
29
30export function staticScan(files: ScanFile[]): { status: 'clean' | 'flagged' | 'high'; findings: Finding[] } {
31 const findings: Finding[] = [];
32 for (const f of files) {
33 const lines = f.text.split('\n');
34 for (let i = 0; i < lines.length && findings.length < 60; i++) {
35 const line = lines[i];
36 if (line.length > 4000 && !LONG_BLOB.test(line)) continue;
37 for (const rule of RULES) {
38 if (rule.re.test(line)) {
39 findings.push({ severity: rule.severity, rule: rule.note, file: f.path, line: i + 1, excerpt: line.trim().slice(0, 200) });
40 }
41 }
42 if (i > 0 || !line.startsWith('')) {
43 if (HIDDEN_CHARS.test(i === 0 ? line.replace(/^/, '') : line)) {
44 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)) });
45 }
46 }
47 if (LONG_BLOB.test(line)) {
48 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) + '…' });
49 }
50 }
51 }
52 const status = findings.some((f) => f.severity === 'high') ? 'high' : findings.length ? 'flagged' : 'clean';
53 return { status, findings };
54}