Repository navigation
feat(add-new-block_code_execution-guardrail): prevent agent from executing code #22056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
20fdedf
feat(add-new-block_code_execution-guardrail): prevent agent from exec…
adda887
feat: working block code execution guardrail
7cac456
feat(ui/): add new guardrails to guardrail_garden
51b2961
fix: fix greptile feedback
e9a96dd
feat: minor cleanup
8fe2d58
docs: cleanup agent instructions
62f33ac
feat: cleanup ui
1dbdb0d
Update litellm/proxy/guardrails/guardrail_hooks/block_code_execution/…
69475d7
Merge branch 'main' into litellm_dev_02_23_2026_p2
ed5bdab
fix: remove unused code
7098155
fix: greptile fixes
d4172f7
fix: fix linting errors
31fb3af
fix: address greptile comments
2fbe1b3
Merge branch 'main' into litellm_dev_02_23_2026_p2
b6de52e
fix: improve block code execution guardrail
7a8d957
fix: cleanup
2307de0
fix: fix greptile issues
490beb5
Fix block_code_execution guardrail: resolve response-side bypass and …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
litellm/proxy/guardrails/guardrail_hooks/block_code_execution/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """Block Code Execution guardrail: blocks or masks fenced code blocks by language.""" | ||
|
|
||
| from typing import TYPE_CHECKING, Any, List, Literal, Optional, Union, cast | ||
|
|
||
| from litellm.types.guardrails import (GuardrailEventHooks, | ||
| SupportedGuardrailIntegrations) | ||
|
|
||
| from .block_code_execution import BlockCodeExecutionGuardrail | ||
|
|
||
| if TYPE_CHECKING: | ||
| from litellm.types.guardrails import Guardrail, LitellmParams | ||
|
|
||
| # Default: run on both request and response (and during_call is supported too) | ||
| DEFAULT_EVENT_HOOKS = [ | ||
| GuardrailEventHooks.pre_call.value, | ||
| GuardrailEventHooks.post_call.value, | ||
| ] | ||
|
|
||
|
|
||
| def _get_param( | ||
| litellm_params: "LitellmParams", | ||
| guardrail: "Guardrail", | ||
| key: str, | ||
| default: Any = None, | ||
| ) -> Any: | ||
| """Get a param from litellm_params, with fallback to raw guardrail litellm_params (for extra fields not on LitellmParams).""" | ||
| value = getattr(litellm_params, key, default) | ||
| if value is not None: | ||
| return value | ||
| raw = guardrail.get("litellm_params") | ||
| if isinstance(raw, dict) and key in raw: | ||
| return raw[key] | ||
| return default | ||
|
|
||
|
|
||
| def initialize_guardrail( | ||
| litellm_params: "LitellmParams", | ||
| guardrail: "Guardrail", | ||
| ) -> BlockCodeExecutionGuardrail: | ||
| """Initialize the Block Code Execution guardrail from config.""" | ||
| import litellm | ||
|
|
||
| guardrail_name = guardrail.get("guardrail_name") | ||
| if not guardrail_name: | ||
| raise ValueError( | ||
| "Block Code Execution guardrail requires a guardrail_name" | ||
| ) | ||
|
|
||
| blocked_languages: Optional[List[str]] = cast( | ||
| Optional[List[str]], | ||
| _get_param(litellm_params, guardrail, "blocked_languages"), | ||
| ) | ||
| action = cast( | ||
| Literal["block", "mask"], | ||
| _get_param(litellm_params, guardrail, "action", "block"), | ||
| ) | ||
| confidence_threshold = float( | ||
| cast( | ||
| Union[int, float, str], | ||
| _get_param(litellm_params, guardrail, "confidence_threshold", 0.5), | ||
| ) | ||
| ) | ||
| mode = _get_param(litellm_params, guardrail, "mode") | ||
| event_hook = cast( | ||
| Optional[Union[Literal["pre_call", "post_call", "during_call"], List[str]]], | ||
| mode if mode is not None else DEFAULT_EVENT_HOOKS, | ||
| ) | ||
|
|
||
| instance = BlockCodeExecutionGuardrail( | ||
| guardrail_name=guardrail_name, | ||
| blocked_languages=blocked_languages, | ||
| action=action, | ||
| confidence_threshold=confidence_threshold, | ||
| event_hook=event_hook, | ||
| default_on=bool(_get_param(litellm_params, guardrail, "default_on", False)), | ||
| ) | ||
| litellm.logging_callback_manager.add_litellm_callback(instance) | ||
| return instance | ||
|
|
||
|
|
||
| guardrail_initializer_registry = { | ||
| SupportedGuardrailIntegrations.BLOCK_CODE_EXECUTION.value: initialize_guardrail, | ||
| } | ||
|
|
||
| guardrail_class_registry = { | ||
| SupportedGuardrailIntegrations.BLOCK_CODE_EXECUTION.value: BlockCodeExecutionGuardrail, | ||
| } | ||
|
|
||
| __all__ = [ | ||
| "BlockCodeExecutionGuardrail", | ||
| "initialize_guardrail", | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import style reformatted by non-standard formatter
The imports in this file were reformatted from the standard Black-compatible
from X import (\n A,\n B,\n)multi-line style to backslash-continuation style (from X import \\\n Y) and compressed grouping. This is inconsistent with the codebase's Black/Ruff formatting conventions (perCLAUDE.md), and the change is unrelated to the feature being added.These formatting changes add noise to the diff and may conflict with future Black formatting runs. Consider reverting the import reformatting to maintain consistency.
Context Used: Context from
dashboard- CLAUDE.md (source)Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!