/** * Test script for prompt injection protection * * Run with: node scripts/test-prompt-injection.mjs */ // Inline the validation logic for testing function validatePrompt(prompt) { const INJECTION_PATTERNS = [ /ignore\s+(previous|above|all|prior)\s+(instructions?|prompts?|commands?)/gi, /ignore\s+all/gi, /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, /you\s+are\s+now/gi, /pretend\s+to\s+be/gi, /simulate\s+being/gi, /roleplay\s+as/gi, /show\s+me\s+(your|the)\s+(system|internal|hidden)/gi, /your\s+(system|internal|hidden)\s+prompt/gi, /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, /execute\s+code/gi, /run\s+command/gi, /shell\s+command/gi, /DAN\s+mode/gi, /developer\s+mode/gi, /admin\s+mode/gi, /sudo\s+mode/gi, /root\s+access/gi, /repeat\s+(the\s+)?above/gi, /what\s+was\s+your\s+(first|initial|original)/gi, /before\s+this\s+conversation/gi, ]; const SUSPICIOUS_SEQUENCES = [ / How much should baby eat?', shouldPass: false, expectedRiskLevel: 'high', }, { name: 'JavaScript protocol', prompt: 'javascript:alert(1) How to feed baby?', shouldPass: false, expectedRiskLevel: 'high', }, // Special character attacks { name: 'Excessive special characters', prompt: '!!@@##$$%%^^&&**(())__++==[[]]{{}}||\\\\//<<>>??', shouldPass: false, expectedRiskLevel: 'medium', }, // Edge cases { name: 'Empty string', prompt: '', shouldPass: false, expectedRiskLevel: 'low', }, ]; function runTests() { console.log('๐Ÿงช Testing Prompt Injection Protection\n'); console.log('='.repeat(60)); let passed = 0; let failed = 0; for (const testCase of testCases) { const result = validatePrompt(testCase.prompt); const actuallyPassed = result.isValid; const testPassed = actuallyPassed === testCase.shouldPass && (!testCase.expectedRiskLevel || result.riskLevel === testCase.expectedRiskLevel); if (testPassed) { passed++; console.log(`โœ… PASS: ${testCase.name}`); } else { failed++; console.log(`โŒ FAIL: ${testCase.name}`); console.log(` Expected: ${testCase.shouldPass ? 'valid' : 'invalid'} (${testCase.expectedRiskLevel || 'any'})`); console.log(` Got: ${actuallyPassed ? 'valid' : 'invalid'} (${result.riskLevel})`); if (result.reason) { console.log(` Reason: ${result.reason}`); } } } console.log('='.repeat(60)); console.log(`\n๐Ÿ“Š Results: ${passed} passed, ${failed} failed out of ${testCases.length} tests`); if (failed === 0) { console.log('๐ŸŽ‰ All tests passed!\n'); } else { console.log(`โš ๏ธ ${failed} test(s) failed.\n`); process.exit(1); } } runTests();