Fix: Command Injection and Sandbox Escape in CodeInterpreterTool (#4516)#4517
Open
devin-ai-integration[bot] wants to merge 4 commits intomainfrom
Open
Fix: Command Injection and Sandbox Escape in CodeInterpreterTool (#4516)#4517devin-ai-integration[bot] wants to merge 4 commits intomainfrom
devin-ai-integration[bot] wants to merge 4 commits intomainfrom
Conversation
- Replace os.system() with subprocess.run() using list args in run_code_unsafe() to prevent command injection via malicious library names (CWE-78) - Add BLOCKED_ATTRS set to SandboxPython to block dangerous dunder attributes (__class__, __bases__, __subclasses__, __mro__, __globals__, __code__, __reduce__, __reduce_ex__, __builtins__) that enable sandbox escape (CWE-94) - Add getattr, setattr, delattr, type, breakpoint to UNSAFE_BUILTINS - Add _check_for_blocked_attrs() pre-execution scan for blocked attribute patterns - Add comprehensive tests for both vulnerabilities Co-Authored-By: João <joao@crewai.com>
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Co-Authored-By: João <joao@crewai.com>
…s://git-manager.devin.ai/proxy/github.com/crewAIInc/crewAI into devin/1771500257-fix-code-interpreter-security
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fix: Command Injection and Sandbox Escape in CodeInterpreterTool (#4516)
Summary
Addresses two security vulnerabilities reported in #4516:
Command Injection (CWE-78):
run_code_unsafe()passed user-provided library names directly toos.system(), enabling shell command injection. Fixed by replacing withsubprocess.run()using list arguments, which avoids shell interpretation.Sandbox Escape (CWE-94):
SandboxPythonfailed to block Python object introspection methods (__class__,__bases__,__subclasses__, etc.) that allow traversing the object hierarchy to access blocked modules. Fixed by:BLOCKED_ATTRSset of dangerous dunder attributes_check_for_blocked_attrs) that rejects code containing these patternsgetattr,setattr,delattr,type,breakpointtoUNSAFE_BUILTINS17 new tests added covering both vulnerability classes.
Updates since last revision
noqacomment placement:S603suppression moved to thesubprocess.run(line (where ruff expects it),S607remains on the args line.tool.specs.jsoncame in via merge withmain(removesAvailableModelenum andmodel_namefrom Web Automation Tool specs) — not part of this fix.Review & Testing Checklist for Human
typeas a builtin is aggressive —type(x)is extremely common in normal Python for type-checking. This will break legitimate sandbox code that usestype(). Decide whether to keep it blocked, allowlist single-argtype(), or remove it fromUNSAFE_BUILTINS._check_for_blocked_attrsmatches at the string level, so code likex = "__class__"or# comment mentioning __bases__will be rejected even though it's harmless. Evaluate if this trade-off is acceptable.chr(95)+chr(95)+...) or indirect access patterns could evade string-level detection.getattr/setattrare also blocked (reducing attack surface), but this is defense-in-depth, not a complete solution. Consider whether mandatory Docker or RestrictedPython is warranted for stronger guarantees.MagicMockimport in test file — minor, was added but never used.Suggested test plan: Run the full code interpreter test suite, then manually try common sandbox patterns — especially
type(x), string literals containing dunder names, and basic arithmetic/list operations — to verify no regressions in legitimate use cases.Notes
Requested by: João
Link to Devin run