|
| 1 | +export type PromptInjectionVerdict = 'allow' | 'block' | 'review'; |
| 2 | + |
| 3 | +export interface PromptInjectionReason { |
| 4 | + code: string; |
| 5 | + message: string; |
| 6 | +} |
| 7 | + |
| 8 | +export interface PromptInjectionCheck { |
| 9 | + verdict: PromptInjectionVerdict; |
| 10 | + score: number; |
| 11 | + reasons: PromptInjectionReason[]; |
| 12 | +} |
| 13 | + |
| 14 | +interface Rule { |
| 15 | + code: string; |
| 16 | + message: string; |
| 17 | + score: number; |
| 18 | + regex: RegExp; |
| 19 | +} |
| 20 | + |
| 21 | +const SPACE_RE = /\s+/g; |
| 22 | +const BASE64_RE = /[A-Za-z0-9+/]{24,}={0,2}/; |
| 23 | + |
| 24 | +const RULES: Rule[] = [ |
| 25 | + { |
| 26 | + code: 'override.ignore_previous', |
| 27 | + message: 'Looks like an attempt to override existing instructions.', |
| 28 | + score: 0.44, |
| 29 | + regex: |
| 30 | + /(ignore|disregard|forget|bypass)\s+(all\s+)?(previous|prior|above|system)\s+(instructions|rules|constraints|prompts?)/i, |
| 31 | + }, |
| 32 | + { |
| 33 | + code: 'override.role_hijack', |
| 34 | + message: 'Looks like a role or policy hijack attempt.', |
| 35 | + score: 0.3, |
| 36 | + regex: /(you\s+are\s+now|act\s+as|developer\s+mode|jailbreak|unrestricted\s+mode|dan)/i, |
| 37 | + }, |
| 38 | + { |
| 39 | + code: 'exfiltrate.system_prompt', |
| 40 | + message: 'Looks like a request to reveal hidden prompts/instructions.', |
| 41 | + score: 0.42, |
| 42 | + regex: |
| 43 | + /(reveal|show|print|dump|leak|display)\s+((the|your)\s+)?(system|developer|hidden)\s+(prompt|instructions|rules|message)/i, |
| 44 | + }, |
| 45 | + { |
| 46 | + code: 'exfiltrate.secrets', |
| 47 | + message: 'Looks like a request for sensitive credentials.', |
| 48 | + score: 0.42, |
| 49 | + regex: |
| 50 | + /(api\s*key|secret|token|password|private\s+key|credentials?|session\s+cookie|jwt|bearer)/i, |
| 51 | + }, |
| 52 | +]; |
| 53 | + |
| 54 | +function normalize(input: string): { |
| 55 | + lowered: string; |
| 56 | + collapsed: string; |
| 57 | + compact: string; |
| 58 | + hasInstructionOverride: boolean; |
| 59 | + hasExfiltrationIntent: boolean; |
| 60 | +} { |
| 61 | + const lowered = input.toLowerCase(); |
| 62 | + const mapped = Array.from(lowered) |
| 63 | + .map(ch => { |
| 64 | + switch (ch) { |
| 65 | + case '0': |
| 66 | + return 'o'; |
| 67 | + case '1': |
| 68 | + return 'i'; |
| 69 | + case '3': |
| 70 | + return 'e'; |
| 71 | + case '4': |
| 72 | + return 'a'; |
| 73 | + case '5': |
| 74 | + return 's'; |
| 75 | + case '7': |
| 76 | + return 't'; |
| 77 | + case '\u200b': |
| 78 | + case '\u200c': |
| 79 | + case '\u200d': |
| 80 | + case '\u2060': |
| 81 | + case '\ufeff': |
| 82 | + return ' '; |
| 83 | + default: |
| 84 | + return /[a-z0-9\s]/i.test(ch) ? ch : ' '; |
| 85 | + } |
| 86 | + }) |
| 87 | + .join(''); |
| 88 | + |
| 89 | + const collapsed = mapped.trim().replace(SPACE_RE, ' '); |
| 90 | + const compact = collapsed.replace(/\s/g, ''); |
| 91 | + const hasInstructionOverride = |
| 92 | + collapsed.includes('ignore previous instructions') || |
| 93 | + collapsed.includes('ignore all previous instructions') || |
| 94 | + compact.includes('ignoreallpreviousinstructions') || |
| 95 | + compact.includes('ignorepreviousinstructions'); |
| 96 | + const hasExfiltrationIntent = |
| 97 | + collapsed.includes('system prompt') || |
| 98 | + collapsed.includes('developer instructions') || |
| 99 | + collapsed.includes('hidden prompt') || |
| 100 | + collapsed.includes('reveal'); |
| 101 | + |
| 102 | + return { lowered, collapsed, compact, hasInstructionOverride, hasExfiltrationIntent }; |
| 103 | +} |
| 104 | + |
| 105 | +export function checkPromptInjection(input: string): PromptInjectionCheck { |
| 106 | + const normalized = normalize(input); |
| 107 | + const reasons: PromptInjectionReason[] = []; |
| 108 | + let score = 0; |
| 109 | + |
| 110 | + if (normalized.hasInstructionOverride) { |
| 111 | + score += 0.46; |
| 112 | + reasons.push({ |
| 113 | + code: 'override.obfuscated_instruction', |
| 114 | + message: 'Detected obfuscated instruction-override phrase.', |
| 115 | + }); |
| 116 | + } |
| 117 | + if (normalized.hasExfiltrationIntent) { |
| 118 | + score += 0.24; |
| 119 | + reasons.push({ |
| 120 | + code: 'exfiltration.intent', |
| 121 | + message: 'Detected exfiltration-focused prompt intent.', |
| 122 | + }); |
| 123 | + } |
| 124 | + if (BASE64_RE.test(normalized.lowered)) { |
| 125 | + score += 0.08; |
| 126 | + reasons.push({ |
| 127 | + code: 'obfuscation.base64_like', |
| 128 | + message: 'Contains base64-like obfuscated content.', |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + for (const rule of RULES) { |
| 133 | + if ( |
| 134 | + rule.regex.test(normalized.lowered) || |
| 135 | + rule.regex.test(normalized.collapsed) || |
| 136 | + rule.regex.test(normalized.compact) |
| 137 | + ) { |
| 138 | + score += rule.score; |
| 139 | + reasons.push({ code: rule.code, message: rule.message }); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + score = Math.min(1, score); |
| 144 | + const verdict: PromptInjectionVerdict = |
| 145 | + score >= 0.7 ? 'block' : score >= 0.45 ? 'review' : 'allow'; |
| 146 | + return { verdict, score, reasons }; |
| 147 | +} |
| 148 | + |
| 149 | +export function promptGuardMessage(check: PromptInjectionCheck): string { |
| 150 | + if (check.verdict === 'block') { |
| 151 | + return 'This message looks like a prompt-injection attempt and will likely be blocked by server-side security checks.'; |
| 152 | + } |
| 153 | + if (check.verdict === 'review') { |
| 154 | + return 'This message may be unsafe and could be rejected by server-side security checks. Please rephrase.'; |
| 155 | + } |
| 156 | + return ''; |
| 157 | +} |
0 commit comments