Agentic Security
Agentic security provides specialized guardrails for LLM-based agents that use tools and interact with external systems.
Injection Detection
The NeMo Guardrails library offers detection of potential exploitation attempts by using injection such as code injection, cross-site scripting, SQL injection, and template injection. Injection detection is primarily intended to be used in agentic systems to enhance other security controls as part of a defense-in-depth strategy.
The first part of injection detection is YARA rules. A YARA rule specifies a set of strings (text or binary patterns) to match and a Boolean expression that specifies the logic of the rule. YARA rules are a technology that is familiar to many security teams.
The second part of injection detection is specifying the action to take when a rule is triggered. You can specify to reject the text and return “I’m sorry, the desired output triggered rule(s) designed to mitigate exploitation of {detections}.” Rejecting the output is the safest action and most appropriate for production deployments. As an alternative to rejecting the output, you can specify to omit the triggering text from the response.
About the Default Rules
By default, the NeMo Guardrails library provides the following rules:
- Code injection (Python): Recommended if the LLM output is used as an argument to downstream functions or passed to a code interpreter.
- SQL injection: Recommended if the LLM output is used as part of a SQL query to a database.
- Template injection (Jinja): Recommended for use if LLM output is rendered using the Jinja templating language. This rule is usually paired with code injection rules.
- Cross-site scripting (Markdown and Javascript): Recommended if the LLM output is rendered directly in HTML or Markdown.
You can view the default rules in the yara_rules directory of the GitHub repository.
Configuring Injection Detection
To activate injection detection, you must specify the rules to apply and the action to take as well as include the injection detection output flow.
As an example config:
Refer to the following table for the rails.config.injection_detection field syntax reference:
If specified, these inline rules override the rules found in the yara_path field.
- None
-
Create a configuration directory, such as
config, and add aconfig.ymlfile with contents like the following:config.yml -
Load the guardrails configuration:
demo.py -
Send a possibly unsafe request:
demo.pyExample Output
demo-out.txt
Context Bloat Detection
Context bloat detection guards against context-manipulation attacks. Attacker-controlled content can be padded, oversized, or repetitively structured to push the system prompt out of the model’s attention or exhaust the token budget. The rail applies to user input and retrieved knowledge-base chunks, the two sources of text an agent does not author.
The rail is local and model-free. It does not call an external service and measures text against four thresholds:
Texts shorter than min_chars (default 50) are checked against the size cap only, because entropy and repetition are not meaningful on a short string.
Configuring Context Bloat Detection
Add the flow for each direction you want to guard, and set the thresholds under rails.config.context_bloat_detection.
All configuration fields are optional.
The action field selects what happens when a check detects a violation:
With action: truncate, the rail rewrites the text it checked rather than blocking it.
This rewrite affects how the engines schedule the rail.
Refer to Rail Engine Support for details.
Engine Support
context bloat detection on input runs on both LLMRails and IORails.
context bloat detection on retrieval runs on LLMRails only, because IORails has no retrieval pipeline.
A configuration that declares a rails.retrieval section routes to LLMRails by default.
With require_iorails=True, Guardrails raises ValueError instead of falling back.