Skip to content

feat: add code injection detection to guardrails library - #1091

Merged
Pouyanpi merged 19 commits into
developfrom
feature/code-injection-detection
Apr 22, 2025
Merged

feat: add code injection detection to guardrails library#1091
Pouyanpi merged 19 commits into
developfrom
feature/code-injection-detection

Conversation

@erickgalinkin

Copy link
Copy Markdown
Collaborator

Description

Add (sql, code, template, xss) injection detection support

Checklist

  • I've read the CONTRIBUTING guidelines.
  • I've updated the documentation if applicable.
  • I've added tests if applicable.

@github-actions

github-actions Bot commented Apr 2, 2025

Copy link
Copy Markdown
Contributor

Documentation preview

https://nvidia.github.io/NeMo-Guardrails/review/pr-1091

@Pouyanpi

Pouyanpi commented Apr 3, 2025

Copy link
Copy Markdown
Collaborator

@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

@erickgalinkin

Copy link
Copy Markdown
Collaborator Author

@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 !

@Pouyanpi

Pouyanpi commented Apr 3, 2025

Copy link
Copy Markdown
Collaborator

@erickgalinkin

Copy link
Copy Markdown
Collaborator Author

I saw that -- will adjust to support 3.9 and push a fix shortly, thanks!

@Pouyanpi Pouyanpi added this to the v0.13.0 milestone Apr 9, 2025
@Pouyanpi Pouyanpi added enhancement New feature or request status: in review labels Apr 9, 2025
@Pouyanpi Pouyanpi modified the milestones: v0.13.0, v0.14.0 Apr 9, 2025
Comment thread docs/user-guides/guardrails-library.md
Comment thread tests/test_configs/injection_detection/test.yara Outdated
Comment thread nemoguardrails/library/injection_detection/actions.py
Comment thread nemoguardrails/library/injection_detection/flows.v1.co Outdated
Comment thread nemoguardrails/library/injection_detection/flows.v1.co Outdated
Comment thread nemoguardrails/library/injection_detection/flows.co Outdated
Comment thread nemoguardrails/library/injection_detection/flows.co Outdated
@Pouyanpi

Pouyanpi commented Apr 9, 2025

Copy link
Copy Markdown
Collaborator

@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.

@Pouyanpi

Pouyanpi commented Apr 9, 2025

Copy link
Copy Markdown
Collaborator

@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

line 166:

@Pouyanpi

Pouyanpi commented Apr 9, 2025

Copy link
Copy Markdown
Collaborator
  File "nemoguardrails/library/injection_detection/actions.py", line 166, in omit_injection
    for instance in match_string:
TypeError: 'yara.StringMatch' object is not iterable

line 166:

resolved with changes in af80ad7, please let me know if I've broken something.

@Pouyanpi
Pouyanpi force-pushed the feature/code-injection-detection branch from 2615e0e to 47cec4d Compare April 10, 2025 14:42
Comment thread docs/user-guides/guardrails-library.md Outdated
Comment thread nemoguardrails/library/injection_detection/actions.py Outdated
Comment thread nemoguardrails/library/injection_detection/actions.py
Comment thread nemoguardrails/library/injection_detection/yara_rules/code.yara
Comment thread tests/test_configs/injection_detection/flows.co
@Pouyanpi Pouyanpi changed the title Code injection detection feat: add code injection detection to guardrails library Apr 10, 2025

@tgasser-nv tgasser-nv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Comment thread docs/user-guides/guardrails-library.md
Comment thread nemoguardrails/library/injection_detection/actions.py Outdated
Comment thread nemoguardrails/library/injection_detection/actions.py Outdated
Comment thread nemoguardrails/library/injection_detection/actions.py Outdated
Comment thread nemoguardrails/library/injection_detection/actions.py
Comment thread nemoguardrails/library/injection_detection/actions.py Outdated
Comment thread docs/user-guides/guardrails-library.md Outdated
Comment thread nemoguardrails/library/injection_detection/actions.py Outdated
@Pouyanpi
Pouyanpi force-pushed the feature/code-injection-detection branch from 27df406 to 9d1033b Compare April 16, 2025 10:06
@Pouyanpi

Copy link
Copy Markdown
Collaborator

Thank you @erickgalinkin!

FYI, I just made following changes:

  • removed the merge commits (we prefer rebase)
  • applied pre-commits
  • update poetry lock to use Poetry 1.8.2 (we don't support >=0.2.0 yet)

for the next PRs just make sure that you have pre-commits installed 👍🏻

@Pouyanpi Pouyanpi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@erickgalinkin

Copy link
Copy Markdown
Collaborator Author

Looks like this is good to merge if you're ok with it @tgasser-nv. Any outstanding questions on your side?

@tgasser-nv tgasser-nv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for making the updates!

erickgalinkin and others added 19 commits April 22, 2025 09:52
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>
@Pouyanpi
Pouyanpi force-pushed the feature/code-injection-detection branch from 9d1033b to c641b37 Compare April 22, 2025 07:53
@Pouyanpi
Pouyanpi merged commit 1bc565e into develop Apr 22, 2025
@Pouyanpi
Pouyanpi deleted the feature/code-injection-detection branch April 22, 2025 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants