You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guardrails/basics.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,7 +80,7 @@ raise "Found pattern" if:
80
80
# actual rule logic
81
81
```
82
82
83
-
The rule logic above, will be applied to every `Message`, `ToolCall`, and `ToolOutput` object encountered during operation, enabling you to easily check your agents for bad behaviors.
83
+
The rule logic above will be applied to every `Message`, `ToolCall`, and `ToolOutput` object encountered during operation, enabling you to easily check your agents for bad behaviors.
Copy file name to clipboardExpand all lines: docs/guardrails/code-validation.md
+52-64Lines changed: 52 additions & 64 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,20 +14,59 @@ Code validation is a critical component of any code-generating LLM system, as it
14
14
!!! danger "Code Validation Risks"
15
15
Code validation is a critical component of any code-generating LLM system. An insecure agent could:
16
16
17
-
- Generate code that contains **security vulnerabilities**, such as SQL injection or cross-site scripting
18
-
- Generate code that **contains bugs or errors**, causing the system to crash or behave unexpectedly
19
-
- Produce code that escapes a **sandboxed execution environment**
20
-
- Generate code that is **not well-formed or does not follow best practices**, causing the system to be difficult to maintain or understand
17
+
- Generate code that contains **security vulnerabilities**, such as SQL injection or cross-site scripting.
18
+
- Generate code that **contains bugs or errors**, causing the system to crash or behave unexpectedly.
19
+
- Produce code that escapes a **sandboxed execution environment**.
20
+
- Generate code that is **not well-formed or does not follow best practices**, causing the system to be difficult to maintain or understand.
21
21
22
-
To validate code as part of Guardrails, Invariant allows you to invoke external codechecking tools as part of the guardrailing process. That means with Invariant you can build code validation right into your LLM layer, without worrying about it on the agent side.
22
+
To validate code as part of Guardrails, Invariant allows you to invoke external code-checking tools as part of the guardrailing process. That means with Invariant you can build code validation right into your LLM layer, without worrying about it on the agent side.
23
23
24
24
For this, two main components are supported: (1) code parsing and (2) semgrep integration.
25
25
26
-
## Code Parsing
26
+
## Code Parsing
27
27
28
28
The code parsing feature allows you to parse generated code, and access its abstract syntax tree, to implement custom validation rules.
29
29
30
-
This is useful for checking the structure and syntax of the code, as well as for identifying potential security vulnerabilities.
30
+
This is useful for checking the structure and syntax of the code and identifying potential security vulnerabilities. Invariant provides the `python_code` function for this.
|`data`|`str|list|dict`| The Python code to be parsed. |
47
+
|`ipython_mode`|`bool`| If set to <span class='boolean-value-true'>TRUE</span>, the code will be parsed in IPython mode. This is useful for parsing code that uses IPython-specific features or syntax. |
|`.imports`|`list[str]`| This field contains a list of imported modules in the provided code. It is useful for identifying which libraries or modules are being used in the code. |
62
+
|`.builtins`|`list[str]`| A list of built-in functions used in the provided code. |
63
+
|`.syntax_error`|`bool`| A boolean flag indicating whether the provided code has syntax errors. |
64
+
|`.syntax_error_exception`|`str|None`| A string containing the exception message if a syntax error occurred while parsing the provided code. |
65
+
|`.function_calls`|`set[str]`| A set of function call identifier names in the provided code. |
66
+
67
+
### Example Usage
68
+
69
+
The `eval` function in Python presents several potential security vulnerabilities, so you may want to prevent it from being present in generated code.
31
70
32
71
**Example:** Validating the function calls in a code snippet.
33
72
```guardrail
@@ -56,12 +95,12 @@ Similarly, you can check for syntactic errors in the code, or check for the pres
56
95
57
96
**Example:** Validating the imports in a code snippet.
| `data` | `Union[str, List[str]]` | A single message or a list of messages to detect PII in. |
97
-
| `entities` | `Optional[List[str]]` | A list of [PII entity types](https://microsoft.github.io/presidio/supported_entities/) to detect. Defaults to detecting all types. |
|`data`|`str|list|dict`| The Python code to be parsed. |
120
-
|`ipython_mode`|`bool`| If set to <span class='boolean-value-true'>TRUE</span>, the code will be parsed in IPython mode. This is useful for parsing code that uses IPython-specific features or syntax. |
|`.imports`|`list[str]`| This field contains a list of imported modules in the provided code. It is useful for identifying which libraries or modules are being used in the code. |
135
-
|`.builtins`|`list[str]`| A list of built-in functions used in the provided code. |
136
-
|`.syntax_error`|`bool`| A boolean flag indicating whether the provided code has syntax errors. |
137
-
|`.syntax_error_exception`|`str|None`| A string containing the exception message if a syntax error occurred while parsing the provided code. |
138
-
|`.function_calls`|`set[str]`| A set of function call identifier names in the provided code. |
139
-
140
130
## Static Code Analysis
141
131
142
132
Static code analysis allows for powerful pattern-based detection of vulnerabilities and insecure coding practices. Invariant integrates [Semgrep](https://semgrep.dev) directly into your guardrails, enabling deep analysis of assistant-generated code before it's executed.
@@ -151,7 +141,7 @@ Static code analysis allows for powerful pattern-based detection of vulnerabilit
151
141
152
142
You can use `semgrep` within a guardrail to scan code in Python, Bash, and other supported languages.
Copy file name to clipboardExpand all lines: docs/guardrails/dataflow-rules.md
+12-13Lines changed: 12 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,28 +19,28 @@ For instance, your agent may access an internal source of information like a dat
19
19
20
20
Invariant allows you to detect such contextually sensitive dataflow, and prevent it from happening.
21
21
22
-
This chapter discusses how Invariant Guardrails can be used to secure agentic dataflow, and make sure that sensitive data never leaves the system through unintended channels.
22
+
This chapter discusses how Invariant Guardrails can be used to secure agentic dataflow and make sure that sensitive data never leaves the system through unintended channels.
23
23
24
24
<divclass='risks'/>
25
25
> **Dataflow Risks**<br/>
26
26
27
27
> Due to their dynamic nature, agentic systems often mix and combine data from different sources, and can easily leak sensitive information. For example, an insecure agent could:
28
28
29
-
> * Leak sensitive information, such as **API keys or passwords**, to an external service
29
+
> * Leak sensitive information, such as **API keys or passwords**, to an external service.
30
30
31
-
> * Send sensitive information, such as **user data or PII**, to an external service
31
+
> * Send sensitive information, such as **user data or PII**, to an external service.
32
32
33
-
> * Be prompt-injected by an external service via indirect channels, to **perform malicious actions** as injected by an potential attacker
33
+
> * Be prompt-injected by an external service via indirect channels, to **perform malicious actions** as injected by a potential attacker.
At the center of Invariant's data flow checking is the flow operator `->`. This operator enables you to precisely detect flows and ordering of operations in an agent trace.
39
+
At the center of Invariant's data flow checking is the flow operator `->`. This operator enables you to precisely detect flows and the ordering of operations in an agent trace.
40
40
41
-
For example, to prevent a user message with the content `"send"` from triggering a `send_email` tool call, you can use the following rule:
41
+
For example, to prevent a user message with the content `"send"` from triggering a `send_email` tool call, you can use the following rule.
42
42
43
-
**Example:** Preventing a simple flow
43
+
**Example:** Preventing a simple flow.
44
44
```guardrail
45
45
raise "Must not call tool after user uses keyword" if:
46
46
(msg: Message) -> (tool: ToolCall)
@@ -66,11 +66,12 @@ raise "Must not call tool after user uses keyword" if:
66
66
}
67
67
]
68
68
```
69
-
Evaluating this rule will highlight both, the relevant part of the user message, as well as the subsequent `send_email` call:
69
+
70
+
Evaluating this rule will highlight both the relevant part of the user message and the subsequent `send_email` call:
This rule will raise an error on the given trace, because a user message with the content `"send"` is followed by a `send_email` tool call, and thus makes it impossible to send an email after the user uses the keyword `"send"`.
74
+
This rule will raise an error on the given trace because a user message with the content `"send"` is followed by a `send_email` tool call, and thus makes it impossible to send an email after the user uses the keyword `"send"`.
74
75
75
76
Here, the line `(msg: Message) -> (tool: ToolCall)` specifies that the rule only applies, when a `Message` is followed by a `ToolCall`, where `msg` and `tool` are further constrained by the extra conditions in the following lines.
76
77
@@ -119,9 +120,9 @@ raise "Must not call tool after user uses keyword" if:
119
120
]
120
121
```
121
122
122
-
Note that for this you have to use the `->` operator twice, in separate lines, to express the transitive connection between `msg`, `tool` and `tool2`.
123
+
Note that for this you have to use the `->` operator twice, in separate lines, to express the transitive connection between `msg`, `tool`, and `tool2`.
0 commit comments