|
| 1 | +/** |
| 2 | + * Content scanner for SKILL.md files. |
| 3 | + * Detects prompt injection, invisible chars, and suspicious patterns. |
| 4 | + * Pure function, no async, no dependencies — fast and testable. |
| 5 | + */ |
| 6 | + |
| 7 | +export type RiskLabel = "safe" | "caution" | "danger" | "unknown"; |
| 8 | + |
| 9 | +export interface ScanResult { |
| 10 | + label: RiskLabel; |
| 11 | + findings: string[]; |
| 12 | +} |
| 13 | + |
| 14 | +// DANGER patterns — any single match = "danger" |
| 15 | +const INVISIBLE_UNICODE = /[\u200B-\u200D\uFEFF\u2060-\u2064\u2066-\u206F]/g; |
| 16 | +const ANSI_ESCAPE = /\x1B\[[0-9;]*[A-Za-z]/g; |
| 17 | +const PROMPT_INJECTION_PATTERNS = [ |
| 18 | + /ignore\s+(all\s+)?(previous|prior|above)\s+(instructions|prompts|rules)/i, |
| 19 | + /you\s+are\s+now\s+(?:a|an|the|my)\s+/i, |
| 20 | + /(?:reveal|show|print|output|leak)\s+(?:the\s+)?system\s+prompt/i, |
| 21 | + /(?:override|replace|rewrite)\s+(?:the\s+)?system\s+prompt/i, |
| 22 | +]; |
| 23 | +const JS_PROTOCOL = /javascript\s*:/i; |
| 24 | +const DATA_HTML = /data\s*:\s*text\/html/i; |
| 25 | +const SHELL_INJECTION = /(?:\$\([^)]+\)|eval\s*\(|exec\s*\()/; |
| 26 | + |
| 27 | +// CAUTION patterns — 2+ matches = "caution" |
| 28 | +const CAUTION_PATTERNS: Array<{ regex: RegExp; label: string }> = [ |
| 29 | + { regex: /<script/i, label: "html-script-tag" }, |
| 30 | + { regex: /<iframe/i, label: "html-iframe-tag" }, |
| 31 | + { regex: /<object/i, label: "html-object-tag" }, |
| 32 | + { regex: /<embed/i, label: "html-embed-tag" }, |
| 33 | + { regex: /<form/i, label: "html-form-tag" }, |
| 34 | + { regex: /(?:bit\.ly|tinyurl\.com|t\.co|goo\.gl)\//i, label: "url-shortener" }, |
| 35 | + { regex: /[A-Za-z0-9+/]{200,}={0,2}/, label: "base64-block" }, |
| 36 | + { regex: /<!--[\s\S]{500,}?-->/, label: "hidden-html-comment" }, |
| 37 | + { regex: /process\.env/i, label: "env-access" }, |
| 38 | + { regex: /fs\.readFile/i, label: "fs-read" }, |
| 39 | + { regex: /child_process/i, label: "child-process" }, |
| 40 | + // XML-style tags commonly used in prompt injection (moved from DANGER) |
| 41 | + { regex: /^\s*<system/im, label: "xml-system-tag" }, |
| 42 | + { regex: /^\s*<assistant/im, label: "xml-assistant-tag" }, |
| 43 | + { regex: /^\s*<human/im, label: "xml-human-tag" }, |
| 44 | +]; |
| 45 | + |
| 46 | +export function scanContent(content: string): ScanResult { |
| 47 | + const findings: string[] = []; |
| 48 | + |
| 49 | + // DANGER checks |
| 50 | + const zwChars = content.match(INVISIBLE_UNICODE); |
| 51 | + if (zwChars) { |
| 52 | + findings.push(`danger:invisible-chars:${zwChars.length} zero-width characters`); |
| 53 | + } |
| 54 | + |
| 55 | + const ansi = content.match(ANSI_ESCAPE); |
| 56 | + if (ansi) { |
| 57 | + findings.push(`danger:ansi-escape:${ansi.length} terminal escape codes`); |
| 58 | + } |
| 59 | + |
| 60 | + for (const pattern of PROMPT_INJECTION_PATTERNS) { |
| 61 | + if (pattern.test(content)) { |
| 62 | + findings.push(`danger:prompt-injection:${pattern.source}`); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (JS_PROTOCOL.test(content)) { |
| 67 | + findings.push("danger:js-protocol:javascript: URL detected"); |
| 68 | + } |
| 69 | + |
| 70 | + if (DATA_HTML.test(content)) { |
| 71 | + findings.push("danger:data-html:data:text/html URL detected"); |
| 72 | + } |
| 73 | + |
| 74 | + if (SHELL_INJECTION.test(content)) { |
| 75 | + findings.push("danger:shell-injection:shell command pattern detected"); |
| 76 | + } |
| 77 | + |
| 78 | + // CAUTION checks |
| 79 | + let cautionCount = 0; |
| 80 | + for (const { regex, label } of CAUTION_PATTERNS) { |
| 81 | + if (regex.test(content)) { |
| 82 | + findings.push(`caution:${label}`); |
| 83 | + cautionCount++; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Derive label |
| 88 | + const hasDanger = findings.some((f) => f.startsWith("danger:")); |
| 89 | + let label: RiskLabel; |
| 90 | + if (hasDanger) { |
| 91 | + label = "danger"; |
| 92 | + } else if (cautionCount >= 2) { |
| 93 | + label = "caution"; |
| 94 | + } else { |
| 95 | + label = "safe"; |
| 96 | + } |
| 97 | + |
| 98 | + return { label, findings }; |
| 99 | +} |
| 100 | + |
| 101 | +/** Strip zero-width Unicode chars and ANSI escape codes from content */ |
| 102 | +export function sanitizeContent(content: string): string { |
| 103 | + return content |
| 104 | + .replace(INVISIBLE_UNICODE, "") |
| 105 | + .replace(ANSI_ESCAPE, ""); |
| 106 | +} |
0 commit comments