Skip to main content
Prompt injection detection protects your application from generating outputs that contain malicious code or exploit attempts.

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
This is an output rail that validates bot responses before they’re sent to users.

Quick Start

1

Install YARA

Install the yara-python package:
2

Configure injection detection

Specify which injection types to detect and the action to take:
config.yml
3

Enable the output rail

Add the injection detection flow:
config.yml

Configuration

Basic Configuration

config.yml

Available Injection Types

Built-in YARA rules detect:
  • code - Code injection patterns
  • sqli - SQL injection attempts
  • xss - Cross-site scripting
  • template - Template injection
  • command - Command injection
  • And more…
You can specify which types to check:

Actions

Three actions are available: Block the output and inform the user:
When an injection is detected, the bot responds:

2. Omit

Strip detected injection patterns from the output:
Omitting injections may not be completely effective and could still result in malicious activity. Use with caution.

3. Sanitize

Currently not implemented. Will raise NotImplementedError.

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
The directory should contain .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
Raises InjectionDetectionRailException when injection is detected.

Without Rails Exceptions

Behavior depends on the action: Reject:
Then aborts. Omit: Removes the detected patterns and continues with the sanitized output.

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
Not suitable for:
  • 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
Actions:
  • InjectionDetectionAction - Scans text using YARA rules
Internal functions:
  • _reject_injection() - Detect injections
  • _omit_injection() - Remove injection patterns
  • _sanitize_injection() - Not yet implemented

Dependencies

Injection detection requires yara-python to be installed.
Without it, you’ll see:

YARA Rule Examples

Built-in rules are located in:
Example rule structure:

Error Handling

If YARA rules fail to compile:
The action returns no detection and logs the error.

Best Practices

  1. Use reject action - Safest option for security-critical applications
  2. Limit injection types - Only check for relevant exploit types
  3. Test false positives - Legitimate outputs may trigger rules
  4. Custom rules for your domain - Add domain-specific patterns
  5. Monitor detections - Log when injections are found
  6. 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

See Also