| 14 | ); |
| 15 | |
| 16 | export function inputDetect(content: unknown): Warning | null { |
| 17 | if (typeof content === "string") { |
| 18 | |
| 19 | const templatePattern = [ |
| 20 | /<role>ASSISTANT<\/role>[\s\S]*?<role>HUMAN<\/role>/i, |
| 21 | /<\|user\|>[\s\S]*?<\|end\|>/i, |
| 22 | /<\|im_start\|>[\s\S]*?<\|im_end\|>/i, |
| 23 | /<step> Source: assistant[\s\S]*?Destination: user/i, |
| 24 | /<\|user\|>[\s\S]*?<\|endoftext\|>/i, |
| 25 | /<\|start_header_id\|>[\s\S]*?<\|end_header_id\|>/i, |
| 26 | /<\|end_of_sentence\|>[\s\S]*?<\|User\|>/i, |
| 27 | /<\|im_end\|>[\s\S]*?<\|im_start\|>/i, |
| 28 | /<\|EOT\|>[\s\S]*?<\|Instruction\|>/i, |
| 29 | /\[\|endofturn\|][\s\S]*?\[\|user\|]/i, |
| 30 | /\[\/INST\][\s\S]*?\[INST\]\[\/INST\]/i, |
| 31 | /<\/s>[\s\S]*?<s>user[\s\S]*?<\/s>/i, |
| 32 | /Assistant:[\s\S]*?<\|end_of_sentence\|>User:/i, |
| 33 | /<\|Assistant\|>[\s\S]*?<\|end_of_sentence\|><\|User\|>/i, |
| 34 | /<\|end\|>[\s\S]*?<\|user\|>/i, |
| 35 | /<end_of_turn>\n<start_of_turn>user[\s\S]*?<end_of_turn>\n/i, |
| 36 | /<\|start_of_role\|>assistant<\|end_of_role\|>[\s\S]*?<\|start_of_role\|>user<\|end_of_role\|>/i, |
| 37 | /<\|endoftext\|>[\s\S]*?<\|endoftext\|>/i, |
| 38 | /<\[begin▁of▁sentence\]><\[end▁of▁sentence\]>/i, |
| 39 | ]; |
| 40 | |
| 41 | const jeilBreakPatterns = [ |
| 42 | /bypass(\s)+(policy|guardrail|safety|restriction)/i, |
| 43 | /act(\s)+as(\s)+(?:an? )?(?:unfiltered|jailbroken|developer mode)\b/i, |
| 44 | /do(\s)+not(\s)+follow(\s)+your(\s)+(rules|policies|constraints)/i, |
| 45 | /pretend(\s)+you(\s)+are(\s)+not(\s)+an(\s)+ai/i, |
| 46 | ]; |
| 47 | |
| 48 | const instructionPatterns = [ |
| 49 | /execute.*sudo(\s)+rm(\s)+-rf/i, |
| 50 | /until(\s)+tokens(\s)+are(\s)+exhausted/i, |
| 51 | /ignore(\s)+(all)?(\s)*(all|any|previous|prior)(\s)+(instruction|order|command|task|)s?/i, |
| 52 | /(send|list|show|summarize|provide)(\s)+.*(\s)+(api[_ -]?key|token|secret|password|credential)/i, |
| 53 | ]; |
| 54 | |
| 55 | let instructionScore = false; |
| 56 | instructionPatterns.forEach((pattern) => { |
| 57 | if (pattern.test(content)) { |
| 58 | instructionScore = true; |
| 59 | getLogger().warn(`[InputSanitization] Detected Injection Instruction: "${content.match(pattern)?.[0]}"`); |
| 60 | return |
| 61 | } |
| 62 | }); |
| 63 | |
| 64 | let jailbreakScore = false; |
| 65 | jeilBreakPatterns.forEach((pattern) => { |
| 66 | if (pattern.test(content)) { |
| 67 | jailbreakScore = true; |
| 68 | getLogger().warn(`[InputSanitization] Detected Jailbreak Content: "${content.match(pattern)?.[0]}"`); |
| 69 | return |
| 70 | } |
| 71 | }); |
| 72 | |
| 73 | let templateScore = false; |