Prompt injection detection protects your application from generating outputs that contain malicious code or exploit attempts.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/NVIDIA-NeMo/Guardrails/llms.txt
Use this file to discover all available pages before exploring further.
Overview
The injection detection guardrail uses YARA rules to scan bot outputs for:- Code injection attempts
- SQL injection (SQLi)
- Cross-site scripting (XSS)
- Template injection
- Command injection
- Other exploit patterns
Quick Start
Configure injection detection
Specify which injection types to detect and the action to take:
config.yml
Configuration
Basic Configuration
config.yml
Available Injection Types
Built-in YARA rules detect:code- Code injection patternssqli- SQL injection attemptsxss- Cross-site scriptingtemplate- Template injectioncommand- Command injection- And more…
Actions
Three actions are available:1. Reject (Recommended)
Block the output and inform the user:2. Omit
Strip detected injection patterns from the output:3. Sanitize
Custom YARA Rules
Provide your own YARA rules instead of using the built-in ones:config.yml
Custom YARA Path
Use YARA rules from a custom directory:config.yml
.yara files named after the injection types:
Behavior
The injection detection flow operates on$bot_message:
flows.co
Response Structure
The action returns:With Rails Exceptions
config.yml
InjectionDetectionRailException when injection is detected.
Without Rails Exceptions
Behavior depends on the action: Reject:Custom Flows
Create custom injection handling:flows.co
Accessing Detection Results
The detection results are available in the flow:flows.co
Use Cases
Good use cases:- Protecting against LLM-generated exploits
- Validating code generation outputs
- Scanning AI-generated SQL queries
- Checking templated responses
- Preventing XSS in web-facing bots
- Input validation (this is an output rail)
- Legitimate code examples (may trigger false positives)
- Technical documentation containing code
Performance Considerations
YARA rule matching is generally fast, but:- More rules = slightly longer processing
- Complex regex patterns may slow down matching
- Consider limiting injection types to only those needed
Implementation Details
The injection detection flows are defined in:/nemoguardrails/library/injection_detection/flows.co/nemoguardrails/library/injection_detection/actions.py/nemoguardrails/library/injection_detection/yara_rules/- Built-in YARA rules
InjectionDetectionAction- Scans text using YARA rules
_reject_injection()- Detect injections_omit_injection()- Remove injection patterns_sanitize_injection()- Not yet implemented
Dependencies
YARA Rule Examples
Built-in rules are located in:Error Handling
If YARA rules fail to compile:Best Practices
- Use reject action - Safest option for security-critical applications
- Limit injection types - Only check for relevant exploit types
- Test false positives - Legitimate outputs may trigger rules
- Custom rules for your domain - Add domain-specific patterns
- Monitor detections - Log when injections are found
- Combine with other rails - Use alongside content safety and jailbreak detection
Limitations
- Only scans bot outputs (not user inputs)
- May have false positives on legitimate technical content
- Omit action is not guaranteed to be effective
- Sanitize action not yet implemented
- Only supports text-based injection detection