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/code-validation.md
+236-2Lines changed: 236 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,11 +4,11 @@
4
4
Secure the code that your agent generates and executes.
5
5
</div>
6
6
7
-
Code validation is a critical component of any code-generating LLM system, as it helps to ensure that the code generated by the LLM is safe and secure. Guardrails provides a simple way to validate the code generated by your LLM, using a set of pre-defined rules and checks.
7
+
Code validation is a critical component of any code-generating LLM system, as it helps to ensure that the code generated by the LLM is safe and secure. Guardrails provides a simple way to validate the code generated by your LLM, using a set of integration and code parsing capabilities.
8
8
9
9
<divclass='risks'/>
10
10
> **Code Validation Risks**<br/>
11
-
> Code validation is a critical component of any code-generating LLM system. For example, an insecure agent could:
11
+
> Code validation is a critical component of any code-generating LLM system. An insecure agent could:
12
12
13
13
> * Generate code that contains **security vulnerabilities**, such as SQL injection or cross-site scripting
14
14
@@ -17,3 +17,237 @@ Code validation is a critical component of any code-generating LLM system, as it
17
17
> * Produce code that escapes a **sandboxed execution environment**
18
18
19
19
> * Generate code that is **not well-formed or does not follow best practices**, causing the system to be difficult to maintain or understand
20
+
21
+
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.
22
+
23
+
For this, two main components are supported: (1) code parsing and (2) semgrep integration.
24
+
25
+
## Code Parsing
26
+
27
+
The code parsing feature allows you to parse generated code, and access its abstract syntax tree, to implement custom validation rules.
28
+
29
+
This is useful for checking the structure and syntax of the code, as well as for identifying potential security vulnerabilities.
30
+
31
+
**Example:** Validating the function calls in a code snippet.
32
+
```guardrail
33
+
from invariant.detectors.code import python_code
34
+
35
+
raise "'eval' function must not be used in generated code" if:
36
+
(msg: Message)
37
+
program := python_code(msg.content)
38
+
"eval" in program.function_calls
39
+
40
+
```
41
+
```example-trace
42
+
[
43
+
{
44
+
"role": "user",
45
+
"content": "Reply to Peter's message"
46
+
},
47
+
{
48
+
"role": "assistant",
49
+
"content": "eval(untrusted_string)"
50
+
}
51
+
]
52
+
```
53
+
54
+
Similarly, you can check for syntactic errors in the code, or check for the presence of certain imports.
55
+
56
+
**Example:** Validating the imports in a code snippet.
### `def python_code(data: str | list | dict, ipython_mode=False)`
92
+
93
+
Parses provided Python code and returns a `PythonDetectorResult` object containing the following fields:
94
+
95
+
**Parameters:**
96
+
97
+
-`data` (str | list | dict): The Python code to be parsed. This can be a string or list of strings, or a dictionary.
98
+
99
+
-`ipython_mode` (bool): If set to `True`, the code will be parsed in IPython mode. This is useful for parsing code that uses IPython-specific features or syntax.
100
+
101
+
102
+
**Returns:**
103
+
104
+
*`PythonDetectorResult.imports`: 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.
105
+
106
+
*`PythonDetectorResult.builtins`: A list of built-in functions used in the provided code.
107
+
108
+
*`PythonDetectorResult.syntax_error`: A boolean flag indicating whether the provided code has syntax errors.
109
+
110
+
*`PythonDetectorResult.syntax_error_exception`: A string containing the exception message if a syntax error occurred while parsing the provided code.
111
+
112
+
*`PythonDetectorResult.function_calls`: A set of function call identifier names in the provided code.
113
+
114
+
### `def ipython_code(data: str | list | dict)`
115
+
116
+
Same as `python_code`, but for [IPython](https://ipython.org/) code. This function is useful for parsing code that uses IPython-specific features or syntax, i.e. code that runs in Jupyter notebook.
117
+
118
+
119
+
## Static Code Analysis
120
+
121
+
Use [`semgrep`](https://semgrep.dev) to perform deep static analysis and identify potential vulnerabilities, bad practices, or policy violations in code. It complements `python_code` by enabling more powerful pattern-based detection.
122
+
123
+
124
+
**Example:** Preventing Dangerous Patterns in Python Code
125
+
126
+
```guardrail
127
+
from invariant.detectors import semgrep
128
+
129
+
raise "Dangerous pattern detected in about-to-be-executed code" if:
- Insecure patterns (e.g. `subprocess` without `shell=False`)
224
+
- Deprecated APIs
225
+
- Style or compliance violations
226
+
227
+
#### 📦 **Best Use**
228
+
Use Semgrep to enforce secure coding practices on any assistant-generated code _before_ execution. -->
229
+
230
+
**Parameters:**
231
+
232
+
-`data`: Code to scan. Can be a `str`, `list`, or `dict`.
233
+
-`lang`: Programming language (e.g., `'python'`, `'javascript'`).
234
+
235
+
**Returns:**
236
+
237
+
A list of `CodeIssue` objects:
238
+
```python
239
+
classCodeIssue(BaseModel):
240
+
description: str
241
+
severity: CodeSeverity # "HIGH", "MEDIUM", or "LOW"
242
+
```
243
+
244
+
Here, `description` is a string describing the issue, and `severity` is an enum indicating the severity level of the issue (e.g., "HIGH", "MEDIUM", or "LOW"). You can use these fields in your guardrails logic to raise exceptions or take other actions based on the detected issues.
0 commit comments