/** * Prompt Injection Protection * * Detects and prevents malicious prompt injection attempts in AI inputs * to protect against system prompt manipulation, data exfiltration, and * jailbreaking attempts. */ export interface PromptValidationResult { isValid: boolean; reason?: string; sanitizedPrompt?: string; riskLevel: 'low' | 'medium' | 'high'; } /** * Common prompt injection patterns to detect */ const INJECTION_PATTERNS = [ // System prompt manipulation /ignore\s+(previous|above|all|prior)\s+(instructions?|prompts?|commands?)/gi, /ignore\s+all/gi, // Catch "ignore all" /disregard\s+(previous|above|all)\s+(instructions?|prompts?|commands?)/gi, /forget\s+(previous|above|all)\s+(instructions?|prompts?|commands?)/gi, /new\s+instructions?:/gi, /system\s+prompt/gi, // Catch "system prompt" anywhere /you\s+are\s+now/gi, /act\s+as\s+a\s+(?!parent|caregiver)/gi, // Allow parenting roles only // Role manipulation /pretend\s+to\s+be/gi, /simulate\s+being/gi, /roleplay\s+as/gi, // Data exfiltration attempts /show\s+me\s+(your|the)\s+(system|internal|hidden)/gi, // Catch "show me your system/internal/hidden" /your\s+(system|internal|hidden)\s+prompt/gi, // Catch "your system/internal prompt" /what\s+(is|are)\s+your\s+(instructions?|rules?|guidelines?)/gi, /reveal\s+your\s+(system|internal|hidden)/gi, /list\s+all\s+(users?|children|families)/gi, /show\s+all\s+data/gi, // Command injection /execute\s+code/gi, /run\s+command/gi, /shell\s+command/gi, // Jailbreak attempts /DAN\s+mode/gi, // "Do Anything Now" /developer\s+mode/gi, /admin\s+mode/gi, /sudo\s+mode/gi, /root\s+access/gi, // Prompt leaking /repeat\s+(the\s+)?above/gi, /what\s+was\s+your\s+(first|initial|original)/gi, /before\s+this\s+conversation/gi, ]; /** * Suspicious character sequences that may indicate encoding attacks */ const SUSPICIOUS_SEQUENCES = [ /\u0000/g, // Null bytes /[\u200B-\u200D\uFEFF]/g, // Zero-width characters /[\u2060-\u2069]/g, // Invisible formatting characters /