> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/guardrails/llms.txt.
> For full documentation content, see https://docs.nvidia.com/nemo/guardrails/llms-full.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/guardrails/_mcp/server.

# nemoguardrails.library.injection_detection.actions

## Module Contents

### Classes

| Name                                                                                                       | Description |
| ---------------------------------------------------------------------------------------------------------- | ----------- |
| [`InjectionDetectionResult`](#nemoguardrails-library-injection_detection-actions-InjectionDetectionResult) | -           |

### Functions

| Name                                                                                                           | Description                                                                  |
| -------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| [`_check_yara_available`](#nemoguardrails-library-injection_detection-actions-_check_yara_available)           | -                                                                            |
| [`_extract_injection_config`](#nemoguardrails-library-injection_detection-actions-_extract_injection_config)   | Extracts and processes the injection detection configuration values.         |
| [`_load_rules`](#nemoguardrails-library-injection_detection-actions-_load_rules)                               | Loads and compiles YARA rules from either file paths or direct rule strings. |
| [`_omit_injection`](#nemoguardrails-library-injection_detection-actions-_omit_injection)                       | Attempts to strip the offending injection attempts from the provided text.   |
| [`_reject_injection`](#nemoguardrails-library-injection_detection-actions-_reject_injection)                   | Detects whether the provided text contains potential injection attempts.     |
| [`_sanitize_injection`](#nemoguardrails-library-injection_detection-actions-_sanitize_injection)               | Attempts to sanitize the offending injection attempts in the provided text.  |
| [`_validate_injection_config`](#nemoguardrails-library-injection_detection-actions-_validate_injection_config) | Validates the injection detection configuration.                             |
| [`injection_detection`](#nemoguardrails-library-injection_detection-actions-injection_detection)               | Detects and mitigates potential injection attempts in the provided text.     |

### Data

[`YARA_DIR`](#nemoguardrails-library-injection_detection-actions-YARA_DIR)

[`log`](#nemoguardrails-library-injection_detection-actions-log)

### API

```python
class nemoguardrails.library.injection_detection.actions.InjectionDetectionResult
```

**Bases:** `typing.TypedDict`

```python
nemoguardrails.library.injection_detection.actions._check_yara_available()
```

```python
nemoguardrails.library.injection_detection.actions._extract_injection_config(
    config: nemoguardrails.RailsConfig
) -> typing.Tuple[str, pathlib.Path, typing.Tuple[str], typing.Optional[typing.Dict[str, str]]]
```

Extracts and processes the injection detection configuration values.

**Parameters:**

The Rails configuration object containing injection detection settings.

**Returns:** `str`

Tuple\[str, Path, Tuple\[str], Optional\[Dict\[str, str]]]: A tuple containing the action option,

**Raises:**

* `ValueError`: If the injection rules contain invalid elements.

```python
nemoguardrails.library.injection_detection.actions._load_rules(
    yara_path: pathlib.Path,
    rule_names: typing.Tuple,
    yara_rules: typing.Optional[typing.Dict[str, str]] = None
) -> typing.Union[yara.Rules, None]
```

Loads and compiles YARA rules from either file paths or direct rule strings.

**Parameters:**

The path to the directory containing YARA rule files.

A tuple of YARA rule names to load.

Dictionary mapping rule names to YARA rule strings.

**Returns:** `Union[yara.Rules, None]`

Union\['yara.Rules', None]: The compiled YARA rules object if successful,

**Raises:**

* `yara.SyntaxError`: If there is a syntax error in the YARA rules.
* `ImportError`: If the yara module is not installed.

```python
nemoguardrails.library.injection_detection.actions._omit_injection(
    text: str,
    matches: list[yara.Match]
) -> typing.Tuple[bool, str]
```

Attempts to strip the offending injection attempts from the provided text.

**Parameters:**

The text to check for command injection.

A list of YARA rule matches.

**Returns:** `Tuple[bool, str]`

Tuple\[bool, str]: A tuple containing:

* bool: True if injection was detected and modified,
  False if the text is safe (i.e., not modified).
* str: The text, with detected injections stripped out if modified.

**Raises:**

* `ImportError`: If the yara module is not installed.

```python
nemoguardrails.library.injection_detection.actions._reject_injection(
    text: str,
    rules: yara.Rules
) -> typing.Tuple[bool, typing.List[str]]
```

Detects whether the provided text contains potential injection attempts.

This function is recommended as an output or execution guardrail. It loads
all relevant YARA rules and compiles them according to the provided configuration.

**Parameters:**

The text to check for command injection.

The loaded YARA rules.

**Returns:** `Tuple[bool, List[str]]`

Tuple\[bool, List\[str]]: A tuple containing:

* bool: True if attempted exploitation is detected, False otherwise.
* List\[str]: List of matched rule names.

**Raises:**

* `ValueError`: If the `action` parameter in the configuration is invalid.
* `ImportError`: If the yara module is not installed.

```python
nemoguardrails.library.injection_detection.actions._sanitize_injection(
    text: str,
    matches: list[yara.Match]
) -> typing.Tuple[bool, str]
```

Attempts to sanitize the offending injection attempts in the provided text.
This is done by 'de-fanging' the offending content, transforming it into a state that will not execute
downstream commands.

**Parameters:**

The text to check for command injection.

A list of YARA rule matches.

**Returns:** `Tuple[bool, str]`

Tuple\[bool, str]: A tuple containing:

* bool: True if injection was detected, False otherwise.
* str: The sanitized text, or original text depending on sanitization outcome.
  Currently, this function will always raise NotImplementedError.

**Raises:**

* `NotImplementedError`: If the sanitization logic is not implemented.
* `ImportError`: If the yara module is not installed.

```python
nemoguardrails.library.injection_detection.actions._validate_injection_config(
    config: nemoguardrails.RailsConfig
) -> None
```

Validates the injection detection configuration.

**Parameters:**

The Rails configuration object containing injection detection settings.

**Raises:**

* `ValueError`: If the configuration is missing or invalid.
* `FileNotFoundError`: If the provided `yara_path` is not a directory.

```python
nemoguardrails.library.injection_detection.actions.injection_detection(
    text: str,
    config: nemoguardrails.RailsConfig
) -> nemoguardrails.library.injection_detection.actions.InjectionDetectionResult
```

async

Detects and mitigates potential injection attempts in the provided text.

Depending on the configuration, this function can omit or sanitize the detected
injection attempts. If the action is set to "reject", it delegates to the
`reject_injection` function.

**Parameters:**

The text to check for command injection.

The Rails configuration object containing injection detection settings.

**Returns:** `InjectionDetectionResult`

A TypedDict containing:

* is\_injection (bool): Whether an injection was detected. True if any injection is detected,
  False if no injection is detected.
* text (str): The sanitized or original text
* detections (List\[str]): List of matched rule names if any injection is detected

**Raises:**

* `ValueError`: If the `action` parameter in the configuration is invalid.
* `NotImplementedError`: If an unsupported action is encountered.
* `ImportError`: If the yara module is not installed.

```python
nemoguardrails.library.injection_detection.actions.YARA_DIR = Path(__file__).resolve().parent.joinpath('yara_rules')
```

```python
nemoguardrails.library.injection_detection.actions.log = logging.getLogger(__name__)
```