feat: add code injection detection to guardrails library - #1091
Conversation
Documentation preview |
|
@erickgalinkin currently we don't have yara as extra dependency. And as the tests depend on it they need to get included in dev group too. If you don't mind, I'll push the updated pyrpoject.toml and poetry.lock |
Thanks @Pouyanpi ! |
|
Python 3.9 complains about [structural pattern matching]( job details: https://github.com/NVIDIA/NeMo-Guardrails/blob/feature/code-injection-detection/nemoguardrails/library/injection_detection/actions.py#L254-L263) (https://github.com/NVIDIA/NeMo-Guardrails/actions/runs/14243111294/job/39917918776?pr=1091#step:15:33) I like it but we should wait until October 2025 |
|
I saw that -- will adjust to support 3.9 and push a fix shortly, thanks! |
|
@erickgalinkin I'm enjoying every bits of this PR and am learning, it is great 🚀 Thank you! If you don't mind I'm going to add more tests while I'm learning about this new feature. If some changes were required I'll mention you in the comments. |
|
@erickgalinkin , am I doing something wrong? from nemoguardrails import RailsConfig, LLMRails
messages = [
{"role": "user", "content": "what can you do"},
{
"role": "assistant",
"content": "This is a SELECT * FROM users; -- malicious comment in the middle of text",
},
]
config = RailsConfig.from_content(
yaml_content="""
models: []
rails:
config:
injection_detection:
injections:
- sqli
action:
omit
output:
flows:
- mitigate injection
"""
)
rails = LLMRails(config, verbose=False)
response = rails.generate(
messages=messages,
options={
"rails": ["output"],
"log": {
"activated_rails": True,
"llm_calls": False,
"internal_events": False,
"colang_history": False,
},
},
)I get: File "nemoguardrails/library/injection_detection/actions.py", line 166, in omit_injection
for instance in match_string:
TypeError: 'yara.StringMatch' object is not iterable |
2615e0e to
47cec4d
Compare
There was a problem hiding this comment.
Looks good! Left some comments on organizing the strings into Enums and returning a more helpful response with debugging information when the yara rules are triggered.
I'm also curious if there's a dataset out there with examples of the injection types we want to detect, and safe prompts which we shouldn't flag. Then we can evaluate the coverage we get from the yara rules>
27df406 to
9d1033b
Compare
|
Thank you @erickgalinkin! FYI, I just made following changes:
for the next PRs just make sure that you have pre-commits installed 👍🏻 |
|
Looks like this is good to merge if you're ok with it @tgasser-nv. Any outstanding questions on your side? |
tgasser-nv
left a comment
There was a problem hiding this comment.
LGTM, thanks for making the updates!
Signed-off-by: Erick Galinkin <egalinkin@nvidia.com>
Signed-off-by: Erick Galinkin <egalinkin@nvidia.com>
Signed-off-by: Erick Galinkin <egalinkin@nvidia.com>
Co-authored-by: Pouyan <13303554+Pouyanpi@users.noreply.github.com> Signed-off-by: Erick Galinkin <erick.galinkin@gmail.com>
…ssage formatting. Remove `is_system_action` from `action` decorator. Signed-off-by: Erick Galinkin <egalinkin@nvidia.com>
…hat handles the action_option. Fixed tests, docs, and flows to reflect the change. Updated rejection return message to indicate what was blocked to the user. Signed-off-by: Erick Galinkin <egalinkin@nvidia.com>
9d1033b to
c641b37
Compare
Description
Add (sql, code, template, xss) injection detection support
Checklist